HttpRequestTest.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. #include "HttpRequest.h"
  2. #include "prefs.h"
  3. #include "AuthConfigFactory.h"
  4. #include "PiecedSegment.h"
  5. #include "Piece.h"
  6. #include "Range.h"
  7. #include "Request.h"
  8. #include "Option.h"
  9. #include "array_fun.h"
  10. #include "CookieStorage.h"
  11. #include <cppunit/extensions/HelperMacros.h>
  12. namespace aria2 {
  13. class HttpRequestTest : public CppUnit::TestFixture {
  14. CPPUNIT_TEST_SUITE(HttpRequestTest);
  15. CPPUNIT_TEST(testGetStartByte);
  16. CPPUNIT_TEST(testGetEndByte);
  17. CPPUNIT_TEST(testCreateRequest);
  18. CPPUNIT_TEST(testCreateRequest_ftp);
  19. CPPUNIT_TEST(testCreateRequest_with_cookie);
  20. CPPUNIT_TEST(testCreateRequest_query);
  21. CPPUNIT_TEST(testCreateProxyRequest);
  22. CPPUNIT_TEST(testIsRangeSatisfied);
  23. CPPUNIT_TEST(testUserAgent);
  24. CPPUNIT_TEST(testAddHeader);
  25. CPPUNIT_TEST(testAddAcceptType);
  26. CPPUNIT_TEST(testEnableAcceptEncoding);
  27. CPPUNIT_TEST_SUITE_END();
  28. private:
  29. SharedHandle<Option> _option;
  30. public:
  31. void setUp()
  32. {
  33. _option.reset(new Option());
  34. SharedHandle<AuthConfigFactory> authConfigFactory
  35. (new AuthConfigFactory(_option.get()));
  36. SingletonHolder<SharedHandle<AuthConfigFactory> >::instance
  37. (authConfigFactory);
  38. }
  39. void testGetStartByte();
  40. void testGetEndByte();
  41. void testCreateRequest();
  42. void testCreateRequest_ftp();
  43. void testCreateRequest_with_cookie();
  44. void testCreateRequest_query();
  45. void testCreateProxyRequest();
  46. void testIsRangeSatisfied();
  47. void testUserAgent();
  48. void testAddHeader();
  49. void testAddAcceptType();
  50. void testEnableAcceptEncoding();
  51. };
  52. CPPUNIT_TEST_SUITE_REGISTRATION( HttpRequestTest );
  53. void HttpRequestTest::testGetStartByte()
  54. {
  55. HttpRequest httpRequest;
  56. SharedHandle<Piece> p(new Piece(1, 1024));
  57. SharedHandle<Segment> segment(new PiecedSegment(1024, p));
  58. CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getStartByte());
  59. httpRequest.setSegment(segment);
  60. CPPUNIT_ASSERT_EQUAL((off_t)1024LL, httpRequest.getStartByte());
  61. }
  62. void HttpRequestTest::testGetEndByte()
  63. {
  64. size_t index = 1;
  65. size_t length = 1024*1024-1024;
  66. size_t segmentLength = 1024*1024;
  67. HttpRequest httpRequest;
  68. SharedHandle<Piece> piece(new Piece(index, length));
  69. SharedHandle<Segment> segment(new PiecedSegment(segmentLength, piece));
  70. CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getEndByte());
  71. httpRequest.setSegment(segment);
  72. CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getEndByte());
  73. SharedHandle<Request> request(new Request());
  74. request->supportsPersistentConnection(true);
  75. request->setPipeliningHint(true);
  76. httpRequest.setRequest(request);
  77. CPPUNIT_ASSERT_EQUAL((off_t)(segmentLength*index+length-1),
  78. httpRequest.getEndByte());
  79. request->setPipeliningHint(false);
  80. CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getEndByte());
  81. }
  82. void HttpRequestTest::testCreateRequest()
  83. {
  84. SharedHandle<Piece> p;
  85. _option->put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  86. _option->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  87. _option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  88. _option->put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  89. _option->put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  90. SharedHandle<Request> request(new Request());
  91. request->supportsPersistentConnection(true);
  92. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  93. p.reset(new Piece(0, 1024));
  94. SharedHandle<Segment> segment(new PiecedSegment(1024, p));
  95. HttpRequest httpRequest;
  96. httpRequest.disableContentEncoding();
  97. httpRequest.setRequest(request);
  98. httpRequest.setSegment(segment);
  99. // remove "Connection: close" and add end byte range
  100. request->setPipeliningHint(true);
  101. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  102. "User-Agent: aria2\r\n"
  103. "Accept: */*\r\n"
  104. "Host: localhost:8080\r\n"
  105. "Pragma: no-cache\r\n"
  106. "Cache-Control: no-cache\r\n"
  107. "Range: bytes=0-1023\r\n"
  108. "\r\n";
  109. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  110. request->setPipeliningHint(false);
  111. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  112. "User-Agent: aria2\r\n"
  113. "Accept: */*\r\n"
  114. "Host: localhost:8080\r\n"
  115. "Pragma: no-cache\r\n"
  116. "Cache-Control: no-cache\r\n"
  117. "Connection: close\r\n"
  118. "\r\n";
  119. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  120. p.reset(new Piece(1, 1024*1024));
  121. segment.reset(new PiecedSegment(1024*1024, p));
  122. httpRequest.setSegment(segment);
  123. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  124. "User-Agent: aria2\r\n"
  125. "Accept: */*\r\n"
  126. "Host: localhost:8080\r\n"
  127. "Pragma: no-cache\r\n"
  128. "Cache-Control: no-cache\r\n"
  129. "Connection: close\r\n"
  130. "Range: bytes=1048576-\r\n"
  131. "\r\n";
  132. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  133. request->setPipeliningHint(true);
  134. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  135. "User-Agent: aria2\r\n"
  136. "Accept: */*\r\n"
  137. "Host: localhost:8080\r\n"
  138. "Pragma: no-cache\r\n"
  139. "Cache-Control: no-cache\r\n"
  140. "Range: bytes=1048576-2097151\r\n"
  141. "\r\n";
  142. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  143. // redirection set persistent connection flag to true
  144. request->redirectUrl("http://localhost:8080/archives/download/aria2-1.0.0.tar.bz2");
  145. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  146. "User-Agent: aria2\r\n"
  147. "Accept: */*\r\n"
  148. "Host: localhost:8080\r\n"
  149. "Pragma: no-cache\r\n"
  150. "Cache-Control: no-cache\r\n"
  151. "Range: bytes=1048576-2097151\r\n"
  152. "\r\n";
  153. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  154. request->supportsPersistentConnection(true);
  155. request->setPipeliningHint(false);
  156. // this only removes "Connection: close".
  157. request->setKeepAliveHint(true);
  158. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  159. "User-Agent: aria2\r\n"
  160. "Accept: */*\r\n"
  161. "Host: localhost:8080\r\n"
  162. "Pragma: no-cache\r\n"
  163. "Cache-Control: no-cache\r\n"
  164. "Range: bytes=1048576-\r\n"
  165. "\r\n";
  166. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  167. request->setKeepAliveHint(false);
  168. request->resetUrl();
  169. p.reset(new Piece(0, 1024*1024));
  170. segment.reset(new PiecedSegment(1024*1024, p));
  171. httpRequest.setSegment(segment);
  172. // enable http auth
  173. _option->put(PREF_HTTP_USER, "aria2user");
  174. _option->put(PREF_HTTP_PASSWD, "aria2passwd");
  175. httpRequest.configure(_option.get());
  176. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  177. "User-Agent: aria2\r\n"
  178. "Accept: */*\r\n"
  179. "Host: localhost:8080\r\n"
  180. "Pragma: no-cache\r\n"
  181. "Cache-Control: no-cache\r\n"
  182. "Connection: close\r\n"
  183. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  184. "\r\n";
  185. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  186. // enable http proxy auth
  187. _option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  188. httpRequest.configure(_option.get());
  189. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  190. "User-Agent: aria2\r\n"
  191. "Accept: */*\r\n"
  192. "Host: localhost:8080\r\n"
  193. "Pragma: no-cache\r\n"
  194. "Cache-Control: no-cache\r\n"
  195. "Connection: close\r\n"
  196. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  197. "\r\n";
  198. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  199. _option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  200. httpRequest.configure(_option.get());
  201. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  202. "User-Agent: aria2\r\n"
  203. "Accept: */*\r\n"
  204. "Host: localhost:8080\r\n"
  205. "Pragma: no-cache\r\n"
  206. "Cache-Control: no-cache\r\n"
  207. "Connection: close\r\n"
  208. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  209. "\r\n";
  210. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  211. _option->put(PREF_HTTP_PROXY_METHOD, V_GET);
  212. httpRequest.configure(_option.get());
  213. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  214. "User-Agent: aria2\r\n"
  215. "Accept: */*\r\n"
  216. "Host: localhost:8080\r\n"
  217. "Pragma: no-cache\r\n"
  218. "Cache-Control: no-cache\r\n"
  219. "Connection: close\r\n"
  220. "Proxy-Connection: close\r\n"
  221. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  222. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  223. "\r\n";
  224. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  225. request->setPipeliningHint(true);
  226. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  227. "User-Agent: aria2\r\n"
  228. "Accept: */*\r\n"
  229. "Host: localhost:8080\r\n"
  230. "Pragma: no-cache\r\n"
  231. "Cache-Control: no-cache\r\n"
  232. "Range: bytes=0-1048575\r\n"
  233. "Proxy-Connection: Keep-Alive\r\n"
  234. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  235. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  236. "\r\n";
  237. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  238. request->setPipeliningHint(false);
  239. _option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  240. httpRequest.configure(_option.get());
  241. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  242. "User-Agent: aria2\r\n"
  243. "Accept: */*\r\n"
  244. "Host: localhost:8080\r\n"
  245. "Pragma: no-cache\r\n"
  246. "Cache-Control: no-cache\r\n"
  247. "Connection: close\r\n"
  248. "Proxy-Connection: close\r\n"
  249. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  250. "\r\n";
  251. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  252. }
  253. void HttpRequestTest::testCreateRequest_ftp()
  254. {
  255. _option->put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  256. _option->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  257. _option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  258. _option->put(PREF_FTP_USER, "aria2user");
  259. _option->put(PREF_FTP_PASSWD, "aria2passwd");
  260. _option->put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  261. _option->put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  262. SharedHandle<Request> request(new Request());
  263. request->setUrl("ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  264. HttpRequest httpRequest;
  265. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  266. SharedHandle<Segment> segment
  267. (new PiecedSegment(1024*1024, p));
  268. httpRequest.disableContentEncoding();
  269. httpRequest.setRequest(request);
  270. httpRequest.setSegment(segment);
  271. httpRequest.configure(_option.get());
  272. std::string expectedText =
  273. "GET ftp://aria2user@localhost:8080/archives/aria2-1.0.0.tar.bz2"
  274. " HTTP/1.1\r\n"
  275. "User-Agent: aria2\r\n"
  276. "Accept: */*\r\n"
  277. "Host: localhost:8080\r\n"
  278. "Pragma: no-cache\r\n"
  279. "Cache-Control: no-cache\r\n"
  280. "Connection: close\r\n"
  281. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  282. "\r\n";
  283. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  284. // How to enable HTTP proxy authorization in FTP download via HTTP proxy
  285. _option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  286. _option->put(PREF_HTTP_PROXY_METHOD, V_GET);
  287. _option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  288. httpRequest.configure(_option.get());
  289. expectedText =
  290. "GET ftp://aria2user@localhost:8080/archives/aria2-1.0.0.tar.bz2"
  291. " HTTP/1.1\r\n"
  292. "User-Agent: aria2\r\n"
  293. "Accept: */*\r\n"
  294. "Host: localhost:8080\r\n"
  295. "Pragma: no-cache\r\n"
  296. "Cache-Control: no-cache\r\n"
  297. "Connection: close\r\n"
  298. "Proxy-Connection: close\r\n"
  299. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  300. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  301. "\r\n";
  302. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  303. }
  304. void HttpRequestTest::testCreateRequest_with_cookie()
  305. {
  306. SharedHandle<Request> request(new Request());
  307. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  308. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  309. SharedHandle<Segment> segment
  310. (new PiecedSegment(1024*1024, p));
  311. Cookie cookie1("name1", "value1", "/archives", "localhost", false);
  312. Cookie cookie2("name2", "value2", "/archives/download", "localhost", false);
  313. Cookie cookie3("name3", "value3", "/archives/download", ".aria2.org", false);
  314. Cookie cookie4("name4", "value4", "/archives/", ".aria2.org", true);
  315. SharedHandle<CookieStorage> st(new CookieStorage());
  316. CPPUNIT_ASSERT(st->store(cookie1));
  317. CPPUNIT_ASSERT(st->store(cookie2));
  318. CPPUNIT_ASSERT(st->store(cookie3));
  319. CPPUNIT_ASSERT(st->store(cookie4));
  320. HttpRequest httpRequest;
  321. httpRequest.disableContentEncoding();
  322. httpRequest.setRequest(request);
  323. httpRequest.setSegment(segment);
  324. httpRequest.setCookieStorage(st);
  325. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  326. "User-Agent: aria2\r\n"
  327. "Accept: */*\r\n"
  328. "Host: localhost\r\n"
  329. "Pragma: no-cache\r\n"
  330. "Cache-Control: no-cache\r\n"
  331. "Connection: close\r\n"
  332. "Cookie: name1=value1;\r\n"
  333. "\r\n";
  334. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  335. request->setUrl("http://localhost/archives/download/aria2-1.0.0.tar.bz2");
  336. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  337. "User-Agent: aria2\r\n"
  338. "Accept: */*\r\n"
  339. "Host: localhost\r\n"
  340. "Pragma: no-cache\r\n"
  341. "Cache-Control: no-cache\r\n"
  342. "Connection: close\r\n"
  343. "Cookie: name2=value2;name1=value1;\r\n"
  344. "\r\n";
  345. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  346. request->setUrl("http://www.aria2.org/archives/download/aria2-1.0.0.tar.bz2");
  347. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  348. "User-Agent: aria2\r\n"
  349. "Accept: */*\r\n"
  350. "Host: www.aria2.org\r\n"
  351. "Pragma: no-cache\r\n"
  352. "Cache-Control: no-cache\r\n"
  353. "Connection: close\r\n"
  354. "Cookie: name3=value3;\r\n"
  355. "\r\n";
  356. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  357. request->setUrl("https://www.aria2.org/archives/download/"
  358. "aria2-1.0.0.tar.bz2");
  359. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  360. "User-Agent: aria2\r\n"
  361. "Accept: */*\r\n"
  362. "Host: www.aria2.org\r\n"
  363. "Pragma: no-cache\r\n"
  364. "Cache-Control: no-cache\r\n"
  365. "Connection: close\r\n"
  366. "Cookie: name3=value3;name4=value4;\r\n"
  367. "\r\n";
  368. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  369. }
  370. void HttpRequestTest::testCreateRequest_query()
  371. {
  372. SharedHandle<Request> request(new Request());
  373. request->setUrl("http://localhost/wiki?id=9ad5109a-b8a5-4edf-9373-56a1c34ae138");
  374. HttpRequest httpRequest;
  375. httpRequest.disableContentEncoding();
  376. httpRequest.setRequest(request);
  377. std::string expectedText =
  378. "GET /wiki?id=9ad5109a-b8a5-4edf-9373-56a1c34ae138 HTTP/1.1\r\n"
  379. "User-Agent: aria2\r\n"
  380. "Accept: */*\r\n"
  381. "Host: localhost\r\n"
  382. "Pragma: no-cache\r\n"
  383. "Cache-Control: no-cache\r\n"
  384. "Connection: close\r\n"
  385. "\r\n";
  386. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  387. }
  388. void HttpRequestTest::testCreateProxyRequest()
  389. {
  390. SharedHandle<Request> request(new Request());
  391. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  392. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  393. SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p));
  394. HttpRequest httpRequest;
  395. httpRequest.setRequest(request);
  396. httpRequest.setSegment(segment);
  397. request->supportsPersistentConnection(true);
  398. std::string expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  399. "User-Agent: aria2\r\n"
  400. "Host: localhost:80\r\n"
  401. "Proxy-Connection: close\r\n"
  402. "\r\n";
  403. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  404. // adds Keep-Alive header.
  405. request->setKeepAliveHint(true);
  406. expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  407. "User-Agent: aria2\r\n"
  408. "Host: localhost:80\r\n"
  409. "Proxy-Connection: Keep-Alive\r\n"
  410. "\r\n";
  411. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  412. request->setKeepAliveHint(false);
  413. // pipelining also adds Keep-Alive header.
  414. request->setPipeliningHint(true);
  415. expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  416. "User-Agent: aria2\r\n"
  417. "Host: localhost:80\r\n"
  418. "Proxy-Connection: Keep-Alive\r\n"
  419. "\r\n";
  420. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  421. }
  422. void HttpRequestTest::testIsRangeSatisfied()
  423. {
  424. SharedHandle<Request> request(new Request());
  425. request->supportsPersistentConnection(true);
  426. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  427. request->setPipeliningHint(false); // default: false
  428. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  429. SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p));
  430. HttpRequest httpRequest;
  431. httpRequest.setRequest(request);
  432. httpRequest.setSegment(segment);
  433. SharedHandle<Range> range(new Range());
  434. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  435. p.reset(new Piece(1, 1024*1024));
  436. segment.reset(new PiecedSegment(1024*1024, p));
  437. httpRequest.setSegment(segment);
  438. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  439. uint64_t entityLength = segment->getSegmentLength()*10;
  440. range.reset(new Range(segment->getPosition(), 0, entityLength));
  441. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  442. httpRequest.setEntityLength(entityLength-1);
  443. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  444. httpRequest.setEntityLength(entityLength);
  445. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  446. request->setPipeliningHint(true);
  447. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  448. range.reset(new Range(segment->getPosition(),
  449. segment->getPosition()+segment->getLength()-1,
  450. entityLength));
  451. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  452. range.reset(new Range(0, segment->getPosition()+segment->getLength()-1,
  453. entityLength));
  454. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  455. }
  456. void HttpRequestTest::testUserAgent()
  457. {
  458. SharedHandle<Request> request(new Request());
  459. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  460. SharedHandle<Piece> p(new Piece(0, 1024));
  461. SharedHandle<Segment> segment(new PiecedSegment(1024, p));
  462. HttpRequest httpRequest;
  463. httpRequest.disableContentEncoding();
  464. httpRequest.setRequest(request);
  465. httpRequest.setSegment(segment);
  466. httpRequest.setUserAgent("aria2 (Linux)");
  467. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  468. "User-Agent: aria2 (Linux)\r\n"
  469. "Accept: */*\r\n"
  470. "Host: localhost:8080\r\n"
  471. "Pragma: no-cache\r\n"
  472. "Cache-Control: no-cache\r\n"
  473. "Connection: close\r\n"
  474. "\r\n";
  475. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  476. std::string expectedTextForProxy = "CONNECT localhost:8080 HTTP/1.1\r\n"
  477. "User-Agent: aria2 (Linux)\r\n"
  478. "Host: localhost:8080\r\n"
  479. "Proxy-Connection: close\r\n"
  480. "\r\n";
  481. CPPUNIT_ASSERT_EQUAL(expectedTextForProxy, httpRequest.createProxyRequest());
  482. }
  483. void HttpRequestTest::testAddHeader()
  484. {
  485. SharedHandle<Request> request(new Request());
  486. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  487. HttpRequest httpRequest;
  488. httpRequest.disableContentEncoding();
  489. httpRequest.setRequest(request);
  490. httpRequest.addHeader("X-ARIA2: v0.13\nX-ARIA2-DISTRIBUTE: enabled\n");
  491. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  492. "User-Agent: aria2\r\n"
  493. "Accept: */*\r\n"
  494. "Host: localhost\r\n"
  495. "Pragma: no-cache\r\n"
  496. "Cache-Control: no-cache\r\n"
  497. "Connection: close\r\n"
  498. "X-ARIA2: v0.13\r\n"
  499. "X-ARIA2-DISTRIBUTE: enabled\r\n"
  500. "\r\n";
  501. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  502. }
  503. void HttpRequestTest::testAddAcceptType()
  504. {
  505. std::string acceptTypes[] = { "cream/custard",
  506. "muffin/chocolate" };
  507. SharedHandle<Request> request(new Request());
  508. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  509. HttpRequest httpRequest;
  510. httpRequest.disableContentEncoding();
  511. httpRequest.setRequest(request);
  512. httpRequest.addAcceptType(&acceptTypes[0], &acceptTypes[arrayLength(acceptTypes)]);
  513. std::string expectedText =
  514. "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  515. "User-Agent: aria2\r\n"
  516. "Accept: */*,cream/custard,muffin/chocolate\r\n"
  517. "Host: localhost\r\n"
  518. "Pragma: no-cache\r\n"
  519. "Cache-Control: no-cache\r\n"
  520. "Connection: close\r\n"
  521. "\r\n";
  522. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  523. }
  524. void HttpRequestTest::testEnableAcceptEncoding()
  525. {
  526. SharedHandle<Request> request(new Request());
  527. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  528. HttpRequest httpRequest;
  529. httpRequest.setRequest(request);
  530. std::string acceptEncodings;
  531. #ifdef HAVE_LIBZ
  532. acceptEncodings += "deflate, gzip";
  533. #endif // HAVE_LIBZ
  534. std::string expectedText =
  535. "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  536. "User-Agent: aria2\r\n"
  537. "Accept: */*\r\n";
  538. if(!acceptEncodings.empty()) {
  539. expectedText += "Accept-Encoding: "+acceptEncodings+"\r\n";
  540. }
  541. expectedText +=
  542. "Host: localhost\r\n"
  543. "Pragma: no-cache\r\n"
  544. "Cache-Control: no-cache\r\n"
  545. "Connection: close\r\n"
  546. "\r\n";
  547. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  548. }
  549. } // namespace aria2