HttpHeaderProcessorTest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "HttpHeaderProcessor.h"
  2. #include "HttpHeader.h"
  3. #include "DlRetryEx.h"
  4. #include "DlAbortEx.h"
  5. #include <cppunit/extensions/HelperMacros.h>
  6. namespace aria2 {
  7. class HttpHeaderProcessorTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(HttpHeaderProcessorTest);
  9. CPPUNIT_TEST(testUpdate1);
  10. CPPUNIT_TEST(testUpdate2);
  11. CPPUNIT_TEST(testGetPutBackDataLength);
  12. CPPUNIT_TEST(testGetPutBackDataLength_nullChar);
  13. CPPUNIT_TEST(testGetHttpStatusHeader);
  14. CPPUNIT_TEST(testGetHttpStatusHeader_empty);
  15. CPPUNIT_TEST(testGetHttpStatusHeader_statusOnly);
  16. CPPUNIT_TEST(testGetHttpStatusHeader_insufficientStatusLength);
  17. CPPUNIT_TEST(testBeyondLimit);
  18. CPPUNIT_TEST(testGetHeaderString);
  19. CPPUNIT_TEST_SUITE_END();
  20. public:
  21. void testUpdate1();
  22. void testUpdate2();
  23. void testGetPutBackDataLength();
  24. void testGetPutBackDataLength_nullChar();
  25. void testGetHttpStatusHeader();
  26. void testGetHttpStatusHeader_empty();
  27. void testGetHttpStatusHeader_statusOnly();
  28. void testGetHttpStatusHeader_insufficientStatusLength();
  29. void testBeyondLimit();
  30. void testGetHeaderString();
  31. };
  32. CPPUNIT_TEST_SUITE_REGISTRATION( HttpHeaderProcessorTest );
  33. void HttpHeaderProcessorTest::testUpdate1()
  34. {
  35. HttpHeaderProcessor proc;
  36. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  37. proc.update(hd1);
  38. CPPUNIT_ASSERT(!proc.eoh());
  39. proc.update("\r\n");
  40. CPPUNIT_ASSERT(proc.eoh());
  41. }
  42. void HttpHeaderProcessorTest::testUpdate2()
  43. {
  44. HttpHeaderProcessor proc;
  45. std::string hd1 = "HTTP/1.1 200 OK\n";
  46. proc.update(hd1);
  47. CPPUNIT_ASSERT(!proc.eoh());
  48. proc.update("\n");
  49. CPPUNIT_ASSERT(proc.eoh());
  50. }
  51. void HttpHeaderProcessorTest::testGetPutBackDataLength()
  52. {
  53. HttpHeaderProcessor proc;
  54. std::string hd1 = "HTTP/1.1 200 OK\r\n"
  55. "\r\nputbackme";
  56. proc.update(hd1);
  57. CPPUNIT_ASSERT(proc.eoh());
  58. CPPUNIT_ASSERT_EQUAL((int32_t)9, proc.getPutBackDataLength());
  59. proc.clear();
  60. std::string hd2 = "HTTP/1.1 200 OK\n"
  61. "\nputbackme";
  62. proc.update(hd2);
  63. CPPUNIT_ASSERT(proc.eoh());
  64. CPPUNIT_ASSERT_EQUAL((int32_t)9, proc.getPutBackDataLength());
  65. }
  66. void HttpHeaderProcessorTest::testGetPutBackDataLength_nullChar()
  67. {
  68. HttpHeaderProcessor proc;
  69. const char* x = "HTTP/1.1 200 OK\r\n"
  70. "foo: foo\0bar\r\n"
  71. "\r\nputbackme";
  72. std::string hd1(&x[0], &x[42]);
  73. proc.update(hd1);
  74. CPPUNIT_ASSERT(proc.eoh());
  75. CPPUNIT_ASSERT_EQUAL((int32_t)9, proc.getPutBackDataLength());
  76. }
  77. void HttpHeaderProcessorTest::testGetHttpStatusHeader()
  78. {
  79. HttpHeaderProcessor proc;
  80. std::string hd = "HTTP/1.1 200 OK\r\n"
  81. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  82. "Server: Apache/2.2.3 (Debian)\r\n"
  83. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  84. "ETag: \"594065-23e3-50825cc0\"\r\n"
  85. "Accept-Ranges: bytes\r\n"
  86. "Content-Length: 9187\r\n"
  87. "Connection: close\r\n"
  88. "Content-Type: text/html; charset=UTF-8\r\n"
  89. "\r\n";
  90. proc.update(hd);
  91. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  92. std::string status = statusHeader.first;
  93. SharedHandle<HttpHeader> header = statusHeader.second;
  94. CPPUNIT_ASSERT_EQUAL(std::string("200"), status);
  95. CPPUNIT_ASSERT_EQUAL(std::string("Mon, 25 Jun 2007 16:04:59 GMT"), header->getFirst("Date"));
  96. CPPUNIT_ASSERT_EQUAL(std::string("Apache/2.2.3 (Debian)"), header->getFirst("Server"));
  97. CPPUNIT_ASSERT_EQUAL((int64_t)9187LL, header->getFirstAsLLInt("Content-Length"));
  98. CPPUNIT_ASSERT_EQUAL(std::string("text/html; charset=UTF-8"), header->getFirst("Content-Type"));
  99. }
  100. void HttpHeaderProcessorTest::testGetHttpStatusHeader_empty()
  101. {
  102. HttpHeaderProcessor proc;
  103. try {
  104. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  105. CPPUNIT_FAIL("Exception must be thrown.");
  106. } catch(DlRetryEx* ex) {
  107. std::cout << ex->getMsg() << std::endl;
  108. delete ex;
  109. }
  110. }
  111. void HttpHeaderProcessorTest::testGetHttpStatusHeader_statusOnly()
  112. {
  113. HttpHeaderProcessor proc;
  114. std::string hd = "HTTP/1.1 200\r\n\r\n";
  115. proc.update(hd);
  116. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  117. CPPUNIT_ASSERT_EQUAL(std::string("200"), statusHeader.first);
  118. CPPUNIT_ASSERT(!statusHeader.second.isNull());
  119. }
  120. void HttpHeaderProcessorTest::testGetHttpStatusHeader_insufficientStatusLength()
  121. {
  122. HttpHeaderProcessor proc;
  123. std::string hd = "HTTP/1.1 20\r\n\r\n";
  124. proc.update(hd);
  125. try {
  126. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  127. CPPUNIT_FAIL("Exception must be thrown.");
  128. } catch(DlRetryEx* ex) {
  129. std::cout << ex->getMsg() << std::endl;
  130. delete ex;
  131. }
  132. }
  133. void HttpHeaderProcessorTest::testBeyondLimit()
  134. {
  135. HttpHeaderProcessor proc;
  136. proc.setHeaderLimit(20);
  137. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  138. std::string hd2 = "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n";
  139. proc.update(hd1);
  140. try {
  141. proc.update(hd2);
  142. CPPUNIT_FAIL("Exception must be thrown.");
  143. } catch(DlAbortEx* ex) {
  144. std::cout << ex->getMsg() << std::endl;
  145. delete ex;
  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