HttpRequestTest.cc 21 KB

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