HttpRequestTest.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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 "CookieBox.h"
  9. #include "Option.h"
  10. #include "array_fun.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", "tt.localhost", false);
  310. Cookie cookie4("name4", "value4", "/archives/download", "tt.localhost", true);
  311. request->cookieBox->add(cookie1);
  312. request->cookieBox->add(cookie2);
  313. request->cookieBox->add(cookie3);
  314. request->cookieBox->add(cookie4);
  315. HttpRequest httpRequest;
  316. httpRequest.disableContentEncoding();
  317. httpRequest.setRequest(request);
  318. httpRequest.setSegment(segment);
  319. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  320. "User-Agent: aria2\r\n"
  321. "Accept: */*\r\n"
  322. "Host: localhost\r\n"
  323. "Pragma: no-cache\r\n"
  324. "Cache-Control: no-cache\r\n"
  325. "Connection: close\r\n"
  326. "Cookie: name1=value1;\r\n"
  327. "\r\n";
  328. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  329. request->setUrl("http://localhost/archives/download/aria2-1.0.0.tar.bz2");
  330. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  331. "User-Agent: aria2\r\n"
  332. "Accept: */*\r\n"
  333. "Host: localhost\r\n"
  334. "Pragma: no-cache\r\n"
  335. "Cache-Control: no-cache\r\n"
  336. "Connection: close\r\n"
  337. "Cookie: name1=value1;name2=value2;\r\n"
  338. "\r\n";
  339. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  340. request->setUrl("http://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  341. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  342. "User-Agent: aria2\r\n"
  343. "Accept: */*\r\n"
  344. "Host: tt.localhost\r\n"
  345. "Pragma: no-cache\r\n"
  346. "Cache-Control: no-cache\r\n"
  347. "Connection: close\r\n"
  348. "Cookie: name1=value1;name2=value2;name3=value3;\r\n"
  349. "\r\n";
  350. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  351. request->setUrl("https://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  352. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  353. "User-Agent: aria2\r\n"
  354. "Accept: */*\r\n"
  355. "Host: tt.localhost\r\n"
  356. "Pragma: no-cache\r\n"
  357. "Cache-Control: no-cache\r\n"
  358. "Connection: close\r\n"
  359. "Cookie: name1=value1;name2=value2;name3=value3;name4=value4;\r\n"
  360. "\r\n";
  361. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  362. }
  363. void HttpRequestTest::testCreateRequest_query()
  364. {
  365. SharedHandle<Request> request(new Request());
  366. request->setUrl("http://localhost/wiki?id=9ad5109a-b8a5-4edf-9373-56a1c34ae138");
  367. HttpRequest httpRequest;
  368. httpRequest.disableContentEncoding();
  369. httpRequest.setRequest(request);
  370. std::string expectedText =
  371. "GET /wiki?id=9ad5109a-b8a5-4edf-9373-56a1c34ae138 HTTP/1.1\r\n"
  372. "User-Agent: aria2\r\n"
  373. "Accept: */*\r\n"
  374. "Host: localhost\r\n"
  375. "Pragma: no-cache\r\n"
  376. "Cache-Control: no-cache\r\n"
  377. "Connection: close\r\n"
  378. "\r\n";
  379. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  380. }
  381. void HttpRequestTest::testCreateProxyRequest()
  382. {
  383. SharedHandle<Request> request(new Request());
  384. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  385. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  386. SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p));
  387. HttpRequest httpRequest;
  388. httpRequest.setRequest(request);
  389. httpRequest.setSegment(segment);
  390. request->supportsPersistentConnection(true);
  391. std::string expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  392. "User-Agent: aria2\r\n"
  393. "Host: localhost:80\r\n"
  394. "Proxy-Connection: close\r\n"
  395. "\r\n";
  396. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  397. // adds Keep-Alive header.
  398. request->setKeepAliveHint(true);
  399. expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  400. "User-Agent: aria2\r\n"
  401. "Host: localhost:80\r\n"
  402. "Proxy-Connection: Keep-Alive\r\n"
  403. "\r\n";
  404. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  405. request->setKeepAliveHint(false);
  406. // pipelining also adds Keep-Alive header.
  407. request->setPipeliningHint(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. }
  415. void HttpRequestTest::testIsRangeSatisfied()
  416. {
  417. SharedHandle<Request> request(new Request());
  418. request->supportsPersistentConnection(true);
  419. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  420. request->setPipeliningHint(false); // default: false
  421. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  422. SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p));
  423. HttpRequest httpRequest;
  424. httpRequest.setRequest(request);
  425. httpRequest.setSegment(segment);
  426. SharedHandle<Range> range(new Range());
  427. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  428. p.reset(new Piece(1, 1024*1024));
  429. segment.reset(new PiecedSegment(1024*1024, p));
  430. httpRequest.setSegment(segment);
  431. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  432. uint64_t entityLength = segment->getSegmentLength()*10;
  433. range.reset(new Range(segment->getPosition(), 0, entityLength));
  434. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  435. httpRequest.setEntityLength(entityLength-1);
  436. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  437. httpRequest.setEntityLength(entityLength);
  438. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  439. request->setPipeliningHint(true);
  440. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  441. range.reset(new Range(segment->getPosition(),
  442. segment->getPosition()+segment->getLength()-1,
  443. entityLength));
  444. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  445. range.reset(new Range(0, segment->getPosition()+segment->getLength()-1,
  446. entityLength));
  447. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  448. }
  449. void HttpRequestTest::testUserAgent()
  450. {
  451. Option option;
  452. SharedHandle<AuthConfigFactory> authConfigFactory
  453. (new AuthConfigFactory(&option));
  454. SingletonHolder<SharedHandle<AuthConfigFactory> >::instance(authConfigFactory);
  455. SharedHandle<Request> request(new Request());
  456. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  457. SharedHandle<Piece> p(new Piece(0, 1024));
  458. SharedHandle<Segment> segment(new PiecedSegment(1024, p));
  459. HttpRequest httpRequest;
  460. httpRequest.disableContentEncoding();
  461. httpRequest.setRequest(request);
  462. httpRequest.setSegment(segment);
  463. httpRequest.setUserAgent("aria2 (Linux)");
  464. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  465. "User-Agent: aria2 (Linux)\r\n"
  466. "Accept: */*\r\n"
  467. "Host: localhost:8080\r\n"
  468. "Pragma: no-cache\r\n"
  469. "Cache-Control: no-cache\r\n"
  470. "Connection: close\r\n"
  471. "\r\n";
  472. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  473. std::string expectedTextForProxy = "CONNECT localhost:8080 HTTP/1.1\r\n"
  474. "User-Agent: aria2 (Linux)\r\n"
  475. "Host: localhost:8080\r\n"
  476. "Proxy-Connection: close\r\n"
  477. "\r\n";
  478. CPPUNIT_ASSERT_EQUAL(expectedTextForProxy, httpRequest.createProxyRequest());
  479. }
  480. void HttpRequestTest::testAddHeader()
  481. {
  482. SharedHandle<Request> request(new Request());
  483. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  484. HttpRequest httpRequest;
  485. httpRequest.disableContentEncoding();
  486. httpRequest.setRequest(request);
  487. httpRequest.addHeader("X-ARIA2: v0.13\nX-ARIA2-DISTRIBUTE: enabled\n");
  488. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  489. "User-Agent: aria2\r\n"
  490. "Accept: */*\r\n"
  491. "Host: localhost\r\n"
  492. "Pragma: no-cache\r\n"
  493. "Cache-Control: no-cache\r\n"
  494. "Connection: close\r\n"
  495. "X-ARIA2: v0.13\r\n"
  496. "X-ARIA2-DISTRIBUTE: enabled\r\n"
  497. "\r\n";
  498. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  499. }
  500. void HttpRequestTest::testAddAcceptType()
  501. {
  502. std::string acceptTypes[] = { "cream/custard",
  503. "muffin/chocolate" };
  504. SharedHandle<Request> request(new Request());
  505. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  506. HttpRequest httpRequest;
  507. httpRequest.disableContentEncoding();
  508. httpRequest.setRequest(request);
  509. httpRequest.addAcceptType(&acceptTypes[0], &acceptTypes[arrayLength(acceptTypes)]);
  510. std::string expectedText =
  511. "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  512. "User-Agent: aria2\r\n"
  513. "Accept: */*,cream/custard,muffin/chocolate\r\n"
  514. "Host: localhost\r\n"
  515. "Pragma: no-cache\r\n"
  516. "Cache-Control: no-cache\r\n"
  517. "Connection: close\r\n"
  518. "\r\n";
  519. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  520. }
  521. void HttpRequestTest::testEnableAcceptEncoding()
  522. {
  523. SharedHandle<Request> request(new Request());
  524. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  525. HttpRequest httpRequest;
  526. httpRequest.setRequest(request);
  527. std::string acceptEncodings;
  528. #ifdef HAVE_LIBZ
  529. acceptEncodings += "deflate, gzip";
  530. #endif // HAVE_LIBZ
  531. std::string expectedText =
  532. "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  533. "User-Agent: aria2\r\n"
  534. "Accept: */*\r\n";
  535. if(!acceptEncodings.empty()) {
  536. expectedText += "Accept-Encoding: "+acceptEncodings+"\r\n";
  537. }
  538. expectedText +=
  539. "Host: localhost\r\n"
  540. "Pragma: no-cache\r\n"
  541. "Cache-Control: no-cache\r\n"
  542. "Connection: close\r\n"
  543. "\r\n";
  544. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  545. }
  546. } // namespace aria2