HttpRequestTest.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. public:
  30. void setUp() {}
  31. void testGetStartByte();
  32. void testGetEndByte();
  33. void testCreateRequest();
  34. void testCreateRequest_ftp();
  35. void testCreateRequest_with_cookie();
  36. void testCreateRequest_query();
  37. void testCreateProxyRequest();
  38. void testIsRangeSatisfied();
  39. void testUserAgent();
  40. void testAddHeader();
  41. void testAddAcceptType();
  42. void testEnableAcceptEncoding();
  43. };
  44. CPPUNIT_TEST_SUITE_REGISTRATION( HttpRequestTest );
  45. void HttpRequestTest::testGetStartByte()
  46. {
  47. HttpRequest httpRequest;
  48. SharedHandle<Piece> p(new Piece(1, 1024));
  49. SharedHandle<Segment> segment(new PiecedSegment(1024, p));
  50. CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getStartByte());
  51. httpRequest.setSegment(segment);
  52. CPPUNIT_ASSERT_EQUAL((off_t)1024LL, httpRequest.getStartByte());
  53. }
  54. void HttpRequestTest::testGetEndByte()
  55. {
  56. size_t index = 1;
  57. size_t length = 1024*1024-1024;
  58. size_t segmentLength = 1024*1024;
  59. HttpRequest httpRequest;
  60. SharedHandle<Piece> piece(new Piece(index, length));
  61. SharedHandle<Segment> segment(new PiecedSegment(segmentLength, piece));
  62. CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getEndByte());
  63. httpRequest.setSegment(segment);
  64. CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getEndByte());
  65. SharedHandle<Request> request(new Request());
  66. request->supportsPersistentConnection(true);
  67. request->setPipeliningHint(true);
  68. httpRequest.setRequest(request);
  69. CPPUNIT_ASSERT_EQUAL((off_t)(segmentLength*index+length-1),
  70. httpRequest.getEndByte());
  71. request->setPipeliningHint(false);
  72. CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getEndByte());
  73. }
  74. void HttpRequestTest::testCreateRequest()
  75. {
  76. SharedHandle<Piece> p;
  77. Option option;
  78. option.put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
  79. option.put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  80. option.put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  81. option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  82. option.put(PREF_HTTP_USER, "aria2user");
  83. option.put(PREF_HTTP_PASSWD, "aria2passwd");
  84. option.put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  85. option.put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  86. SharedHandle<AuthConfigFactory> authConfigFactory(new AuthConfigFactory(&option));
  87. SingletonHolder<SharedHandle<AuthConfigFactory> >::instance(authConfigFactory);
  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. // remove "Connection: close" and add end byte range
  98. request->setPipeliningHint(true);
  99. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  100. "User-Agent: aria2\r\n"
  101. "Accept: */*\r\n"
  102. "Host: localhost:8080\r\n"
  103. "Pragma: no-cache\r\n"
  104. "Cache-Control: no-cache\r\n"
  105. "Range: bytes=0-1023\r\n"
  106. "\r\n";
  107. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  108. request->setPipeliningHint(false);
  109. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  110. "User-Agent: aria2\r\n"
  111. "Accept: */*\r\n"
  112. "Host: localhost:8080\r\n"
  113. "Pragma: no-cache\r\n"
  114. "Cache-Control: no-cache\r\n"
  115. "Connection: close\r\n"
  116. "\r\n";
  117. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  118. p.reset(new Piece(1, 1024*1024));
  119. segment.reset(new PiecedSegment(1024*1024, p));
  120. httpRequest.setSegment(segment);
  121. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  122. "User-Agent: aria2\r\n"
  123. "Accept: */*\r\n"
  124. "Host: localhost:8080\r\n"
  125. "Pragma: no-cache\r\n"
  126. "Cache-Control: no-cache\r\n"
  127. "Connection: close\r\n"
  128. "Range: bytes=1048576-\r\n"
  129. "\r\n";
  130. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  131. request->setPipeliningHint(true);
  132. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  133. "User-Agent: aria2\r\n"
  134. "Accept: */*\r\n"
  135. "Host: localhost:8080\r\n"
  136. "Pragma: no-cache\r\n"
  137. "Cache-Control: no-cache\r\n"
  138. "Range: bytes=1048576-2097151\r\n"
  139. "\r\n";
  140. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  141. // redirection set persistent connection flag to true
  142. request->redirectUrl("http://localhost:8080/archives/download/aria2-1.0.0.tar.bz2");
  143. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  144. "User-Agent: aria2\r\n"
  145. "Accept: */*\r\n"
  146. "Host: localhost:8080\r\n"
  147. "Pragma: no-cache\r\n"
  148. "Cache-Control: no-cache\r\n"
  149. "Range: bytes=1048576-2097151\r\n"
  150. "\r\n";
  151. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  152. request->supportsPersistentConnection(true);
  153. request->setPipeliningHint(false);
  154. // this only removes "Connection: close".
  155. request->setKeepAliveHint(true);
  156. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  157. "User-Agent: aria2\r\n"
  158. "Accept: */*\r\n"
  159. "Host: localhost:8080\r\n"
  160. "Pragma: no-cache\r\n"
  161. "Cache-Control: no-cache\r\n"
  162. "Range: bytes=1048576-\r\n"
  163. "\r\n";
  164. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  165. request->setKeepAliveHint(false);
  166. request->resetUrl();
  167. p.reset(new Piece(0, 1024*1024));
  168. segment.reset(new PiecedSegment(1024*1024, p));
  169. httpRequest.setSegment(segment);
  170. // enable http auth
  171. option.put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  172. httpRequest.configure(&option);
  173. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  174. "User-Agent: aria2\r\n"
  175. "Accept: */*\r\n"
  176. "Host: localhost:8080\r\n"
  177. "Pragma: no-cache\r\n"
  178. "Cache-Control: no-cache\r\n"
  179. "Connection: close\r\n"
  180. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  181. "\r\n";
  182. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  183. // enable http proxy auth
  184. option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  185. httpRequest.configure(&option);
  186. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  187. "User-Agent: aria2\r\n"
  188. "Accept: */*\r\n"
  189. "Host: localhost:8080\r\n"
  190. "Pragma: no-cache\r\n"
  191. "Cache-Control: no-cache\r\n"
  192. "Connection: close\r\n"
  193. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  194. "\r\n";
  195. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  196. option.put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  197. httpRequest.configure(&option);
  198. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  199. "User-Agent: aria2\r\n"
  200. "Accept: */*\r\n"
  201. "Host: localhost:8080\r\n"
  202. "Pragma: no-cache\r\n"
  203. "Cache-Control: no-cache\r\n"
  204. "Connection: close\r\n"
  205. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  206. "\r\n";
  207. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  208. option.put(PREF_HTTP_PROXY_METHOD, V_GET);
  209. httpRequest.configure(&option);
  210. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  211. "User-Agent: aria2\r\n"
  212. "Accept: */*\r\n"
  213. "Host: localhost:8080\r\n"
  214. "Pragma: no-cache\r\n"
  215. "Cache-Control: no-cache\r\n"
  216. "Connection: close\r\n"
  217. "Proxy-Connection: close\r\n"
  218. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  219. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  220. "\r\n";
  221. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  222. request->setPipeliningHint(true);
  223. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  224. "User-Agent: aria2\r\n"
  225. "Accept: */*\r\n"
  226. "Host: localhost:8080\r\n"
  227. "Pragma: no-cache\r\n"
  228. "Cache-Control: no-cache\r\n"
  229. "Range: bytes=0-1048575\r\n"
  230. "Proxy-Connection: Keep-Alive\r\n"
  231. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  232. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  233. "\r\n";
  234. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  235. request->setPipeliningHint(false);
  236. option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  237. httpRequest.configure(&option);
  238. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  239. "User-Agent: aria2\r\n"
  240. "Accept: */*\r\n"
  241. "Host: localhost:8080\r\n"
  242. "Pragma: no-cache\r\n"
  243. "Cache-Control: no-cache\r\n"
  244. "Connection: close\r\n"
  245. "Proxy-Connection: close\r\n"
  246. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  247. "\r\n";
  248. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  249. }
  250. void HttpRequestTest::testCreateRequest_ftp()
  251. {
  252. Option option;
  253. option.put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
  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_HTTP_USER, "aria2user");
  258. option.put(PREF_HTTP_PASSWD, "aria2passwd");
  259. option.put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  260. option.put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  261. SharedHandle<AuthConfigFactory> authConfigFactory
  262. (new AuthConfigFactory(&option));
  263. SingletonHolder<SharedHandle<AuthConfigFactory> >::instance(authConfigFactory);
  264. SharedHandle<Request> request(new Request());
  265. request->setUrl("ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  266. HttpRequest httpRequest;
  267. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  268. SharedHandle<Segment> segment
  269. (new PiecedSegment(1024*1024, p));
  270. httpRequest.disableContentEncoding();
  271. httpRequest.setRequest(request);
  272. httpRequest.setSegment(segment);
  273. httpRequest.configure(&option);
  274. std::string expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 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. "\r\n";
  282. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  283. // How to enable HTTP proxy authorization in FTP download via HTTP proxy
  284. option.put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  285. option.put(PREF_HTTP_PROXY_METHOD, V_GET);
  286. option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  287. httpRequest.configure(&option);
  288. expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  289. "User-Agent: aria2\r\n"
  290. "Accept: */*\r\n"
  291. "Host: localhost:8080\r\n"
  292. "Pragma: no-cache\r\n"
  293. "Cache-Control: no-cache\r\n"
  294. "Connection: close\r\n"
  295. "Proxy-Connection: close\r\n"
  296. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  297. "\r\n";
  298. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  299. }
  300. void HttpRequestTest::testCreateRequest_with_cookie()
  301. {
  302. SharedHandle<Request> request(new Request());
  303. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  304. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  305. SharedHandle<Segment> segment
  306. (new PiecedSegment(1024*1024, p));
  307. Cookie cookie1("name1", "value1", "/archives", "localhost", false);
  308. Cookie cookie2("name2", "value2", "/archives/download", "localhost", false);
  309. Cookie cookie3("name3", "value3", "/archives/download", ".aria2.org", false);
  310. Cookie cookie4("name4", "value4", "/archives/", ".aria2.org", true);
  311. SharedHandle<CookieStorage> st(new CookieStorage());
  312. CPPUNIT_ASSERT(st->store(cookie1));
  313. CPPUNIT_ASSERT(st->store(cookie2));
  314. CPPUNIT_ASSERT(st->store(cookie3));
  315. CPPUNIT_ASSERT(st->store(cookie4));
  316. HttpRequest httpRequest;
  317. httpRequest.disableContentEncoding();
  318. httpRequest.setRequest(request);
  319. httpRequest.setSegment(segment);
  320. httpRequest.setCookieStorage(st);
  321. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  322. "User-Agent: aria2\r\n"
  323. "Accept: */*\r\n"
  324. "Host: localhost\r\n"
  325. "Pragma: no-cache\r\n"
  326. "Cache-Control: no-cache\r\n"
  327. "Connection: close\r\n"
  328. "Cookie: name1=value1;\r\n"
  329. "\r\n";
  330. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  331. request->setUrl("http://localhost/archives/download/aria2-1.0.0.tar.bz2");
  332. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  333. "User-Agent: aria2\r\n"
  334. "Accept: */*\r\n"
  335. "Host: localhost\r\n"
  336. "Pragma: no-cache\r\n"
  337. "Cache-Control: no-cache\r\n"
  338. "Connection: close\r\n"
  339. "Cookie: name2=value2;name1=value1;\r\n"
  340. "\r\n";
  341. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  342. request->setUrl("http://www.aria2.org/archives/download/aria2-1.0.0.tar.bz2");
  343. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  344. "User-Agent: aria2\r\n"
  345. "Accept: */*\r\n"
  346. "Host: www.aria2.org\r\n"
  347. "Pragma: no-cache\r\n"
  348. "Cache-Control: no-cache\r\n"
  349. "Connection: close\r\n"
  350. "Cookie: name3=value3;\r\n"
  351. "\r\n";
  352. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  353. request->setUrl("https://www.aria2.org/archives/download/"
  354. "aria2-1.0.0.tar.bz2");
  355. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  356. "User-Agent: aria2\r\n"
  357. "Accept: */*\r\n"
  358. "Host: www.aria2.org\r\n"
  359. "Pragma: no-cache\r\n"
  360. "Cache-Control: no-cache\r\n"
  361. "Connection: close\r\n"
  362. "Cookie: name3=value3;name4=value4;\r\n"
  363. "\r\n";
  364. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  365. }
  366. void HttpRequestTest::testCreateRequest_query()
  367. {
  368. SharedHandle<Request> request(new Request());
  369. request->setUrl("http://localhost/wiki?id=9ad5109a-b8a5-4edf-9373-56a1c34ae138");
  370. HttpRequest httpRequest;
  371. httpRequest.disableContentEncoding();
  372. httpRequest.setRequest(request);
  373. std::string expectedText =
  374. "GET /wiki?id=9ad5109a-b8a5-4edf-9373-56a1c34ae138 HTTP/1.1\r\n"
  375. "User-Agent: aria2\r\n"
  376. "Accept: */*\r\n"
  377. "Host: localhost\r\n"
  378. "Pragma: no-cache\r\n"
  379. "Cache-Control: no-cache\r\n"
  380. "Connection: close\r\n"
  381. "\r\n";
  382. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  383. }
  384. void HttpRequestTest::testCreateProxyRequest()
  385. {
  386. SharedHandle<Request> request(new Request());
  387. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  388. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  389. SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p));
  390. HttpRequest httpRequest;
  391. httpRequest.setRequest(request);
  392. httpRequest.setSegment(segment);
  393. request->supportsPersistentConnection(true);
  394. std::string expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  395. "User-Agent: aria2\r\n"
  396. "Host: localhost:80\r\n"
  397. "Proxy-Connection: close\r\n"
  398. "\r\n";
  399. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  400. // adds Keep-Alive header.
  401. request->setKeepAliveHint(true);
  402. expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  403. "User-Agent: aria2\r\n"
  404. "Host: localhost:80\r\n"
  405. "Proxy-Connection: Keep-Alive\r\n"
  406. "\r\n";
  407. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  408. request->setKeepAliveHint(false);
  409. // pipelining also adds Keep-Alive header.
  410. request->setPipeliningHint(true);
  411. expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  412. "User-Agent: aria2\r\n"
  413. "Host: localhost:80\r\n"
  414. "Proxy-Connection: Keep-Alive\r\n"
  415. "\r\n";
  416. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  417. }
  418. void HttpRequestTest::testIsRangeSatisfied()
  419. {
  420. SharedHandle<Request> request(new Request());
  421. request->supportsPersistentConnection(true);
  422. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  423. request->setPipeliningHint(false); // default: false
  424. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  425. SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p));
  426. HttpRequest httpRequest;
  427. httpRequest.setRequest(request);
  428. httpRequest.setSegment(segment);
  429. SharedHandle<Range> range(new Range());
  430. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  431. p.reset(new Piece(1, 1024*1024));
  432. segment.reset(new PiecedSegment(1024*1024, p));
  433. httpRequest.setSegment(segment);
  434. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  435. uint64_t entityLength = segment->getSegmentLength()*10;
  436. range.reset(new Range(segment->getPosition(), 0, entityLength));
  437. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  438. httpRequest.setEntityLength(entityLength-1);
  439. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  440. httpRequest.setEntityLength(entityLength);
  441. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  442. request->setPipeliningHint(true);
  443. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  444. range.reset(new Range(segment->getPosition(),
  445. segment->getPosition()+segment->getLength()-1,
  446. entityLength));
  447. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  448. range.reset(new Range(0, segment->getPosition()+segment->getLength()-1,
  449. entityLength));
  450. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  451. }
  452. void HttpRequestTest::testUserAgent()
  453. {
  454. Option option;
  455. SharedHandle<AuthConfigFactory> authConfigFactory
  456. (new AuthConfigFactory(&option));
  457. SingletonHolder<SharedHandle<AuthConfigFactory> >::instance(authConfigFactory);
  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