HttpHeaderProcessorTest.cc 5.5 KB

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