HttpHeaderProcessorTest.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "HttpHeaderProcessor.h"
  2. #include <iostream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "HttpHeader.h"
  5. #include "DlRetryEx.h"
  6. #include "DlAbortEx.h"
  7. namespace aria2 {
  8. class HttpHeaderProcessorTest:public CppUnit::TestFixture {
  9. CPPUNIT_TEST_SUITE(HttpHeaderProcessorTest);
  10. CPPUNIT_TEST(testParse1);
  11. CPPUNIT_TEST(testParse2);
  12. CPPUNIT_TEST(testParse3);
  13. CPPUNIT_TEST(testGetLastBytesProcessed);
  14. CPPUNIT_TEST(testGetLastBytesProcessed_nullChar);
  15. CPPUNIT_TEST(testGetHttpResponseHeader);
  16. CPPUNIT_TEST(testGetHttpResponseHeader_statusOnly);
  17. CPPUNIT_TEST(testGetHttpResponseHeader_insufficientStatusLength);
  18. CPPUNIT_TEST(testGetHttpResponseHeader_nameStartsWs);
  19. CPPUNIT_TEST(testBeyondLimit);
  20. CPPUNIT_TEST(testGetHeaderString);
  21. CPPUNIT_TEST(testGetHttpRequestHeader);
  22. CPPUNIT_TEST_SUITE_END();
  23. public:
  24. void testParse1();
  25. void testParse2();
  26. void testParse3();
  27. void testGetLastBytesProcessed();
  28. void testGetLastBytesProcessed_nullChar();
  29. void testGetHttpResponseHeader();
  30. void testGetHttpResponseHeader_statusOnly();
  31. void testGetHttpResponseHeader_insufficientStatusLength();
  32. void testGetHttpResponseHeader_nameStartsWs();
  33. void testBeyondLimit();
  34. void testGetHeaderString();
  35. void testGetHttpRequestHeader();
  36. };
  37. CPPUNIT_TEST_SUITE_REGISTRATION( HttpHeaderProcessorTest );
  38. void HttpHeaderProcessorTest::testParse1()
  39. {
  40. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  41. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  42. CPPUNIT_ASSERT(!proc.parse(hd1));
  43. CPPUNIT_ASSERT(proc.parse("\r\n"));
  44. }
  45. void HttpHeaderProcessorTest::testParse2()
  46. {
  47. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  48. std::string hd1 = "HTTP/1.1 200 OK\n";
  49. CPPUNIT_ASSERT(!proc.parse(hd1));
  50. CPPUNIT_ASSERT(proc.parse("\n"));
  51. }
  52. void HttpHeaderProcessorTest::testParse3()
  53. {
  54. HttpHeaderProcessor proc(HttpHeaderProcessor::SERVER_PARSER);
  55. std::string s =
  56. "GET / HTTP/1.1\r\n"
  57. "Host: aria2.sourceforge.net\r\n"
  58. "Connection: close \r\n" // trailing white space (BWS)
  59. "Accept-Encoding: text1\r\n" // Multi-line header
  60. " text2\r\n"
  61. " text3\r\n"
  62. "Authorization: foo\r\n"
  63. "Authorization: bar\r\n"
  64. "Content-Type:\r\n"
  65. "\r\n";
  66. CPPUNIT_ASSERT(proc.parse(s));
  67. auto h = proc.getResult();
  68. CPPUNIT_ASSERT_EQUAL(std::string("close"), h->find(HttpHeader::CONNECTION));
  69. CPPUNIT_ASSERT_EQUAL(std::string("text1 text2 text3"),
  70. h->find(HttpHeader::ACCEPT_ENCODING));
  71. CPPUNIT_ASSERT_EQUAL(std::string("foo"),
  72. h->findAll(HttpHeader::AUTHORIZATION)[0]);
  73. CPPUNIT_ASSERT_EQUAL(std::string("bar"),
  74. h->findAll(HttpHeader::AUTHORIZATION)[1]);
  75. CPPUNIT_ASSERT_EQUAL(std::string(""), h->find(HttpHeader::CONTENT_TYPE));
  76. CPPUNIT_ASSERT(h->defined(HttpHeader::CONTENT_TYPE));
  77. }
  78. void HttpHeaderProcessorTest::testGetLastBytesProcessed()
  79. {
  80. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  81. std::string hd1 =
  82. "HTTP/1.1 200 OK\r\n"
  83. "\r\nputbackme";
  84. CPPUNIT_ASSERT(proc.parse(hd1));
  85. CPPUNIT_ASSERT_EQUAL((size_t)19, proc.getLastBytesProcessed());
  86. proc.clear();
  87. std::string hd2 =
  88. "HTTP/1.1 200 OK\n"
  89. "\nputbackme";
  90. CPPUNIT_ASSERT(proc.parse(hd2));
  91. CPPUNIT_ASSERT_EQUAL((size_t)17, proc.getLastBytesProcessed());
  92. }
  93. void HttpHeaderProcessorTest::testGetLastBytesProcessed_nullChar()
  94. {
  95. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  96. const char x[] =
  97. "HTTP/1.1 200 OK\r\n"
  98. "foo: foo\0bar\r\n"
  99. "\r\nputbackme";
  100. std::string hd1(&x[0], &x[sizeof(x)-1]);
  101. CPPUNIT_ASSERT(proc.parse(hd1));
  102. CPPUNIT_ASSERT_EQUAL((size_t)33, proc.getLastBytesProcessed());
  103. }
  104. void HttpHeaderProcessorTest::testGetHttpResponseHeader()
  105. {
  106. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  107. std::string hd =
  108. "HTTP/1.1 404 Not Found\r\n"
  109. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  110. "Server: Apache/2.2.3 (Debian)\r\n"
  111. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  112. "ETag: \"594065-23e3-50825cc0\"\r\n"
  113. "Accept-Ranges: bytes\r\n"
  114. "Content-Length: 9187\r\n"
  115. "Connection: close\r\n"
  116. "Content-Type: text/html; charset=UTF-8\r\n"
  117. "\r\n"
  118. "Content-Encoding: body";
  119. CPPUNIT_ASSERT(proc.parse(hd));
  120. auto header = proc.getResult();
  121. CPPUNIT_ASSERT_EQUAL(404, header->getStatusCode());
  122. CPPUNIT_ASSERT_EQUAL(std::string("Not Found"), header->getReasonPhrase());
  123. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), header->getVersion());
  124. CPPUNIT_ASSERT_EQUAL(std::string("9187"),
  125. header->find(HttpHeader::CONTENT_LENGTH));
  126. CPPUNIT_ASSERT_EQUAL(std::string("text/html; charset=UTF-8"),
  127. header->find(HttpHeader::CONTENT_TYPE));
  128. CPPUNIT_ASSERT(!header->defined(HttpHeader::CONTENT_ENCODING));
  129. }
  130. void HttpHeaderProcessorTest::testGetHttpResponseHeader_statusOnly()
  131. {
  132. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  133. std::string hd = "HTTP/1.1 200\r\n\r\n";
  134. CPPUNIT_ASSERT(proc.parse(hd));
  135. auto header = proc.getResult();
  136. CPPUNIT_ASSERT_EQUAL(200, header->getStatusCode());
  137. }
  138. void HttpHeaderProcessorTest::testGetHttpResponseHeader_insufficientStatusLength()
  139. {
  140. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  141. std::string hd = "HTTP/1.1 20\r\n\r\n";
  142. try {
  143. proc.parse(hd);
  144. CPPUNIT_FAIL("Exception must be thrown.");
  145. } catch(DlAbortEx& ex) {
  146. // Success
  147. }
  148. }
  149. void HttpHeaderProcessorTest::testGetHttpResponseHeader_nameStartsWs()
  150. {
  151. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  152. std::string hd =
  153. "HTTP/1.1 200\r\n"
  154. " foo:bar\r\n"
  155. "\r\n";
  156. try {
  157. proc.parse(hd);
  158. CPPUNIT_FAIL("Exception must be thrown.");
  159. } catch(DlAbortEx& ex) {
  160. // Success
  161. }
  162. proc.clear();
  163. hd =
  164. "HTTP/1.1 200\r\n"
  165. ":foo:bar\r\n"
  166. "\r\n";
  167. try {
  168. proc.parse(hd);
  169. CPPUNIT_FAIL("Exception must be thrown.");
  170. } catch(DlAbortEx& ex) {
  171. // Success
  172. }
  173. proc.clear();
  174. hd =
  175. "HTTP/1.1 200\r\n"
  176. ":foo\r\n"
  177. "\r\n";
  178. try {
  179. proc.parse(hd);
  180. CPPUNIT_FAIL("Exception must be thrown.");
  181. } catch(DlAbortEx& ex) {
  182. // Success
  183. }
  184. }
  185. void HttpHeaderProcessorTest::testBeyondLimit()
  186. {
  187. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  188. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  189. std::string hd2 = std::string(1025, 'A');
  190. proc.parse(hd1);
  191. try {
  192. proc.parse(hd2);
  193. CPPUNIT_FAIL("Exception must be thrown.");
  194. } catch(DlAbortEx& ex) {
  195. // Success
  196. }
  197. }
  198. void HttpHeaderProcessorTest::testGetHeaderString()
  199. {
  200. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  201. std::string hd =
  202. "HTTP/1.1 200 OK\r\n"
  203. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  204. "Server: Apache/2.2.3 (Debian)\r\n"
  205. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  206. "ETag: \"594065-23e3-50825cc0\"\r\n"
  207. "Accept-Ranges: bytes\r\n"
  208. "Content-Length: 9187\r\n"
  209. "Connection: close\r\n"
  210. "Content-Type: text/html; charset=UTF-8\r\n"
  211. "\r\nputbackme";
  212. CPPUNIT_ASSERT(proc.parse(hd));
  213. CPPUNIT_ASSERT_EQUAL
  214. (std::string("HTTP/1.1 200 OK\r\n"
  215. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  216. "Server: Apache/2.2.3 (Debian)\r\n"
  217. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  218. "ETag: \"594065-23e3-50825cc0\"\r\n"
  219. "Accept-Ranges: bytes\r\n"
  220. "Content-Length: 9187\r\n"
  221. "Connection: close\r\n"
  222. "Content-Type: text/html; charset=UTF-8\r\n"
  223. "\r\n"),
  224. proc.getHeaderString());
  225. }
  226. void HttpHeaderProcessorTest::testGetHttpRequestHeader()
  227. {
  228. HttpHeaderProcessor proc(HttpHeaderProcessor::SERVER_PARSER);
  229. std::string request =
  230. "GET /index.html HTTP/1.1\r\n"
  231. "Host: host\r\n"
  232. "Connection: close\r\n"
  233. "\r\n"
  234. "Content-Encoding: body";
  235. CPPUNIT_ASSERT(proc.parse(request));
  236. auto httpHeader = proc.getResult();
  237. CPPUNIT_ASSERT_EQUAL(std::string("GET"), httpHeader->getMethod());
  238. CPPUNIT_ASSERT_EQUAL(std::string("/index.html"),httpHeader->getRequestPath());
  239. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), httpHeader->getVersion());
  240. CPPUNIT_ASSERT_EQUAL(std::string("close"),
  241. httpHeader->find(HttpHeader::CONNECTION));
  242. CPPUNIT_ASSERT(!httpHeader->defined(HttpHeader::CONTENT_ENCODING));
  243. }
  244. } // namespace aria2