HttpRequestTest.cc 17 KB

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