HttpHeaderProcessorTest.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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(testUpdate1);
  11. CPPUNIT_TEST(testUpdate2);
  12. CPPUNIT_TEST(testGetPutBackDataLength);
  13. CPPUNIT_TEST(testGetPutBackDataLength_nullChar);
  14. CPPUNIT_TEST(testGetHttpResponseHeader);
  15. CPPUNIT_TEST(testGetHttpResponseHeader_empty);
  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 testUpdate1();
  24. void testUpdate2();
  25. void testGetPutBackDataLength();
  26. void testGetPutBackDataLength_nullChar();
  27. void testGetHttpResponseHeader();
  28. void testGetHttpResponseHeader_empty();
  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::testUpdate1()
  37. {
  38. HttpHeaderProcessor proc;
  39. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  40. proc.update(hd1);
  41. CPPUNIT_ASSERT(!proc.eoh());
  42. proc.update("\r\n");
  43. CPPUNIT_ASSERT(proc.eoh());
  44. }
  45. void HttpHeaderProcessorTest::testUpdate2()
  46. {
  47. HttpHeaderProcessor proc;
  48. std::string hd1 = "HTTP/1.1 200 OK\n";
  49. proc.update(hd1);
  50. CPPUNIT_ASSERT(!proc.eoh());
  51. proc.update("\n");
  52. CPPUNIT_ASSERT(proc.eoh());
  53. }
  54. void HttpHeaderProcessorTest::testGetPutBackDataLength()
  55. {
  56. HttpHeaderProcessor proc;
  57. std::string hd1 = "HTTP/1.1 200 OK\r\n"
  58. "\r\nputbackme";
  59. proc.update(hd1);
  60. CPPUNIT_ASSERT(proc.eoh());
  61. CPPUNIT_ASSERT_EQUAL((size_t)9, proc.getPutBackDataLength());
  62. proc.clear();
  63. std::string hd2 = "HTTP/1.1 200 OK\n"
  64. "\nputbackme";
  65. proc.update(hd2);
  66. CPPUNIT_ASSERT(proc.eoh());
  67. CPPUNIT_ASSERT_EQUAL((size_t)9, proc.getPutBackDataLength());
  68. }
  69. void HttpHeaderProcessorTest::testGetPutBackDataLength_nullChar()
  70. {
  71. HttpHeaderProcessor proc;
  72. const char* x = "HTTP/1.1 200 OK\r\n"
  73. "foo: foo\0bar\r\n"
  74. "\r\nputbackme";
  75. std::string hd1(&x[0], &x[42]);
  76. proc.update(hd1);
  77. CPPUNIT_ASSERT(proc.eoh());
  78. CPPUNIT_ASSERT_EQUAL((size_t)9, proc.getPutBackDataLength());
  79. }
  80. void HttpHeaderProcessorTest::testGetHttpResponseHeader()
  81. {
  82. HttpHeaderProcessor proc;
  83. std::string hd = "HTTP/1.1 200 OK\r\n"
  84. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  85. "Server: Apache/2.2.3 (Debian)\r\n"
  86. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  87. "ETag: \"594065-23e3-50825cc0\"\r\n"
  88. "Accept-Ranges: bytes\r\n"
  89. "Content-Length: 9187\r\n"
  90. "Connection: close\r\n"
  91. "Content-Type: text/html; charset=UTF-8\r\n"
  92. "\r\n"
  93. "Entity: body";
  94. proc.update(hd);
  95. SharedHandle<HttpHeader> header = proc.getHttpResponseHeader();
  96. CPPUNIT_ASSERT_EQUAL(200, header->getStatusCode());
  97. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), header->getVersion());
  98. CPPUNIT_ASSERT_EQUAL(std::string("Mon, 25 Jun 2007 16:04:59 GMT"),
  99. header->find("date"));
  100. CPPUNIT_ASSERT_EQUAL(std::string("Apache/2.2.3 (Debian)"),
  101. header->find("server"));
  102. CPPUNIT_ASSERT_EQUAL((uint64_t)9187ULL,
  103. header->findAsULLInt("content-length"));
  104. CPPUNIT_ASSERT_EQUAL(std::string("text/html; charset=UTF-8"),
  105. header->find("content-type"));
  106. CPPUNIT_ASSERT(!header->defined("entity"));
  107. }
  108. void HttpHeaderProcessorTest::testGetHttpResponseHeader_empty()
  109. {
  110. HttpHeaderProcessor proc;
  111. try {
  112. proc.getHttpResponseHeader();
  113. CPPUNIT_FAIL("Exception must be thrown.");
  114. } catch(DlRetryEx& ex) {
  115. std::cout << ex.stackTrace() << std::endl;
  116. }
  117. }
  118. void HttpHeaderProcessorTest::testGetHttpResponseHeader_statusOnly()
  119. {
  120. HttpHeaderProcessor proc;
  121. std::string hd = "HTTP/1.1 200\r\n\r\n";
  122. proc.update(hd);
  123. SharedHandle<HttpHeader> header = proc.getHttpResponseHeader();
  124. CPPUNIT_ASSERT_EQUAL(200, header->getStatusCode());
  125. }
  126. void HttpHeaderProcessorTest::testGetHttpResponseHeader_insufficientStatusLength()
  127. {
  128. HttpHeaderProcessor proc;
  129. std::string hd = "HTTP/1.1 20\r\n\r\n";
  130. proc.update(hd);
  131. try {
  132. proc.getHttpResponseHeader();
  133. CPPUNIT_FAIL("Exception must be thrown.");
  134. } catch(DlRetryEx& ex) {
  135. std::cout << ex.stackTrace() << std::endl;
  136. }
  137. }
  138. void HttpHeaderProcessorTest::testBeyondLimit()
  139. {
  140. HttpHeaderProcessor proc;
  141. proc.setHeaderLimit(20);
  142. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  143. std::string hd2 = "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n";
  144. proc.update(hd1);
  145. try {
  146. proc.update(hd2);
  147. CPPUNIT_FAIL("Exception must be thrown.");
  148. } catch(DlAbortEx& ex) {
  149. std::cout << ex.stackTrace() << std::endl;
  150. }
  151. }
  152. void HttpHeaderProcessorTest::testGetHeaderString()
  153. {
  154. HttpHeaderProcessor proc;
  155. std::string hd = "HTTP/1.1 200 OK\r\n"
  156. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  157. "Server: Apache/2.2.3 (Debian)\r\n"
  158. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  159. "ETag: \"594065-23e3-50825cc0\"\r\n"
  160. "Accept-Ranges: bytes\r\n"
  161. "Content-Length: 9187\r\n"
  162. "Connection: close\r\n"
  163. "Content-Type: text/html; charset=UTF-8\r\n"
  164. "\r\nputbackme";
  165. proc.update(hd);
  166. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1 200 OK\r\n"
  167. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  168. "Server: Apache/2.2.3 (Debian)\r\n"
  169. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  170. "ETag: \"594065-23e3-50825cc0\"\r\n"
  171. "Accept-Ranges: bytes\r\n"
  172. "Content-Length: 9187\r\n"
  173. "Connection: close\r\n"
  174. "Content-Type: text/html; charset=UTF-8"),
  175. proc.getHeaderString());
  176. }
  177. void HttpHeaderProcessorTest::testGetHttpRequestHeader()
  178. {
  179. HttpHeaderProcessor proc;
  180. std::string request = "GET /index.html HTTP/1.1\r\n"
  181. "Host: host\r\n"
  182. "Connection: close\r\n"
  183. "\r\n"
  184. "Entity: body";
  185. proc.update(request);
  186. SharedHandle<HttpHeader> httpHeader = proc.getHttpRequestHeader();
  187. CPPUNIT_ASSERT(httpHeader);
  188. CPPUNIT_ASSERT_EQUAL(std::string("GET"), httpHeader->getMethod());
  189. CPPUNIT_ASSERT_EQUAL(std::string("/index.html"),httpHeader->getRequestPath());
  190. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), httpHeader->getVersion());
  191. CPPUNIT_ASSERT_EQUAL(std::string("close"),httpHeader->find("connection"));
  192. CPPUNIT_ASSERT(!httpHeader->defined("entity"));
  193. }
  194. } // namespace aria2