HttpRequestTest.cc 18 KB

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