HttpHeaderTest.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "HttpHeader.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "Range.h"
  4. #include "DlAbortEx.h"
  5. namespace aria2 {
  6. class HttpHeaderTest : public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(HttpHeaderTest);
  8. CPPUNIT_TEST(testGetRange);
  9. CPPUNIT_TEST(testFindAll);
  10. CPPUNIT_TEST(testClearField);
  11. CPPUNIT_TEST(testFieldContains);
  12. CPPUNIT_TEST(testRemove);
  13. CPPUNIT_TEST_SUITE_END();
  14. public:
  15. void testGetRange();
  16. void testFindAll();
  17. void testClearField();
  18. void testFieldContains();
  19. void testRemove();
  20. };
  21. CPPUNIT_TEST_SUITE_REGISTRATION(HttpHeaderTest);
  22. void HttpHeaderTest::testGetRange()
  23. {
  24. {
  25. HttpHeader httpHeader;
  26. httpHeader.put(
  27. HttpHeader::CONTENT_RANGE,
  28. "9223372036854775800-9223372036854775801/9223372036854775807");
  29. Range range = httpHeader.getRange();
  30. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775800LL, range.startByte);
  31. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775801LL, range.endByte);
  32. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL, range.entityLength);
  33. }
  34. {
  35. HttpHeader httpHeader;
  36. httpHeader.put(
  37. HttpHeader::CONTENT_RANGE,
  38. "9223372036854775800-9223372036854775801/9223372036854775807");
  39. Range range = httpHeader.getRange();
  40. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775800LL, range.startByte);
  41. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775801LL, range.endByte);
  42. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL, range.entityLength);
  43. }
  44. {
  45. HttpHeader httpHeader;
  46. httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes */1024");
  47. Range range = httpHeader.getRange();
  48. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte);
  49. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
  50. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
  51. }
  52. {
  53. HttpHeader httpHeader;
  54. httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes 0-9/*");
  55. Range range = httpHeader.getRange();
  56. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte);
  57. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
  58. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
  59. }
  60. {
  61. HttpHeader httpHeader;
  62. httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes */*");
  63. Range range = httpHeader.getRange();
  64. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte);
  65. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
  66. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
  67. }
  68. {
  69. HttpHeader httpHeader;
  70. httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes 0");
  71. Range range = httpHeader.getRange();
  72. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte);
  73. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
  74. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
  75. }
  76. {
  77. HttpHeader httpHeader;
  78. httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes 0/");
  79. Range range = httpHeader.getRange();
  80. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte);
  81. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
  82. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
  83. }
  84. {
  85. // Support for non-compliant server
  86. HttpHeader httpHeader;
  87. httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes=0-1023/1024");
  88. Range range = httpHeader.getRange();
  89. CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte);
  90. CPPUNIT_ASSERT_EQUAL((int64_t)1023, range.endByte);
  91. CPPUNIT_ASSERT_EQUAL((int64_t)1024, range.entityLength);
  92. }
  93. {
  94. HttpHeader httpHeader;
  95. httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes 0-/3");
  96. try {
  97. httpHeader.getRange();
  98. CPPUNIT_FAIL("Exception must be thrown");
  99. }
  100. catch (const DlAbortEx& e) {
  101. // success
  102. }
  103. }
  104. {
  105. HttpHeader httpHeader;
  106. httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes -0/3");
  107. try {
  108. httpHeader.getRange();
  109. CPPUNIT_FAIL("Exception must be thrown");
  110. }
  111. catch (const DlAbortEx& e) {
  112. // success
  113. }
  114. }
  115. }
  116. void HttpHeaderTest::testFindAll()
  117. {
  118. HttpHeader h;
  119. h.put(HttpHeader::LINK, "100");
  120. h.put(HttpHeader::LINK, "101");
  121. h.put(HttpHeader::CONNECTION, "200");
  122. std::vector<std::string> r(h.findAll(HttpHeader::LINK));
  123. CPPUNIT_ASSERT_EQUAL((size_t)2, r.size());
  124. CPPUNIT_ASSERT_EQUAL(std::string("100"), r[0]);
  125. CPPUNIT_ASSERT_EQUAL(std::string("101"), r[1]);
  126. }
  127. void HttpHeaderTest::testClearField()
  128. {
  129. HttpHeader h;
  130. h.setStatusCode(200);
  131. h.setVersion("HTTP/1.1");
  132. h.put(HttpHeader::LINK, "Bar");
  133. CPPUNIT_ASSERT_EQUAL(std::string("Bar"), h.find(HttpHeader::LINK));
  134. h.clearField();
  135. CPPUNIT_ASSERT_EQUAL(std::string(""), h.find(HttpHeader::LINK));
  136. CPPUNIT_ASSERT_EQUAL(200, h.getStatusCode());
  137. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), h.getVersion());
  138. }
  139. void HttpHeaderTest::testFieldContains()
  140. {
  141. HttpHeader h;
  142. h.put(HttpHeader::CONNECTION, "Keep-Alive, Upgrade");
  143. h.put(HttpHeader::UPGRADE, "WebSocket");
  144. h.put(HttpHeader::SEC_WEBSOCKET_VERSION, "13");
  145. h.put(HttpHeader::SEC_WEBSOCKET_VERSION, "8, 7");
  146. CPPUNIT_ASSERT(h.fieldContains(HttpHeader::CONNECTION, "upgrade"));
  147. CPPUNIT_ASSERT(h.fieldContains(HttpHeader::CONNECTION, "keep-alive"));
  148. CPPUNIT_ASSERT(!h.fieldContains(HttpHeader::CONNECTION, "close"));
  149. CPPUNIT_ASSERT(h.fieldContains(HttpHeader::UPGRADE, "websocket"));
  150. CPPUNIT_ASSERT(!h.fieldContains(HttpHeader::UPGRADE, "spdy"));
  151. CPPUNIT_ASSERT(h.fieldContains(HttpHeader::SEC_WEBSOCKET_VERSION, "13"));
  152. CPPUNIT_ASSERT(h.fieldContains(HttpHeader::SEC_WEBSOCKET_VERSION, "8"));
  153. CPPUNIT_ASSERT(!h.fieldContains(HttpHeader::SEC_WEBSOCKET_VERSION, "6"));
  154. }
  155. void HttpHeaderTest::testRemove()
  156. {
  157. HttpHeader h;
  158. h.put(HttpHeader::CONNECTION, "close");
  159. h.put(HttpHeader::TRANSFER_ENCODING, "chunked");
  160. h.put(HttpHeader::TRANSFER_ENCODING, "gzip");
  161. h.remove(HttpHeader::TRANSFER_ENCODING);
  162. CPPUNIT_ASSERT(!h.defined(HttpHeader::TRANSFER_ENCODING));
  163. CPPUNIT_ASSERT(h.defined(HttpHeader::CONNECTION));
  164. }
  165. } // namespace aria2