HttpRequestTest.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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(0LL, httpRequest.getStartByte());
  49. httpRequest.setSegment(segment);
  50. CPPUNIT_ASSERT_EQUAL(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(0LL, httpRequest.getEndByte());
  61. httpRequest.setSegment(segment);
  62. CPPUNIT_ASSERT_EQUAL(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(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 clears persistent connection falg
  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. "Connection: close\r\n"
  147. "Range: bytes=1048576-\r\n"
  148. "\r\n";
  149. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  150. request->supportsPersistentConnection(true);
  151. request->setPipeliningHint(false);
  152. // this only removes "Connection: close".
  153. request->setKeepAliveHint(true);
  154. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  155. "User-Agent: aria2\r\n"
  156. "Accept: */*\r\n"
  157. "Host: localhost:8080\r\n"
  158. "Pragma: no-cache\r\n"
  159. "Cache-Control: no-cache\r\n"
  160. "Range: bytes=1048576-\r\n"
  161. "\r\n";
  162. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  163. request->setKeepAliveHint(false);
  164. request->resetUrl();
  165. p.reset(new Piece(0, 1024*1024));
  166. segment.reset(new PiecedSegment(1024*1024, p));
  167. httpRequest.setSegment(segment);
  168. // enable http auth
  169. option.put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  170. httpRequest.configure(&option);
  171. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  172. "User-Agent: aria2\r\n"
  173. "Accept: */*\r\n"
  174. "Host: localhost:8080\r\n"
  175. "Pragma: no-cache\r\n"
  176. "Cache-Control: no-cache\r\n"
  177. "Connection: close\r\n"
  178. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  179. "\r\n";
  180. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  181. // enable http proxy auth
  182. option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  183. httpRequest.configure(&option);
  184. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  185. "User-Agent: aria2\r\n"
  186. "Accept: */*\r\n"
  187. "Host: localhost:8080\r\n"
  188. "Pragma: no-cache\r\n"
  189. "Cache-Control: no-cache\r\n"
  190. "Connection: close\r\n"
  191. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  192. "\r\n";
  193. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  194. option.put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  195. httpRequest.configure(&option);
  196. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  197. "User-Agent: aria2\r\n"
  198. "Accept: */*\r\n"
  199. "Host: localhost:8080\r\n"
  200. "Pragma: no-cache\r\n"
  201. "Cache-Control: no-cache\r\n"
  202. "Connection: close\r\n"
  203. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  204. "\r\n";
  205. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  206. option.put(PREF_HTTP_PROXY_METHOD, V_GET);
  207. httpRequest.configure(&option);
  208. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  209. "User-Agent: aria2\r\n"
  210. "Accept: */*\r\n"
  211. "Host: localhost:8080\r\n"
  212. "Pragma: no-cache\r\n"
  213. "Cache-Control: no-cache\r\n"
  214. "Connection: close\r\n"
  215. "Proxy-Connection: close\r\n"
  216. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  217. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  218. "\r\n";
  219. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  220. request->setPipeliningHint(true);
  221. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  222. "User-Agent: aria2\r\n"
  223. "Accept: */*\r\n"
  224. "Host: localhost:8080\r\n"
  225. "Pragma: no-cache\r\n"
  226. "Cache-Control: no-cache\r\n"
  227. "Range: bytes=0-1048575\r\n"
  228. "Proxy-Connection: Keep-Alive\r\n"
  229. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  230. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  231. "\r\n";
  232. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  233. request->setPipeliningHint(false);
  234. option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  235. httpRequest.configure(&option);
  236. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  237. "User-Agent: aria2\r\n"
  238. "Accept: */*\r\n"
  239. "Host: localhost:8080\r\n"
  240. "Pragma: no-cache\r\n"
  241. "Cache-Control: no-cache\r\n"
  242. "Connection: close\r\n"
  243. "Proxy-Connection: close\r\n"
  244. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  245. "\r\n";
  246. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  247. }
  248. void HttpRequestTest::testCreateRequest_ftp()
  249. {
  250. Option option;
  251. option.put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
  252. option.put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  253. option.put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  254. option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  255. option.put(PREF_HTTP_USER, "aria2user");
  256. option.put(PREF_HTTP_PASSWD, "aria2passwd");
  257. option.put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  258. option.put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  259. SharedHandle<AuthConfigFactory> authConfigFactory
  260. (new AuthConfigFactory(&option));
  261. SingletonHolder<SharedHandle<AuthConfigFactory> >::instance(authConfigFactory);
  262. SharedHandle<Request> request(new Request());
  263. request->setUrl("ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  264. HttpRequest httpRequest;
  265. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  266. SharedHandle<Segment> segment
  267. (new PiecedSegment(1024*1024, p));
  268. httpRequest.setRequest(request);
  269. httpRequest.setSegment(segment);
  270. httpRequest.configure(&option);
  271. std::string expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  272. "User-Agent: aria2\r\n"
  273. "Accept: */*\r\n"
  274. "Host: localhost:8080\r\n"
  275. "Pragma: no-cache\r\n"
  276. "Cache-Control: no-cache\r\n"
  277. "Connection: close\r\n"
  278. "\r\n";
  279. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  280. // How to enable HTTP proxy authorization in FTP download via HTTP proxy
  281. option.put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  282. option.put(PREF_HTTP_PROXY_METHOD, V_GET);
  283. option.put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  284. httpRequest.configure(&option);
  285. expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  286. "User-Agent: aria2\r\n"
  287. "Accept: */*\r\n"
  288. "Host: localhost:8080\r\n"
  289. "Pragma: no-cache\r\n"
  290. "Cache-Control: no-cache\r\n"
  291. "Connection: close\r\n"
  292. "Proxy-Connection: close\r\n"
  293. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  294. "\r\n";
  295. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  296. }
  297. void HttpRequestTest::testCreateRequest_with_cookie()
  298. {
  299. SharedHandle<Request> request(new Request());
  300. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  301. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  302. SharedHandle<Segment> segment
  303. (new PiecedSegment(1024*1024, p));
  304. Cookie cookie1("name1", "value1", "/archives", "localhost", false);
  305. Cookie cookie2("name2", "value2", "/archives/download", "localhost", false);
  306. Cookie cookie3("name3", "value3", "/archives/download", "tt.localhost", false);
  307. Cookie cookie4("name4", "value4", "/archives/download", "tt.localhost", true);
  308. request->cookieBox->add(cookie1);
  309. request->cookieBox->add(cookie2);
  310. request->cookieBox->add(cookie3);
  311. request->cookieBox->add(cookie4);
  312. HttpRequest httpRequest;
  313. httpRequest.setRequest(request);
  314. httpRequest.setSegment(segment);
  315. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  316. "User-Agent: aria2\r\n"
  317. "Accept: */*\r\n"
  318. "Host: localhost\r\n"
  319. "Pragma: no-cache\r\n"
  320. "Cache-Control: no-cache\r\n"
  321. "Connection: close\r\n"
  322. "Cookie: name1=value1;\r\n"
  323. "\r\n";
  324. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  325. request->setUrl("http://localhost/archives/download/aria2-1.0.0.tar.bz2");
  326. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  327. "User-Agent: aria2\r\n"
  328. "Accept: */*\r\n"
  329. "Host: localhost\r\n"
  330. "Pragma: no-cache\r\n"
  331. "Cache-Control: no-cache\r\n"
  332. "Connection: close\r\n"
  333. "Cookie: name1=value1;name2=value2;\r\n"
  334. "\r\n";
  335. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  336. request->setUrl("http://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  337. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  338. "User-Agent: aria2\r\n"
  339. "Accept: */*\r\n"
  340. "Host: tt.localhost\r\n"
  341. "Pragma: no-cache\r\n"
  342. "Cache-Control: no-cache\r\n"
  343. "Connection: close\r\n"
  344. "Cookie: name1=value1;name2=value2;name3=value3;\r\n"
  345. "\r\n";
  346. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  347. request->setUrl("https://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  348. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  349. "User-Agent: aria2\r\n"
  350. "Accept: */*\r\n"
  351. "Host: tt.localhost\r\n"
  352. "Pragma: no-cache\r\n"
  353. "Cache-Control: no-cache\r\n"
  354. "Connection: close\r\n"
  355. "Cookie: name1=value1;name2=value2;name3=value3;name4=value4;\r\n"
  356. "\r\n";
  357. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  358. }
  359. void HttpRequestTest::testCreateRequest_query()
  360. {
  361. SharedHandle<Request> request(new Request());
  362. request->setUrl("http://localhost/wiki?id=9ad5109a-b8a5-4edf-9373-56a1c34ae138");
  363. HttpRequest httpRequest;
  364. httpRequest.setRequest(request);
  365. std::string expectedText =
  366. "GET /wiki?id=9ad5109a-b8a5-4edf-9373-56a1c34ae138 HTTP/1.1\r\n"
  367. "User-Agent: aria2\r\n"
  368. "Accept: */*\r\n"
  369. "Host: localhost\r\n"
  370. "Pragma: no-cache\r\n"
  371. "Cache-Control: no-cache\r\n"
  372. "Connection: close\r\n"
  373. "\r\n";
  374. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  375. }
  376. void HttpRequestTest::testCreateProxyRequest()
  377. {
  378. SharedHandle<Request> request(new Request());
  379. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  380. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  381. SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p));
  382. HttpRequest httpRequest;
  383. httpRequest.setRequest(request);
  384. httpRequest.setSegment(segment);
  385. request->supportsPersistentConnection(true);
  386. std::string expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  387. "User-Agent: aria2\r\n"
  388. "Host: localhost:80\r\n"
  389. "Proxy-Connection: close\r\n"
  390. "\r\n";
  391. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  392. // adds Keep-Alive header.
  393. request->setKeepAliveHint(true);
  394. expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  395. "User-Agent: aria2\r\n"
  396. "Host: localhost:80\r\n"
  397. "Proxy-Connection: Keep-Alive\r\n"
  398. "\r\n";
  399. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  400. request->setKeepAliveHint(false);
  401. // pipelining also adds Keep-Alive header.
  402. request->setPipeliningHint(true);
  403. expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  404. "User-Agent: aria2\r\n"
  405. "Host: localhost:80\r\n"
  406. "Proxy-Connection: Keep-Alive\r\n"
  407. "\r\n";
  408. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  409. }
  410. void HttpRequestTest::testIsRangeSatisfied()
  411. {
  412. SharedHandle<Request> request(new Request());
  413. request->supportsPersistentConnection(true);
  414. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  415. request->setPipeliningHint(false); // default: false
  416. SharedHandle<Piece> p(new Piece(0, 1024*1024));
  417. SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p));
  418. HttpRequest httpRequest;
  419. httpRequest.setRequest(request);
  420. httpRequest.setSegment(segment);
  421. SharedHandle<Range> range(new Range());
  422. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  423. p.reset(new Piece(1, 1024*1024));
  424. segment.reset(new PiecedSegment(1024*1024, p));
  425. httpRequest.setSegment(segment);
  426. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  427. uint64_t entityLength = segment->getSegmentLength()*10;
  428. range.reset(new Range(segment->getPosition(), 0, entityLength));
  429. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  430. httpRequest.setEntityLength(entityLength-1);
  431. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  432. httpRequest.setEntityLength(entityLength);
  433. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  434. request->setPipeliningHint(true);
  435. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  436. range.reset(new Range(segment->getPosition(),
  437. segment->getPosition()+segment->getLength()-1,
  438. entityLength));
  439. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  440. range.reset(new Range(0, segment->getPosition()+segment->getLength()-1,
  441. entityLength));
  442. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  443. }
  444. void HttpRequestTest::testUserAgent()
  445. {
  446. Option option;
  447. SharedHandle<AuthConfigFactory> authConfigFactory
  448. (new AuthConfigFactory(&option));
  449. SingletonHolder<SharedHandle<AuthConfigFactory> >::instance(authConfigFactory);
  450. SharedHandle<Request> request(new Request());
  451. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  452. SharedHandle<Piece> p(new Piece(0, 1024));
  453. SharedHandle<Segment> segment(new PiecedSegment(1024, p));
  454. HttpRequest httpRequest;
  455. httpRequest.setRequest(request);
  456. httpRequest.setSegment(segment);
  457. httpRequest.setUserAgent("aria2 (Linux)");
  458. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  459. "User-Agent: aria2 (Linux)\r\n"
  460. "Accept: */*\r\n"
  461. "Host: localhost:8080\r\n"
  462. "Pragma: no-cache\r\n"
  463. "Cache-Control: no-cache\r\n"
  464. "Connection: close\r\n"
  465. "\r\n";
  466. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  467. std::string expectedTextForProxy = "CONNECT localhost:8080 HTTP/1.1\r\n"
  468. "User-Agent: aria2 (Linux)\r\n"
  469. "Host: localhost:8080\r\n"
  470. "Proxy-Connection: close\r\n"
  471. "\r\n";
  472. CPPUNIT_ASSERT_EQUAL(expectedTextForProxy, httpRequest.createProxyRequest());
  473. }
  474. void HttpRequestTest::testAddHeader()
  475. {
  476. SharedHandle<Request> request(new Request());
  477. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  478. HttpRequest httpRequest;
  479. httpRequest.setRequest(request);
  480. httpRequest.addHeader("X-ARIA2: v0.13\nX-ARIA2-DISTRIBUTE: enabled\n");
  481. std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  482. "User-Agent: aria2\r\n"
  483. "Accept: */*\r\n"
  484. "Host: localhost\r\n"
  485. "Pragma: no-cache\r\n"
  486. "Cache-Control: no-cache\r\n"
  487. "Connection: close\r\n"
  488. "X-ARIA2: v0.13\r\n"
  489. "X-ARIA2-DISTRIBUTE: enabled\r\n"
  490. "\r\n";
  491. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  492. }
  493. void HttpRequestTest::testAddAcceptType()
  494. {
  495. std::string acceptTypes[] = { "cream/custard",
  496. "muffin/chocolate" };
  497. SharedHandle<Request> request(new Request());
  498. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  499. HttpRequest httpRequest;
  500. httpRequest.setRequest(request);
  501. httpRequest.addAcceptType(&acceptTypes[0], &acceptTypes[arrayLength(acceptTypes)]);
  502. std::string expectedText =
  503. "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  504. "User-Agent: aria2\r\n"
  505. "Accept: */*,cream/custard,muffin/chocolate\r\n"
  506. "Host: localhost\r\n"
  507. "Pragma: no-cache\r\n"
  508. "Cache-Control: no-cache\r\n"
  509. "Connection: close\r\n"
  510. "\r\n";
  511. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  512. }
  513. } // namespace aria2