123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710 |
- #include "HttpRequest.h"
- #include "prefs.h"
- #include "AuthConfigFactory.h"
- #include "PiecedSegment.h"
- #include "Piece.h"
- #include "Range.h"
- #include "Request.h"
- #include "CookieBox.h"
- #include "Option.h"
- #include "array_fun.h"
- #include <cppunit/extensions/HelperMacros.h>
- namespace aria2 {
- class HttpRequestTest : public CppUnit::TestFixture {
- CPPUNIT_TEST_SUITE(HttpRequestTest);
- CPPUNIT_TEST(testGetStartByte);
- CPPUNIT_TEST(testGetEndByte);
- CPPUNIT_TEST(testCreateRequest);
- CPPUNIT_TEST(testCreateRequest_ftp);
- CPPUNIT_TEST(testCreateRequest_with_cookie);
- CPPUNIT_TEST(testCreateRequest_query);
- CPPUNIT_TEST(testCreateProxyRequest);
- CPPUNIT_TEST(testIsRangeSatisfied);
- CPPUNIT_TEST(testUserAgent);
- CPPUNIT_TEST(testAddHeader);
- CPPUNIT_TEST(testAddAcceptType);
- CPPUNIT_TEST(testEnableAcceptEncoding);
- CPPUNIT_TEST_SUITE_END();
- private:
- public:
- void setUp() {}
- void testGetStartByte();
- void testGetEndByte();
- void testCreateRequest();
- void testCreateRequest_ftp();
- void testCreateRequest_with_cookie();
- void testCreateRequest_query();
- void testCreateProxyRequest();
- void testIsRangeSatisfied();
- void testUserAgent();
- void testAddHeader();
- void testAddAcceptType();
- void testEnableAcceptEncoding();
- };
- CPPUNIT_TEST_SUITE_REGISTRATION( HttpRequestTest );
- void HttpRequestTest::testGetStartByte()
- {
- HttpRequest httpRequest;
- SharedHandle<Piece> p(new Piece(1, 1024));
- SharedHandle<Segment> segment(new PiecedSegment(1024, p));
- CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getStartByte());
- httpRequest.setSegment(segment);
-
- CPPUNIT_ASSERT_EQUAL((off_t)1024LL, httpRequest.getStartByte());
- }
- void HttpRequestTest::testGetEndByte()
- {
- size_t index = 1;
- size_t length = 1024*1024-1024;
- size_t segmentLength = 1024*1024;
- HttpRequest httpRequest;
- SharedHandle<Piece> piece(new Piece(index, length));
- SharedHandle<Segment> segment(new PiecedSegment(segmentLength, piece));
- CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getEndByte());
- httpRequest.setSegment(segment);
- CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getEndByte());
- SharedHandle<Request> request(new Request());
- request->supportsPersistentConnection(true);
- request->setPipeliningHint(true);
- httpRequest.setRequest(request);
- CPPUNIT_ASSERT_EQUAL((off_t)(segmentLength*index+length-1),
- httpRequest.getEndByte());
- request->setPipeliningHint(false);
- CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getEndByte());
- }
- void HttpRequestTest::testCreateRequest()
- {
- SharedHandle<Piece> p;
- Option option;
- option.put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
- option.put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
- option.put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
- option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
- option.put(PREF_HTTP_USER, "aria2user");
- option.put(PREF_HTTP_PASSWD, "aria2passwd");
- option.put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
- option.put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
- SharedHandle<AuthConfigFactory> authConfigFactory(new AuthConfigFactory(&option));
- SingletonHolder<SharedHandle<AuthConfigFactory> >::instance(authConfigFactory);
- SharedHandle<Request> request(new Request());
- request->supportsPersistentConnection(true);
- request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
- p.reset(new Piece(0, 1024));
- SharedHandle<Segment> segment(new PiecedSegment(1024, p));
- HttpRequest httpRequest;
- httpRequest.disableContentEncoding();
- httpRequest.setRequest(request);
- httpRequest.setSegment(segment);
- // remove "Connection: close" and add end byte range
- request->setPipeliningHint(true);
-
- std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Range: bytes=0-1023\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- request->setPipeliningHint(false);
- expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- p.reset(new Piece(1, 1024*1024));
- segment.reset(new PiecedSegment(1024*1024, p));
- httpRequest.setSegment(segment);
- expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "Range: bytes=1048576-\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- request->setPipeliningHint(true);
- expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Range: bytes=1048576-2097151\r\n"
- "\r\n";
-
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- // redirection set persistent connection flag to true
- request->redirectUrl("http://localhost:8080/archives/download/aria2-1.0.0.tar.bz2");
- expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Range: bytes=1048576-2097151\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- request->supportsPersistentConnection(true);
- request->setPipeliningHint(false);
- // this only removes "Connection: close".
- request->setKeepAliveHint(true);
- expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Range: bytes=1048576-\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- request->setKeepAliveHint(false);
- request->resetUrl();
- p.reset(new Piece(0, 1024*1024));
- segment.reset(new PiecedSegment(1024*1024, p));
- httpRequest.setSegment(segment);
- // enable http auth
- option.put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
-
- httpRequest.configure(&option);
- expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- // enable http proxy auth
- option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
- httpRequest.configure(&option);
- expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- option.put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
- httpRequest.configure(&option);
- expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- option.put(PREF_HTTP_PROXY_METHOD, V_GET);
- httpRequest.configure(&option);
- expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "Proxy-Connection: close\r\n"
- "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
- "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- request->setPipeliningHint(true);
- expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Range: bytes=0-1048575\r\n"
- "Proxy-Connection: Keep-Alive\r\n"
- "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
- "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- request->setPipeliningHint(false);
- option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
- httpRequest.configure(&option);
- expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "Proxy-Connection: close\r\n"
- "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- }
- void HttpRequestTest::testCreateRequest_ftp()
- {
- Option option;
- option.put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
- option.put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
- option.put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
- option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
- option.put(PREF_HTTP_USER, "aria2user");
- option.put(PREF_HTTP_PASSWD, "aria2passwd");
- option.put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
- option.put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
- SharedHandle<AuthConfigFactory> authConfigFactory
- (new AuthConfigFactory(&option));
- SingletonHolder<SharedHandle<AuthConfigFactory> >::instance(authConfigFactory);
- SharedHandle<Request> request(new Request());
- request->setUrl("ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2");
- HttpRequest httpRequest;
- SharedHandle<Piece> p(new Piece(0, 1024*1024));
- SharedHandle<Segment> segment
- (new PiecedSegment(1024*1024, p));
- httpRequest.disableContentEncoding();
- httpRequest.setRequest(request);
- httpRequest.setSegment(segment);
- httpRequest.configure(&option);
- std::string expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- // How to enable HTTP proxy authorization in FTP download via HTTP proxy
- option.put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
- option.put(PREF_HTTP_PROXY_METHOD, V_GET);
- option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
- httpRequest.configure(&option);
- expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "Proxy-Connection: close\r\n"
- "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
-
- }
- void HttpRequestTest::testCreateRequest_with_cookie()
- {
- SharedHandle<Request> request(new Request());
- request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
- SharedHandle<Piece> p(new Piece(0, 1024*1024));
- SharedHandle<Segment> segment
- (new PiecedSegment(1024*1024, p));
- Cookie cookie1("name1", "value1", "/archives", "localhost", false);
- Cookie cookie2("name2", "value2", "/archives/download", "localhost", false);
- Cookie cookie3("name3", "value3", "/archives/download", "tt.localhost", false);
- Cookie cookie4("name4", "value4", "/archives/download", "tt.localhost", true);
- request->cookieBox->add(cookie1);
- request->cookieBox->add(cookie2);
- request->cookieBox->add(cookie3);
- request->cookieBox->add(cookie4);
- HttpRequest httpRequest;
- httpRequest.disableContentEncoding();
- httpRequest.setRequest(request);
- httpRequest.setSegment(segment);
- std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "Cookie: name1=value1;\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- request->setUrl("http://localhost/archives/download/aria2-1.0.0.tar.bz2");
- expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "Cookie: name1=value1;name2=value2;\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- request->setUrl("http://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
- expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: tt.localhost\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "Cookie: name1=value1;name2=value2;name3=value3;\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- request->setUrl("https://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
- expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: tt.localhost\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "Cookie: name1=value1;name2=value2;name3=value3;name4=value4;\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
-
- }
- void HttpRequestTest::testCreateRequest_query()
- {
- SharedHandle<Request> request(new Request());
- request->setUrl("http://localhost/wiki?id=9ad5109a-b8a5-4edf-9373-56a1c34ae138");
- HttpRequest httpRequest;
- httpRequest.disableContentEncoding();
- httpRequest.setRequest(request);
- std::string expectedText =
- "GET /wiki?id=9ad5109a-b8a5-4edf-9373-56a1c34ae138 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "\r\n";
-
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- }
- void HttpRequestTest::testCreateProxyRequest()
- {
- SharedHandle<Request> request(new Request());
- request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
- SharedHandle<Piece> p(new Piece(0, 1024*1024));
- SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p));
- HttpRequest httpRequest;
- httpRequest.setRequest(request);
- httpRequest.setSegment(segment);
- request->supportsPersistentConnection(true);
- std::string expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Host: localhost:80\r\n"
- "Proxy-Connection: close\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
- // adds Keep-Alive header.
- request->setKeepAliveHint(true);
- expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Host: localhost:80\r\n"
- "Proxy-Connection: Keep-Alive\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
- request->setKeepAliveHint(false);
- // pipelining also adds Keep-Alive header.
- request->setPipeliningHint(true);
- expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Host: localhost:80\r\n"
- "Proxy-Connection: Keep-Alive\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
- }
- void HttpRequestTest::testIsRangeSatisfied()
- {
- SharedHandle<Request> request(new Request());
- request->supportsPersistentConnection(true);
- request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
- request->setPipeliningHint(false); // default: false
- SharedHandle<Piece> p(new Piece(0, 1024*1024));
- SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p));
- HttpRequest httpRequest;
- httpRequest.setRequest(request);
- httpRequest.setSegment(segment);
- SharedHandle<Range> range(new Range());
- CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
- p.reset(new Piece(1, 1024*1024));
- segment.reset(new PiecedSegment(1024*1024, p));
- httpRequest.setSegment(segment);
- CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
- uint64_t entityLength = segment->getSegmentLength()*10;
- range.reset(new Range(segment->getPosition(), 0, entityLength));
- CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
- httpRequest.setEntityLength(entityLength-1);
- CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
- httpRequest.setEntityLength(entityLength);
- CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
- request->setPipeliningHint(true);
- CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
- range.reset(new Range(segment->getPosition(),
- segment->getPosition()+segment->getLength()-1,
- entityLength));
- CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
- range.reset(new Range(0, segment->getPosition()+segment->getLength()-1,
- entityLength));
- CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
- }
- void HttpRequestTest::testUserAgent()
- {
- Option option;
- SharedHandle<AuthConfigFactory> authConfigFactory
- (new AuthConfigFactory(&option));
- SingletonHolder<SharedHandle<AuthConfigFactory> >::instance(authConfigFactory);
- SharedHandle<Request> request(new Request());
- request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
- SharedHandle<Piece> p(new Piece(0, 1024));
- SharedHandle<Segment> segment(new PiecedSegment(1024, p));
- HttpRequest httpRequest;
- httpRequest.disableContentEncoding();
- httpRequest.setRequest(request);
- httpRequest.setSegment(segment);
- httpRequest.setUserAgent("aria2 (Linux)");
- std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2 (Linux)\r\n"
- "Accept: */*\r\n"
- "Host: localhost:8080\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- std::string expectedTextForProxy = "CONNECT localhost:8080 HTTP/1.1\r\n"
- "User-Agent: aria2 (Linux)\r\n"
- "Host: localhost:8080\r\n"
- "Proxy-Connection: close\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedTextForProxy, httpRequest.createProxyRequest());
- }
- void HttpRequestTest::testAddHeader()
- {
- SharedHandle<Request> request(new Request());
- request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
- HttpRequest httpRequest;
- httpRequest.disableContentEncoding();
- httpRequest.setRequest(request);
- httpRequest.addHeader("X-ARIA2: v0.13\nX-ARIA2-DISTRIBUTE: enabled\n");
- std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n"
- "Host: localhost\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "X-ARIA2: v0.13\r\n"
- "X-ARIA2-DISTRIBUTE: enabled\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- }
- void HttpRequestTest::testAddAcceptType()
- {
- std::string acceptTypes[] = { "cream/custard",
- "muffin/chocolate" };
- SharedHandle<Request> request(new Request());
- request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
- HttpRequest httpRequest;
- httpRequest.disableContentEncoding();
- httpRequest.setRequest(request);
- httpRequest.addAcceptType(&acceptTypes[0], &acceptTypes[arrayLength(acceptTypes)]);
- std::string expectedText =
- "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*,cream/custard,muffin/chocolate\r\n"
- "Host: localhost\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- }
- void HttpRequestTest::testEnableAcceptEncoding()
- {
- SharedHandle<Request> request(new Request());
- request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
- HttpRequest httpRequest;
- httpRequest.setRequest(request);
- std::string acceptEncodings;
- #ifdef HAVE_LIBZ
- acceptEncodings += "deflate, gzip";
- #endif // HAVE_LIBZ
-
- std::string expectedText =
- "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
- "User-Agent: aria2\r\n"
- "Accept: */*\r\n";
- if(!acceptEncodings.empty()) {
- expectedText += "Accept-Encoding: "+acceptEncodings+"\r\n";
- }
- expectedText +=
- "Host: localhost\r\n"
- "Pragma: no-cache\r\n"
- "Cache-Control: no-cache\r\n"
- "Connection: close\r\n"
- "\r\n";
- CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
- }
- } // namespace aria2
|