HttpHeaderProcessorTest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "HttpHeaderProcessor.h"
  2. #include "HttpHeader.h"
  3. #include "DlRetryEx.h"
  4. #include "DlAbortEx.h"
  5. #include <iostream>
  6. #include <cppunit/extensions/HelperMacros.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. proc.update(hd);
  94. SharedHandle<HttpHeader> header = proc.getHttpResponseHeader();
  95. CPPUNIT_ASSERT_EQUAL(200, header->getStatusCode());
  96. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), header->getVersion());
  97. CPPUNIT_ASSERT_EQUAL(std::string("Mon, 25 Jun 2007 16:04:59 GMT"),
  98. header->getFirst("Date"));
  99. CPPUNIT_ASSERT_EQUAL(std::string("Apache/2.2.3 (Debian)"),
  100. header->getFirst("Server"));
  101. CPPUNIT_ASSERT_EQUAL((uint64_t)9187ULL, header->getFirstAsULLInt("Content-Length"));
  102. CPPUNIT_ASSERT_EQUAL(std::string("text/html; charset=UTF-8"),
  103. header->getFirst("Content-Type"));
  104. }
  105. void HttpHeaderProcessorTest::testGetHttpResponseHeader_empty()
  106. {
  107. HttpHeaderProcessor proc;
  108. try {
  109. proc.getHttpResponseHeader();
  110. CPPUNIT_FAIL("Exception must be thrown.");
  111. } catch(DlRetryEx& ex) {
  112. std::cout << ex.stackTrace() << std::endl;
  113. }
  114. }
  115. void HttpHeaderProcessorTest::testGetHttpResponseHeader_statusOnly()
  116. {
  117. HttpHeaderProcessor proc;
  118. std::string hd = "HTTP/1.1 200\r\n\r\n";
  119. proc.update(hd);
  120. SharedHandle<HttpHeader> header = proc.getHttpResponseHeader();
  121. CPPUNIT_ASSERT_EQUAL(200, header->getStatusCode());
  122. }
  123. void HttpHeaderProcessorTest::testGetHttpResponseHeader_insufficientStatusLength()
  124. {
  125. HttpHeaderProcessor proc;
  126. std::string hd = "HTTP/1.1 20\r\n\r\n";
  127. proc.update(hd);
  128. try {
  129. proc.getHttpResponseHeader();
  130. CPPUNIT_FAIL("Exception must be thrown.");
  131. } catch(DlRetryEx& ex) {
  132. std::cout << ex.stackTrace() << std::endl;
  133. }
  134. }
  135. void HttpHeaderProcessorTest::testBeyondLimit()
  136. {
  137. HttpHeaderProcessor proc;
  138. proc.setHeaderLimit(20);
  139. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  140. std::string hd2 = "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n";
  141. proc.update(hd1);
  142. try {
  143. proc.update(hd2);
  144. CPPUNIT_FAIL("Exception must be thrown.");
  145. } catch(DlAbortEx& ex) {
  146. std::cout << ex.stackTrace() << std::endl;
  147. }
  148. }
  149. void HttpHeaderProcessorTest::testGetHeaderString()
  150. {
  151. HttpHeaderProcessor proc;
  152. std::string hd = "HTTP/1.1 200 OK\r\n"
  153. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  154. "Server: Apache/2.2.3 (Debian)\r\n"
  155. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  156. "ETag: \"594065-23e3-50825cc0\"\r\n"
  157. "Accept-Ranges: bytes\r\n"
  158. "Content-Length: 9187\r\n"
  159. "Connection: close\r\n"
  160. "Content-Type: text/html; charset=UTF-8\r\n"
  161. "\r\nputbackme";
  162. proc.update(hd);
  163. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1 200 OK\r\n"
  164. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  165. "Server: Apache/2.2.3 (Debian)\r\n"
  166. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  167. "ETag: \"594065-23e3-50825cc0\"\r\n"
  168. "Accept-Ranges: bytes\r\n"
  169. "Content-Length: 9187\r\n"
  170. "Connection: close\r\n"
  171. "Content-Type: text/html; charset=UTF-8"),
  172. proc.getHeaderString());
  173. }
  174. void HttpHeaderProcessorTest::testGetHttpRequestHeader()
  175. {
  176. HttpHeaderProcessor proc;
  177. std::string request = "GET /index.html HTTP/1.1\r\n"
  178. "Host: host\r\n"
  179. "Connection: close\r\n"
  180. "\r\n";
  181. proc.update(request);
  182. SharedHandle<HttpHeader> httpHeader = proc.getHttpRequestHeader();
  183. CPPUNIT_ASSERT(httpHeader);
  184. CPPUNIT_ASSERT_EQUAL(std::string("GET"), httpHeader->getMethod());
  185. CPPUNIT_ASSERT_EQUAL(std::string("/index.html"),httpHeader->getRequestPath());
  186. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), httpHeader->getVersion());
  187. CPPUNIT_ASSERT_EQUAL(std::string("close"),httpHeader->getFirst("Connection"));
  188. }
  189. } // namespace aria2