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