HttpRequestTest.cc 19 KB

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