HttpHeaderTest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "HttpHeader.h"
  2. #include <sstream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "Range.h"
  5. namespace aria2 {
  6. class HttpHeaderTest:public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(HttpHeaderTest);
  8. CPPUNIT_TEST(testGetRange);
  9. CPPUNIT_TEST(testGet);
  10. CPPUNIT_TEST(testClearField);
  11. CPPUNIT_TEST(testFill);
  12. CPPUNIT_TEST_SUITE_END();
  13. public:
  14. void testGetRange();
  15. void testGet();
  16. void testClearField();
  17. void testFill();
  18. };
  19. CPPUNIT_TEST_SUITE_REGISTRATION( HttpHeaderTest );
  20. void HttpHeaderTest::testGetRange()
  21. {
  22. {
  23. HttpHeader httpHeader;
  24. httpHeader.put("Content-Range",
  25. "9223372036854775800-9223372036854775801/9223372036854775807");
  26. SharedHandle<Range> range = httpHeader.getRange();
  27. CPPUNIT_ASSERT_EQUAL((off_t)9223372036854775800LL, range->getStartByte());
  28. CPPUNIT_ASSERT_EQUAL((off_t)9223372036854775801LL, range->getEndByte());
  29. CPPUNIT_ASSERT_EQUAL((uint64_t)9223372036854775807ULL, range->getEntityLength());
  30. }
  31. {
  32. HttpHeader httpHeader;
  33. httpHeader.put("Content-Range",
  34. "9223372036854775800-9223372036854775801/9223372036854775807");
  35. SharedHandle<Range> range = httpHeader.getRange();
  36. CPPUNIT_ASSERT_EQUAL((off_t)9223372036854775800LL, range->getStartByte());
  37. CPPUNIT_ASSERT_EQUAL((off_t)9223372036854775801LL, range->getEndByte());
  38. CPPUNIT_ASSERT_EQUAL((uint64_t)9223372036854775807ULL, range->getEntityLength());
  39. }
  40. {
  41. HttpHeader httpHeader;
  42. httpHeader.put("Content-Range", "bytes */1024");
  43. SharedHandle<Range> range = httpHeader.getRange();
  44. CPPUNIT_ASSERT_EQUAL((off_t)0, range->getStartByte());
  45. CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEndByte());
  46. CPPUNIT_ASSERT_EQUAL((uint64_t)0, range->getEntityLength());
  47. }
  48. {
  49. HttpHeader httpHeader;
  50. httpHeader.put("Content-Range", "bytes 0-9/*");
  51. SharedHandle<Range> range = httpHeader.getRange();
  52. CPPUNIT_ASSERT_EQUAL((off_t)0, range->getStartByte());
  53. CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEndByte());
  54. CPPUNIT_ASSERT_EQUAL((uint64_t)0, range->getEntityLength());
  55. }
  56. }
  57. void HttpHeaderTest::testGet()
  58. {
  59. HttpHeader h;
  60. h.put("A", "100");
  61. h.put("a", "101");
  62. h.put("B", "200");
  63. std::vector<std::string> r(h.get("A"));
  64. CPPUNIT_ASSERT_EQUAL((size_t)2, r.size());
  65. CPPUNIT_ASSERT_EQUAL(std::string("100"), r[0]);
  66. CPPUNIT_ASSERT_EQUAL(std::string("101"), r[1]);
  67. }
  68. void HttpHeaderTest::testClearField()
  69. {
  70. HttpHeader h;
  71. h.setStatusCode(200);
  72. h.setVersion(HttpHeader::HTTP_1_1);
  73. h.put("Foo", "Bar");
  74. CPPUNIT_ASSERT_EQUAL(std::string("Bar"), h.getFirst("Foo"));
  75. h.clearField();
  76. CPPUNIT_ASSERT_EQUAL(std::string(""), h.getFirst("Foo"));
  77. CPPUNIT_ASSERT_EQUAL(200, h.getStatusCode());
  78. CPPUNIT_ASSERT_EQUAL(std::string(HttpHeader::HTTP_1_1), h.getVersion());
  79. }
  80. void HttpHeaderTest::testFill()
  81. {
  82. std::stringstream ss;
  83. ss << "Host: aria2.sourceforge.net\r\n"
  84. << "Connection: close \r\n" // trailing white space
  85. << "Multi-Line: text1\r\n"
  86. << " text2\r\n"
  87. << " text3\r\n"
  88. << "Duplicate: foo\r\n"
  89. << "Duplicate: bar\r\n";
  90. HttpHeader h;
  91. h.fill(ss);
  92. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.net"),
  93. h.getFirst("Host"));
  94. CPPUNIT_ASSERT_EQUAL(std::string("close"),
  95. h.getFirst("Connection"));
  96. CPPUNIT_ASSERT_EQUAL(std::string("text1 text2 text3"),
  97. h.getFirst("Multi-Line"));
  98. CPPUNIT_ASSERT_EQUAL(std::string("foo"),
  99. h.get("Duplicate")[0]);
  100. CPPUNIT_ASSERT_EQUAL(std::string("bar"),
  101. h.get("Duplicate")[1]);
  102. }
  103. } // namespace aria2