HttpHeaderProcessorTest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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((size_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((size_t)9, proc.getPutBackDataLength());
  66. }
  67. void HttpHeaderProcessorTest::testGetPutBackDataLength_nullChar()
  68. {
  69. HttpHeaderProcessor proc;
  70. const char* x = "HTTP/1.1 200 OK\r\n"
  71. "foo: foo\0bar\r\n"
  72. "\r\nputbackme";
  73. std::string hd1(&x[0], &x[42]);
  74. proc.update(hd1);
  75. CPPUNIT_ASSERT(proc.eoh());
  76. CPPUNIT_ASSERT_EQUAL((size_t)9, proc.getPutBackDataLength());
  77. }
  78. void HttpHeaderProcessorTest::testGetHttpStatusHeader()
  79. {
  80. HttpHeaderProcessor proc;
  81. std::string hd = "HTTP/1.1 200 OK\r\n"
  82. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  83. "Server: Apache/2.2.3 (Debian)\r\n"
  84. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  85. "ETag: \"594065-23e3-50825cc0\"\r\n"
  86. "Accept-Ranges: bytes\r\n"
  87. "Content-Length: 9187\r\n"
  88. "Connection: close\r\n"
  89. "Content-Type: text/html; charset=UTF-8\r\n"
  90. "\r\n";
  91. proc.update(hd);
  92. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  93. std::string status = statusHeader.first;
  94. SharedHandle<HttpHeader> header = statusHeader.second;
  95. CPPUNIT_ASSERT_EQUAL(std::string("200"), status);
  96. CPPUNIT_ASSERT_EQUAL(std::string("Mon, 25 Jun 2007 16:04:59 GMT"), header->getFirst("Date"));
  97. CPPUNIT_ASSERT_EQUAL(std::string("Apache/2.2.3 (Debian)"), header->getFirst("Server"));
  98. CPPUNIT_ASSERT_EQUAL(9187ULL, header->getFirstAsULLInt("Content-Length"));
  99. CPPUNIT_ASSERT_EQUAL(std::string("text/html; charset=UTF-8"), header->getFirst("Content-Type"));
  100. }
  101. void HttpHeaderProcessorTest::testGetHttpStatusHeader_empty()
  102. {
  103. HttpHeaderProcessor proc;
  104. try {
  105. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  106. CPPUNIT_FAIL("Exception must be thrown.");
  107. } catch(DlRetryEx* ex) {
  108. std::cout << ex->getMsg() << std::endl;
  109. delete ex;
  110. }
  111. }
  112. void HttpHeaderProcessorTest::testGetHttpStatusHeader_statusOnly()
  113. {
  114. HttpHeaderProcessor proc;
  115. std::string hd = "HTTP/1.1 200\r\n\r\n";
  116. proc.update(hd);
  117. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  118. CPPUNIT_ASSERT_EQUAL(std::string("200"), statusHeader.first);
  119. CPPUNIT_ASSERT(!statusHeader.second.isNull());
  120. }
  121. void HttpHeaderProcessorTest::testGetHttpStatusHeader_insufficientStatusLength()
  122. {
  123. HttpHeaderProcessor proc;
  124. std::string hd = "HTTP/1.1 20\r\n\r\n";
  125. proc.update(hd);
  126. try {
  127. std::pair<std::string, SharedHandle<HttpHeader> > statusHeader = proc.getHttpStatusHeader();
  128. CPPUNIT_FAIL("Exception must be thrown.");
  129. } catch(DlRetryEx* ex) {
  130. std::cout << ex->getMsg() << std::endl;
  131. delete ex;
  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->getMsg() << std::endl;
  146. delete ex;
  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. } // namespace aria2