HttpHeaderProcessorTest.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. proc.update("HTTP/1.1 200 OK\r\n"
  70. "foo: foo\0bar\r\n"
  71. "\r\nputbackme", 35+7);
  72. CPPUNIT_ASSERT(proc.eoh());
  73. CPPUNIT_ASSERT_EQUAL((int32_t)9, proc.getPutBackDataLength());
  74. }
  75. void HttpHeaderProcessorTest::testGetHttpStatusHeader()
  76. {
  77. HttpHeaderProcessor proc;
  78. std::string hd = "HTTP/1.1 200 OK\r\n"
  79. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  80. "Server: Apache/2.2.3 (Debian)\r\n"
  81. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  82. "ETag: \"594065-23e3-50825cc0\"\r\n"
  83. "Accept-Ranges: bytes\r\n"
  84. "Content-Length: 9187\r\n"
  85. "Connection: close\r\n"
  86. "Content-Type: text/html; charset=UTF-8\r\n"
  87. "\r\n";
  88. proc.update(hd);
  89. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  90. std::string status = statusHeader.first;
  91. SharedHandle<HttpHeader> header = statusHeader.second;
  92. CPPUNIT_ASSERT_EQUAL(std::string("200"), status);
  93. CPPUNIT_ASSERT_EQUAL(std::string("Mon, 25 Jun 2007 16:04:59 GMT"), header->getFirst("Date"));
  94. CPPUNIT_ASSERT_EQUAL(std::string("Apache/2.2.3 (Debian)"), header->getFirst("Server"));
  95. CPPUNIT_ASSERT_EQUAL((int64_t)9187LL, header->getFirstAsLLInt("Content-Length"));
  96. CPPUNIT_ASSERT_EQUAL(std::string("text/html; charset=UTF-8"), header->getFirst("Content-Type"));
  97. }
  98. void HttpHeaderProcessorTest::testGetHttpStatusHeader_empty()
  99. {
  100. HttpHeaderProcessor proc;
  101. try {
  102. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  103. CPPUNIT_FAIL("Exception must be thrown.");
  104. } catch(DlRetryEx* ex) {
  105. std::cout << ex->getMsg() << std::endl;
  106. delete ex;
  107. }
  108. }
  109. void HttpHeaderProcessorTest::testGetHttpStatusHeader_statusOnly()
  110. {
  111. HttpHeaderProcessor proc;
  112. std::string hd = "HTTP/1.1 200\r\n\r\n";
  113. proc.update(hd);
  114. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  115. CPPUNIT_ASSERT_EQUAL(std::string("200"), statusHeader.first);
  116. CPPUNIT_ASSERT(!statusHeader.second.isNull());
  117. }
  118. void HttpHeaderProcessorTest::testGetHttpStatusHeader_insufficientStatusLength()
  119. {
  120. HttpHeaderProcessor proc;
  121. std::string hd = "HTTP/1.1 20\r\n\r\n";
  122. proc.update(hd);
  123. try {
  124. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  125. CPPUNIT_FAIL("Exception must be thrown.");
  126. } catch(DlRetryEx* ex) {
  127. std::cout << ex->getMsg() << std::endl;
  128. delete ex;
  129. }
  130. }
  131. void HttpHeaderProcessorTest::testBeyondLimit()
  132. {
  133. HttpHeaderProcessor proc;
  134. proc.setHeaderLimit(20);
  135. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  136. std::string hd2 = "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n";
  137. proc.update(hd1);
  138. try {
  139. proc.update(hd2);
  140. CPPUNIT_FAIL("Exception must be thrown.");
  141. } catch(DlAbortEx* ex) {
  142. std::cout << ex->getMsg() << std::endl;
  143. delete ex;
  144. }
  145. }
  146. void HttpHeaderProcessorTest::testGetHeaderString()
  147. {
  148. HttpHeaderProcessor proc;
  149. std::string hd = "HTTP/1.1 200 OK\r\n"
  150. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  151. "Server: Apache/2.2.3 (Debian)\r\n"
  152. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  153. "ETag: \"594065-23e3-50825cc0\"\r\n"
  154. "Accept-Ranges: bytes\r\n"
  155. "Content-Length: 9187\r\n"
  156. "Connection: close\r\n"
  157. "Content-Type: text/html; charset=UTF-8\r\n"
  158. "\r\nputbackme";
  159. proc.update(hd);
  160. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1 200 OK\r\n"
  161. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  162. "Server: Apache/2.2.3 (Debian)\r\n"
  163. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  164. "ETag: \"594065-23e3-50825cc0\"\r\n"
  165. "Accept-Ranges: bytes\r\n"
  166. "Content-Length: 9187\r\n"
  167. "Connection: close\r\n"
  168. "Content-Type: text/html; charset=UTF-8"),
  169. proc.getHeaderString());
  170. }
  171. } // namespace aria2