HttpRequestTest.cc 20 KB

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