HttpHeaderProcessorTest.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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(testBeyondLimit);
  19. CPPUNIT_TEST(testGetHeaderString);
  20. CPPUNIT_TEST(testGetHttpRequestHeader);
  21. CPPUNIT_TEST_SUITE_END();
  22. public:
  23. void testParse1();
  24. void testParse2();
  25. void testParse3();
  26. void testGetLastBytesProcessed();
  27. void testGetLastBytesProcessed_nullChar();
  28. void testGetHttpResponseHeader();
  29. void testGetHttpResponseHeader_statusOnly();
  30. void testGetHttpResponseHeader_insufficientStatusLength();
  31. void testBeyondLimit();
  32. void testGetHeaderString();
  33. void testGetHttpRequestHeader();
  34. };
  35. CPPUNIT_TEST_SUITE_REGISTRATION( HttpHeaderProcessorTest );
  36. void HttpHeaderProcessorTest::testParse1()
  37. {
  38. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  39. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  40. CPPUNIT_ASSERT(!proc.parse(hd1));
  41. CPPUNIT_ASSERT(proc.parse("\r\n"));
  42. }
  43. void HttpHeaderProcessorTest::testParse2()
  44. {
  45. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  46. std::string hd1 = "HTTP/1.1 200 OK\n";
  47. CPPUNIT_ASSERT(!proc.parse(hd1));
  48. CPPUNIT_ASSERT(proc.parse("\n"));
  49. }
  50. void HttpHeaderProcessorTest::testParse3()
  51. {
  52. HttpHeaderProcessor proc(HttpHeaderProcessor::SERVER_PARSER);
  53. std::string s =
  54. "GET / HTTP/1.1\r\n"
  55. "Host: aria2.sourceforge.net\r\n"
  56. "Connection: close \r\n" // trailing white space (BWS)
  57. "Multi-Line: text1\r\n" // Multi-line header
  58. " text2\r\n"
  59. " text3\r\n"
  60. "Duplicate: foo\r\n"
  61. "Duplicate: bar\r\n"
  62. "\r\n";
  63. CPPUNIT_ASSERT(proc.parse(s));
  64. SharedHandle<HttpHeader> h = proc.getResult();
  65. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.net"),
  66. h->find("host"));
  67. CPPUNIT_ASSERT_EQUAL(std::string("close"),
  68. h->find("connection"));
  69. CPPUNIT_ASSERT_EQUAL(std::string("text1 text2 text3"),
  70. h->find("multi-line"));
  71. CPPUNIT_ASSERT_EQUAL(std::string("foo"),
  72. h->findAll("duplicate")[0]);
  73. CPPUNIT_ASSERT_EQUAL(std::string("bar"),
  74. h->findAll("duplicate")[1]);
  75. }
  76. void HttpHeaderProcessorTest::testGetLastBytesProcessed()
  77. {
  78. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  79. std::string hd1 =
  80. "HTTP/1.1 200 OK\r\n"
  81. "\r\nputbackme";
  82. CPPUNIT_ASSERT(proc.parse(hd1));
  83. CPPUNIT_ASSERT_EQUAL((size_t)19, proc.getLastBytesProcessed());
  84. proc.clear();
  85. std::string hd2 =
  86. "HTTP/1.1 200 OK\n"
  87. "\nputbackme";
  88. CPPUNIT_ASSERT(proc.parse(hd2));
  89. CPPUNIT_ASSERT_EQUAL((size_t)17, proc.getLastBytesProcessed());
  90. }
  91. void HttpHeaderProcessorTest::testGetLastBytesProcessed_nullChar()
  92. {
  93. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  94. const char x[] =
  95. "HTTP/1.1 200 OK\r\n"
  96. "foo: foo\0bar\r\n"
  97. "\r\nputbackme";
  98. std::string hd1(&x[0], &x[sizeof(x)-1]);
  99. CPPUNIT_ASSERT(proc.parse(hd1));
  100. CPPUNIT_ASSERT_EQUAL((size_t)33, proc.getLastBytesProcessed());
  101. }
  102. void HttpHeaderProcessorTest::testGetHttpResponseHeader()
  103. {
  104. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  105. std::string hd =
  106. "HTTP/1.1 404 Not Found\r\n"
  107. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  108. "Server: Apache/2.2.3 (Debian)\r\n"
  109. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  110. "ETag: \"594065-23e3-50825cc0\"\r\n"
  111. "Accept-Ranges: bytes\r\n"
  112. "Content-Length: 9187\r\n"
  113. "Connection: close\r\n"
  114. "Content-Type: text/html; charset=UTF-8\r\n"
  115. "\r\n"
  116. "Entity: body";
  117. CPPUNIT_ASSERT(proc.parse(hd));
  118. SharedHandle<HttpHeader> header = proc.getResult();
  119. CPPUNIT_ASSERT_EQUAL(404, header->getStatusCode());
  120. CPPUNIT_ASSERT_EQUAL(std::string("Not Found"), header->getReasonPhrase());
  121. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), header->getVersion());
  122. CPPUNIT_ASSERT_EQUAL(std::string("Mon, 25 Jun 2007 16:04:59 GMT"),
  123. header->find("date"));
  124. CPPUNIT_ASSERT_EQUAL(std::string("Apache/2.2.3 (Debian)"),
  125. header->find("server"));
  126. CPPUNIT_ASSERT_EQUAL((int64_t)9187LL,
  127. header->findAsLLInt("content-length"));
  128. CPPUNIT_ASSERT_EQUAL(std::string("text/html; charset=UTF-8"),
  129. header->find("content-type"));
  130. CPPUNIT_ASSERT(!header->defined("entity"));
  131. }
  132. void HttpHeaderProcessorTest::testGetHttpResponseHeader_statusOnly()
  133. {
  134. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  135. std::string hd = "HTTP/1.1 200\r\n\r\n";
  136. CPPUNIT_ASSERT(proc.parse(hd));
  137. SharedHandle<HttpHeader> header = proc.getResult();
  138. CPPUNIT_ASSERT_EQUAL(200, header->getStatusCode());
  139. }
  140. void HttpHeaderProcessorTest::testGetHttpResponseHeader_insufficientStatusLength()
  141. {
  142. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  143. std::string hd = "HTTP/1.1 20\r\n\r\n";
  144. try {
  145. proc.parse(hd);
  146. CPPUNIT_FAIL("Exception must be thrown.");
  147. } catch(DlAbortEx& ex) {
  148. // Success
  149. }
  150. }
  151. void HttpHeaderProcessorTest::testBeyondLimit()
  152. {
  153. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  154. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  155. std::string hd2 = std::string(1025, 'A');
  156. proc.parse(hd1);
  157. try {
  158. proc.parse(hd2);
  159. CPPUNIT_FAIL("Exception must be thrown.");
  160. } catch(DlAbortEx& ex) {
  161. // Success
  162. }
  163. }
  164. void HttpHeaderProcessorTest::testGetHeaderString()
  165. {
  166. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  167. std::string hd =
  168. "HTTP/1.1 200 OK\r\n"
  169. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  170. "Server: Apache/2.2.3 (Debian)\r\n"
  171. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  172. "ETag: \"594065-23e3-50825cc0\"\r\n"
  173. "Accept-Ranges: bytes\r\n"
  174. "Content-Length: 9187\r\n"
  175. "Connection: close\r\n"
  176. "Content-Type: text/html; charset=UTF-8\r\n"
  177. "\r\nputbackme";
  178. CPPUNIT_ASSERT(proc.parse(hd));
  179. CPPUNIT_ASSERT_EQUAL
  180. (std::string("HTTP/1.1 200 OK\r\n"
  181. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  182. "Server: Apache/2.2.3 (Debian)\r\n"
  183. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  184. "ETag: \"594065-23e3-50825cc0\"\r\n"
  185. "Accept-Ranges: bytes\r\n"
  186. "Content-Length: 9187\r\n"
  187. "Connection: close\r\n"
  188. "Content-Type: text/html; charset=UTF-8\r\n"
  189. "\r\n"),
  190. proc.getHeaderString());
  191. }
  192. void HttpHeaderProcessorTest::testGetHttpRequestHeader()
  193. {
  194. HttpHeaderProcessor proc(HttpHeaderProcessor::SERVER_PARSER);
  195. std::string request =
  196. "GET /index.html HTTP/1.1\r\n"
  197. "Host: host\r\n"
  198. "Connection: close\r\n"
  199. "\r\n"
  200. "Entity: body";
  201. CPPUNIT_ASSERT(proc.parse(request));
  202. SharedHandle<HttpHeader> httpHeader = proc.getResult();
  203. CPPUNIT_ASSERT_EQUAL(std::string("GET"), httpHeader->getMethod());
  204. CPPUNIT_ASSERT_EQUAL(std::string("/index.html"),httpHeader->getRequestPath());
  205. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), httpHeader->getVersion());
  206. CPPUNIT_ASSERT_EQUAL(std::string("close"),httpHeader->find("connection"));
  207. CPPUNIT_ASSERT(!httpHeader->defined("entity"));
  208. }
  209. } // namespace aria2