aria2/test/HttpHeaderTest.cc

179 lines
4.9 KiB
C++
Raw Normal View History

2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To handle Segment as SegmentHandle: * src/AbstractCommand.cc (execute): Rewritten. * src/SegmentMan.h: Segment -> SegmentHandle Introducded HttpResponse class, HttpRequest class to improve code extensiveness and make it clear: * src/HttpDownloadCommand.cc: transfer encoders are now managed by HttpResponse class. * src/HttpRequest.h, src/HttpRequest.cc: New class. * src/HttpResponse.h, src/HttpResponse.cc: New class. * src/HttpConnection.cc: Contruction of http request were moved to HttpRequest class. * src/HttpResponseCommand.h, src/HttpResponseCommand.cc: Refactored. * src/HttpRequestCommand.cc (executeInternal): Rewritten. * src/HttpAuthConfig.h: New class. * src/Range.h: New class. To make FtpTunnel{Request, Response}Command and HttpProxy{Request, Response}Command derived from AbstractProxy{Request, Response}Command: * src/FtpTunnelResponseCommand.h, src/FtpTunnelResponseCommand.cc: Derived from AbstractProxyRequestCommand class. * src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc: Derived from AbstractProxyResponseCommand class. * src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc: Derived from AbstractProxyRequestCommand class. * src/HttpProxyResponseCommand.h, src/HttpProxyResponseCommand.cc: Derived from AbstractProxyResponseCommand class. * src/AbstractProxyRequestCommand.h, src/AbstractProxyRequestCommand.cc : New class. * src/AbstractProxyResponseCommand.h, src/AbstractProxyResponseCommand.cc: New class. To add netrc support: * src/Netrc.h, src/Netrc.cc: New class. * src/Util.h, src/Util.cc (split): New function. * src/HttpHeader.cc (getRange): Fixed so that it inspects "Content-Range" header instead of "Range" header. * src/HttpHeader.h (getStatus): Removed. (setStatus): Removed. * src/Segment.h (getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
#include "HttpHeader.h"
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To handle Segment as SegmentHandle: * src/AbstractCommand.cc (execute): Rewritten. * src/SegmentMan.h: Segment -> SegmentHandle Introducded HttpResponse class, HttpRequest class to improve code extensiveness and make it clear: * src/HttpDownloadCommand.cc: transfer encoders are now managed by HttpResponse class. * src/HttpRequest.h, src/HttpRequest.cc: New class. * src/HttpResponse.h, src/HttpResponse.cc: New class. * src/HttpConnection.cc: Contruction of http request were moved to HttpRequest class. * src/HttpResponseCommand.h, src/HttpResponseCommand.cc: Refactored. * src/HttpRequestCommand.cc (executeInternal): Rewritten. * src/HttpAuthConfig.h: New class. * src/Range.h: New class. To make FtpTunnel{Request, Response}Command and HttpProxy{Request, Response}Command derived from AbstractProxy{Request, Response}Command: * src/FtpTunnelResponseCommand.h, src/FtpTunnelResponseCommand.cc: Derived from AbstractProxyRequestCommand class. * src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc: Derived from AbstractProxyResponseCommand class. * src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc: Derived from AbstractProxyRequestCommand class. * src/HttpProxyResponseCommand.h, src/HttpProxyResponseCommand.cc: Derived from AbstractProxyResponseCommand class. * src/AbstractProxyRequestCommand.h, src/AbstractProxyRequestCommand.cc : New class. * src/AbstractProxyResponseCommand.h, src/AbstractProxyResponseCommand.cc: New class. To add netrc support: * src/Netrc.h, src/Netrc.cc: New class. * src/Util.h, src/Util.cc (split): New function. * src/HttpHeader.cc (getRange): Fixed so that it inspects "Content-Range" header instead of "Range" header. * src/HttpHeader.h (getStatus): Removed. (setStatus): Removed. * src/Segment.h (getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
#include <cppunit/extensions/HelperMacros.h>
#include "Range.h"
2011-11-02 15:31:27 +00:00
#include "DlAbortEx.h"
namespace aria2 {
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To handle Segment as SegmentHandle: * src/AbstractCommand.cc (execute): Rewritten. * src/SegmentMan.h: Segment -> SegmentHandle Introducded HttpResponse class, HttpRequest class to improve code extensiveness and make it clear: * src/HttpDownloadCommand.cc: transfer encoders are now managed by HttpResponse class. * src/HttpRequest.h, src/HttpRequest.cc: New class. * src/HttpResponse.h, src/HttpResponse.cc: New class. * src/HttpConnection.cc: Contruction of http request were moved to HttpRequest class. * src/HttpResponseCommand.h, src/HttpResponseCommand.cc: Refactored. * src/HttpRequestCommand.cc (executeInternal): Rewritten. * src/HttpAuthConfig.h: New class. * src/Range.h: New class. To make FtpTunnel{Request, Response}Command and HttpProxy{Request, Response}Command derived from AbstractProxy{Request, Response}Command: * src/FtpTunnelResponseCommand.h, src/FtpTunnelResponseCommand.cc: Derived from AbstractProxyRequestCommand class. * src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc: Derived from AbstractProxyResponseCommand class. * src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc: Derived from AbstractProxyRequestCommand class. * src/HttpProxyResponseCommand.h, src/HttpProxyResponseCommand.cc: Derived from AbstractProxyResponseCommand class. * src/AbstractProxyRequestCommand.h, src/AbstractProxyRequestCommand.cc : New class. * src/AbstractProxyResponseCommand.h, src/AbstractProxyResponseCommand.cc: New class. To add netrc support: * src/Netrc.h, src/Netrc.cc: New class. * src/Util.h, src/Util.cc (split): New function. * src/HttpHeader.cc (getRange): Fixed so that it inspects "Content-Range" header instead of "Range" header. * src/HttpHeader.h (getStatus): Removed. (setStatus): Removed. * src/Segment.h (getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
class HttpHeaderTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(HttpHeaderTest);
CPPUNIT_TEST(testGetRange);
CPPUNIT_TEST(testFindAll);
CPPUNIT_TEST(testClearField);
CPPUNIT_TEST(testFill);
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To handle Segment as SegmentHandle: * src/AbstractCommand.cc (execute): Rewritten. * src/SegmentMan.h: Segment -> SegmentHandle Introducded HttpResponse class, HttpRequest class to improve code extensiveness and make it clear: * src/HttpDownloadCommand.cc: transfer encoders are now managed by HttpResponse class. * src/HttpRequest.h, src/HttpRequest.cc: New class. * src/HttpResponse.h, src/HttpResponse.cc: New class. * src/HttpConnection.cc: Contruction of http request were moved to HttpRequest class. * src/HttpResponseCommand.h, src/HttpResponseCommand.cc: Refactored. * src/HttpRequestCommand.cc (executeInternal): Rewritten. * src/HttpAuthConfig.h: New class. * src/Range.h: New class. To make FtpTunnel{Request, Response}Command and HttpProxy{Request, Response}Command derived from AbstractProxy{Request, Response}Command: * src/FtpTunnelResponseCommand.h, src/FtpTunnelResponseCommand.cc: Derived from AbstractProxyRequestCommand class. * src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc: Derived from AbstractProxyResponseCommand class. * src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc: Derived from AbstractProxyRequestCommand class. * src/HttpProxyResponseCommand.h, src/HttpProxyResponseCommand.cc: Derived from AbstractProxyResponseCommand class. * src/AbstractProxyRequestCommand.h, src/AbstractProxyRequestCommand.cc : New class. * src/AbstractProxyResponseCommand.h, src/AbstractProxyResponseCommand.cc: New class. To add netrc support: * src/Netrc.h, src/Netrc.cc: New class. * src/Util.h, src/Util.cc (split): New function. * src/HttpHeader.cc (getRange): Fixed so that it inspects "Content-Range" header instead of "Range" header. * src/HttpHeader.h (getStatus): Removed. (setStatus): Removed. * src/Segment.h (getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
CPPUNIT_TEST_SUITE_END();
public:
void testGetRange();
void testFindAll();
void testClearField();
void testFill();
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To handle Segment as SegmentHandle: * src/AbstractCommand.cc (execute): Rewritten. * src/SegmentMan.h: Segment -> SegmentHandle Introducded HttpResponse class, HttpRequest class to improve code extensiveness and make it clear: * src/HttpDownloadCommand.cc: transfer encoders are now managed by HttpResponse class. * src/HttpRequest.h, src/HttpRequest.cc: New class. * src/HttpResponse.h, src/HttpResponse.cc: New class. * src/HttpConnection.cc: Contruction of http request were moved to HttpRequest class. * src/HttpResponseCommand.h, src/HttpResponseCommand.cc: Refactored. * src/HttpRequestCommand.cc (executeInternal): Rewritten. * src/HttpAuthConfig.h: New class. * src/Range.h: New class. To make FtpTunnel{Request, Response}Command and HttpProxy{Request, Response}Command derived from AbstractProxy{Request, Response}Command: * src/FtpTunnelResponseCommand.h, src/FtpTunnelResponseCommand.cc: Derived from AbstractProxyRequestCommand class. * src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc: Derived from AbstractProxyResponseCommand class. * src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc: Derived from AbstractProxyRequestCommand class. * src/HttpProxyResponseCommand.h, src/HttpProxyResponseCommand.cc: Derived from AbstractProxyResponseCommand class. * src/AbstractProxyRequestCommand.h, src/AbstractProxyRequestCommand.cc : New class. * src/AbstractProxyResponseCommand.h, src/AbstractProxyResponseCommand.cc: New class. To add netrc support: * src/Netrc.h, src/Netrc.cc: New class. * src/Util.h, src/Util.cc (split): New function. * src/HttpHeader.cc (getRange): Fixed so that it inspects "Content-Range" header instead of "Range" header. * src/HttpHeader.h (getStatus): Removed. (setStatus): Removed. * src/Segment.h (getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
};
CPPUNIT_TEST_SUITE_REGISTRATION( HttpHeaderTest );
void HttpHeaderTest::testGetRange()
{
{
HttpHeader httpHeader;
httpHeader.put("content-range",
"9223372036854775800-9223372036854775801/9223372036854775807");
SharedHandle<Range> range = httpHeader.getRange();
CPPUNIT_ASSERT_EQUAL((off_t)9223372036854775800LL, range->getStartByte());
CPPUNIT_ASSERT_EQUAL((off_t)9223372036854775801LL, range->getEndByte());
CPPUNIT_ASSERT_EQUAL((off_t)9223372036854775807LL, range->getEntityLength());
}
{
HttpHeader httpHeader;
httpHeader.put("content-range",
"9223372036854775800-9223372036854775801/9223372036854775807");
SharedHandle<Range> range = httpHeader.getRange();
CPPUNIT_ASSERT_EQUAL((off_t)9223372036854775800LL, range->getStartByte());
CPPUNIT_ASSERT_EQUAL((off_t)9223372036854775801LL, range->getEndByte());
CPPUNIT_ASSERT_EQUAL((off_t)9223372036854775807LL, range->getEntityLength());
}
{
HttpHeader httpHeader;
httpHeader.put("content-range", "bytes */1024");
SharedHandle<Range> range = httpHeader.getRange();
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getStartByte());
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEndByte());
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEntityLength());
}
{
HttpHeader httpHeader;
httpHeader.put("content-range", "bytes 0-9/*");
SharedHandle<Range> range = httpHeader.getRange();
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getStartByte());
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEndByte());
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEntityLength());
}
2011-11-02 15:31:27 +00:00
{
HttpHeader httpHeader;
httpHeader.put("content-range", "bytes */*");
2011-11-02 15:31:27 +00:00
SharedHandle<Range> range = httpHeader.getRange();
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getStartByte());
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEndByte());
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEntityLength());
2011-11-02 15:31:27 +00:00
}
{
HttpHeader httpHeader;
httpHeader.put("content-range", "bytes 0");
2011-11-02 15:31:27 +00:00
SharedHandle<Range> range = httpHeader.getRange();
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getStartByte());
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEndByte());
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEntityLength());
2011-11-02 15:31:27 +00:00
}
{
HttpHeader httpHeader;
httpHeader.put("content-range", "bytes 0/");
2011-11-02 15:31:27 +00:00
SharedHandle<Range> range = httpHeader.getRange();
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getStartByte());
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEndByte());
CPPUNIT_ASSERT_EQUAL((off_t)0, range->getEntityLength());
2011-11-02 15:31:27 +00:00
}
{
HttpHeader httpHeader;
httpHeader.put("content-range", "bytes 0-/3");
2011-11-02 15:31:27 +00:00
try {
httpHeader.getRange();
CPPUNIT_FAIL("Exception must be thrown");
} catch(const DlAbortEx& e) {
// success
}
}
{
HttpHeader httpHeader;
httpHeader.put("content-range", "bytes -0/3");
2011-11-02 15:31:27 +00:00
try {
httpHeader.getRange();
CPPUNIT_FAIL("Exception must be thrown");
} catch(const DlAbortEx& e) {
// success
}
}
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To handle Segment as SegmentHandle: * src/AbstractCommand.cc (execute): Rewritten. * src/SegmentMan.h: Segment -> SegmentHandle Introducded HttpResponse class, HttpRequest class to improve code extensiveness and make it clear: * src/HttpDownloadCommand.cc: transfer encoders are now managed by HttpResponse class. * src/HttpRequest.h, src/HttpRequest.cc: New class. * src/HttpResponse.h, src/HttpResponse.cc: New class. * src/HttpConnection.cc: Contruction of http request were moved to HttpRequest class. * src/HttpResponseCommand.h, src/HttpResponseCommand.cc: Refactored. * src/HttpRequestCommand.cc (executeInternal): Rewritten. * src/HttpAuthConfig.h: New class. * src/Range.h: New class. To make FtpTunnel{Request, Response}Command and HttpProxy{Request, Response}Command derived from AbstractProxy{Request, Response}Command: * src/FtpTunnelResponseCommand.h, src/FtpTunnelResponseCommand.cc: Derived from AbstractProxyRequestCommand class. * src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc: Derived from AbstractProxyResponseCommand class. * src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc: Derived from AbstractProxyRequestCommand class. * src/HttpProxyResponseCommand.h, src/HttpProxyResponseCommand.cc: Derived from AbstractProxyResponseCommand class. * src/AbstractProxyRequestCommand.h, src/AbstractProxyRequestCommand.cc : New class. * src/AbstractProxyResponseCommand.h, src/AbstractProxyResponseCommand.cc: New class. To add netrc support: * src/Netrc.h, src/Netrc.cc: New class. * src/Util.h, src/Util.cc (split): New function. * src/HttpHeader.cc (getRange): Fixed so that it inspects "Content-Range" header instead of "Range" header. * src/HttpHeader.h (getStatus): Removed. (setStatus): Removed. * src/Segment.h (getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
}
void HttpHeaderTest::testFindAll()
{
HttpHeader h;
h.put("A", "100");
h.put("A", "101");
h.put("B", "200");
std::vector<std::string> r(h.findAll("A"));
CPPUNIT_ASSERT_EQUAL((size_t)2, r.size());
CPPUNIT_ASSERT_EQUAL(std::string("100"), r[0]);
CPPUNIT_ASSERT_EQUAL(std::string("101"), r[1]);
}
void HttpHeaderTest::testClearField()
{
HttpHeader h;
h.setStatusCode(200);
h.setVersion(HttpHeader::HTTP_1_1);
h.put("Foo", "Bar");
CPPUNIT_ASSERT_EQUAL(std::string("Bar"), h.find("Foo"));
h.clearField();
CPPUNIT_ASSERT_EQUAL(std::string(""), h.find("Foo"));
CPPUNIT_ASSERT_EQUAL(200, h.getStatusCode());
CPPUNIT_ASSERT_EQUAL(std::string(HttpHeader::HTTP_1_1), h.getVersion());
}
void HttpHeaderTest::testFill()
{
2011-11-02 15:31:27 +00:00
std::string s =
"Host: aria2.sourceforge.net\r\n"
"Connection: close \r\n" // trailing white space
"Multi-Line: text1\r\n"
" text2\r\n"
" text3\r\n"
"Duplicate: foo\r\n"
"Duplicate: bar\r\n";
HttpHeader h;
2011-11-02 15:31:27 +00:00
h.fill(s.begin(), s.end());
CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.net"),
h.find("host"));
CPPUNIT_ASSERT_EQUAL(std::string("close"),
h.find("connection"));
CPPUNIT_ASSERT_EQUAL(std::string("text1 text2 text3"),
h.find("multi-line"));
CPPUNIT_ASSERT_EQUAL(std::string("foo"),
h.findAll("duplicate")[0]);
CPPUNIT_ASSERT_EQUAL(std::string("bar"),
h.findAll("duplicate")[1]);
}
} // namespace aria2