HttpHeaderProcessorTest.cc 5.7 KB

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