HttpRequestTest.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. #include "HttpRequest.h"
  2. #include "prefs.h"
  3. #include "RequestFactory.h"
  4. #include "PiecedSegment.h"
  5. #include "Piece.h"
  6. #include <cppunit/extensions/HelperMacros.h>
  7. using namespace std;
  8. class HttpRequestTest : public CppUnit::TestFixture {
  9. CPPUNIT_TEST_SUITE(HttpRequestTest);
  10. CPPUNIT_TEST(testGetStartByte);
  11. CPPUNIT_TEST(testGetEndByte);
  12. CPPUNIT_TEST(testCreateRequest);
  13. CPPUNIT_TEST(testCreateRequest_ftp);
  14. CPPUNIT_TEST(testCreateRequest_with_cookie);
  15. CPPUNIT_TEST(testCreateProxyRequest);
  16. CPPUNIT_TEST(testIsRangeSatisfied);
  17. CPPUNIT_TEST(testUserAgent);
  18. CPPUNIT_TEST_SUITE_END();
  19. private:
  20. public:
  21. void setUp() {}
  22. void testGetStartByte();
  23. void testGetEndByte();
  24. void testCreateRequest();
  25. void testCreateRequest_ftp();
  26. void testCreateRequest_with_cookie();
  27. void testCreateProxyRequest();
  28. void testIsRangeSatisfied();
  29. void testUserAgent();
  30. };
  31. CPPUNIT_TEST_SUITE_REGISTRATION( HttpRequestTest );
  32. void HttpRequestTest::testGetStartByte()
  33. {
  34. HttpRequest httpRequest;
  35. SegmentHandle segment = new PiecedSegment(1024, new Piece(1, 1024));
  36. CPPUNIT_ASSERT_EQUAL((int64_t)0, httpRequest.getStartByte());
  37. httpRequest.setSegment(segment);
  38. CPPUNIT_ASSERT_EQUAL((int64_t)1024, httpRequest.getStartByte());
  39. }
  40. void HttpRequestTest::testGetEndByte()
  41. {
  42. int32_t index = 1;
  43. int32_t length = 1024*1024-1024;
  44. int32_t segmentLength = 1024*1024;
  45. int32_t writtenLength = 1024;
  46. HttpRequest httpRequest;
  47. SegmentHandle segment = new PiecedSegment(segmentLength,
  48. new Piece(index, length));
  49. CPPUNIT_ASSERT_EQUAL((int64_t)0, httpRequest.getEndByte());
  50. httpRequest.setSegment(segment);
  51. CPPUNIT_ASSERT_EQUAL((int64_t)0, httpRequest.getEndByte());
  52. RequestHandle request = new Request();
  53. request->setKeepAlive(true);
  54. httpRequest.setRequest(request);
  55. CPPUNIT_ASSERT_EQUAL((int64_t)segmentLength*index+length-1,
  56. httpRequest.getEndByte());
  57. request->setKeepAlive(false);
  58. CPPUNIT_ASSERT_EQUAL((int64_t)0, httpRequest.getEndByte());
  59. }
  60. void HttpRequestTest::testCreateRequest()
  61. {
  62. SharedHandle<Option> option = new Option();
  63. option->put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
  64. option->put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  65. option->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  66. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  67. option->put(PREF_HTTP_USER, "aria2user");
  68. option->put(PREF_HTTP_PASSWD, "aria2passwd");
  69. option->put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  70. option->put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  71. RequestFactory requestFactory;
  72. requestFactory.setOption(option.get());
  73. RequestHandle request = requestFactory.createRequest();
  74. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  75. SegmentHandle segment = new PiecedSegment(1024, new Piece(0, 1024));
  76. HttpRequest httpRequest;
  77. httpRequest.setRequest(request);
  78. httpRequest.setSegment(segment);
  79. request->setKeepAlive(true);
  80. string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  81. "User-Agent: aria2\r\n"
  82. "Accept: */*\r\n"
  83. "Host: localhost:8080\r\n"
  84. "Pragma: no-cache\r\n"
  85. "Cache-Control: no-cache\r\n"
  86. "Range: bytes=0-1023\r\n"
  87. "\r\n";
  88. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  89. request->setKeepAlive(false);
  90. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  91. "User-Agent: aria2\r\n"
  92. "Accept: */*\r\n"
  93. "Host: localhost:8080\r\n"
  94. "Pragma: no-cache\r\n"
  95. "Cache-Control: no-cache\r\n"
  96. "Connection: close\r\n"
  97. "\r\n";
  98. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  99. segment = new PiecedSegment(1024*1024, new Piece(1, 1024*1024));
  100. httpRequest.setSegment(segment);
  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. "Range: bytes=1048576-\r\n"
  109. "\r\n";
  110. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  111. request->setKeepAlive(true);
  112. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  113. "User-Agent: aria2\r\n"
  114. "Accept: */*\r\n"
  115. "Host: localhost:8080\r\n"
  116. "Pragma: no-cache\r\n"
  117. "Cache-Control: no-cache\r\n"
  118. "Range: bytes=1048576-2097151\r\n"
  119. "\r\n";
  120. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  121. request->setKeepAlive(false);
  122. request->redirectUrl("http://localhost:8080/archives/download/aria2-1.0.0.tar.bz2");
  123. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  124. "User-Agent: aria2\r\n"
  125. "Accept: */*\r\n"
  126. "Host: localhost:8080\r\n"
  127. "Pragma: no-cache\r\n"
  128. "Cache-Control: no-cache\r\n"
  129. "Connection: close\r\n"
  130. "Range: bytes=1048576-\r\n"
  131. "\r\n";
  132. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  133. request->resetUrl();
  134. segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  135. httpRequest.setSegment(segment);
  136. // enable http auth
  137. option->put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  138. httpRequest.configure(option.get());
  139. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  140. "User-Agent: aria2\r\n"
  141. "Accept: */*\r\n"
  142. "Host: localhost:8080\r\n"
  143. "Pragma: no-cache\r\n"
  144. "Cache-Control: no-cache\r\n"
  145. "Connection: close\r\n"
  146. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  147. "\r\n";
  148. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  149. // enable http proxy auth
  150. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  151. httpRequest.configure(option.get());
  152. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  153. "User-Agent: aria2\r\n"
  154. "Accept: */*\r\n"
  155. "Host: localhost:8080\r\n"
  156. "Pragma: no-cache\r\n"
  157. "Cache-Control: no-cache\r\n"
  158. "Connection: close\r\n"
  159. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  160. "\r\n";
  161. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  162. option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  163. httpRequest.configure(option.get());
  164. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  165. "User-Agent: aria2\r\n"
  166. "Accept: */*\r\n"
  167. "Host: localhost:8080\r\n"
  168. "Pragma: no-cache\r\n"
  169. "Cache-Control: no-cache\r\n"
  170. "Connection: close\r\n"
  171. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  172. "\r\n";
  173. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  174. option->put(PREF_HTTP_PROXY_METHOD, V_GET);
  175. httpRequest.configure(option.get());
  176. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  177. "User-Agent: aria2\r\n"
  178. "Accept: */*\r\n"
  179. "Host: localhost:8080\r\n"
  180. "Pragma: no-cache\r\n"
  181. "Cache-Control: no-cache\r\n"
  182. "Connection: close\r\n"
  183. "Proxy-Connection: close\r\n"
  184. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  185. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  186. "\r\n";
  187. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  188. request->setKeepAlive(true);
  189. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  190. "User-Agent: aria2\r\n"
  191. "Accept: */*\r\n"
  192. "Host: localhost:8080\r\n"
  193. "Pragma: no-cache\r\n"
  194. "Cache-Control: no-cache\r\n"
  195. "Range: bytes=0-1048575\r\n"
  196. "Proxy-Connection: Keep-Alive\r\n"
  197. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  198. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  199. "\r\n";
  200. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  201. request->setKeepAlive(false);
  202. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  203. httpRequest.configure(option.get());
  204. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  205. "User-Agent: aria2\r\n"
  206. "Accept: */*\r\n"
  207. "Host: localhost:8080\r\n"
  208. "Pragma: no-cache\r\n"
  209. "Cache-Control: no-cache\r\n"
  210. "Connection: close\r\n"
  211. "Proxy-Connection: close\r\n"
  212. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  213. "\r\n";
  214. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  215. }
  216. void HttpRequestTest::testCreateRequest_ftp()
  217. {
  218. SharedHandle<Option> option = new Option();
  219. option->put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
  220. option->put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  221. option->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  222. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  223. option->put(PREF_HTTP_USER, "aria2user");
  224. option->put(PREF_HTTP_PASSWD, "aria2passwd");
  225. option->put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  226. option->put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  227. RequestFactory requestFactory;
  228. requestFactory.setOption(option.get());
  229. RequestHandle request = requestFactory.createRequest();
  230. request->setUrl("ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  231. HttpRequest httpRequest;
  232. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  233. httpRequest.setRequest(request);
  234. httpRequest.setSegment(segment);
  235. httpRequest.configure(option.get());
  236. string expectedText = "GET ftp://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. "\r\n";
  244. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  245. // How to enable HTTP proxy authorization in FTP download via HTTP proxy
  246. option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  247. option->put(PREF_HTTP_PROXY_METHOD, V_GET);
  248. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  249. httpRequest.configure(option.get());
  250. expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  251. "User-Agent: aria2\r\n"
  252. "Accept: */*\r\n"
  253. "Host: localhost:8080\r\n"
  254. "Pragma: no-cache\r\n"
  255. "Cache-Control: no-cache\r\n"
  256. "Connection: close\r\n"
  257. "Proxy-Connection: close\r\n"
  258. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  259. "\r\n";
  260. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  261. }
  262. void HttpRequestTest::testCreateRequest_with_cookie()
  263. {
  264. RequestHandle request = new Request();
  265. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  266. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  267. Cookie cookie1("name1", "value1", "/archives", "localhost", false);
  268. Cookie cookie2("name2", "value2", "/archives/download", "localhost", false);
  269. Cookie cookie3("name3", "value3", "/archives/download", "tt.localhost", false);
  270. Cookie cookie4("name4", "value4", "/archives/download", "tt.localhost", true);
  271. request->cookieBox->add(cookie1);
  272. request->cookieBox->add(cookie2);
  273. request->cookieBox->add(cookie3);
  274. request->cookieBox->add(cookie4);
  275. HttpRequest httpRequest;
  276. httpRequest.setRequest(request);
  277. httpRequest.setSegment(segment);
  278. string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  279. "User-Agent: aria2\r\n"
  280. "Accept: */*\r\n"
  281. "Host: localhost\r\n"
  282. "Pragma: no-cache\r\n"
  283. "Cache-Control: no-cache\r\n"
  284. "Connection: close\r\n"
  285. "Cookie: name1=value1;\r\n"
  286. "\r\n";
  287. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  288. request->setUrl("http://localhost/archives/download/aria2-1.0.0.tar.bz2");
  289. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  290. "User-Agent: aria2\r\n"
  291. "Accept: */*\r\n"
  292. "Host: localhost\r\n"
  293. "Pragma: no-cache\r\n"
  294. "Cache-Control: no-cache\r\n"
  295. "Connection: close\r\n"
  296. "Cookie: name1=value1;name2=value2;\r\n"
  297. "\r\n";
  298. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  299. request->setUrl("http://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  300. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  301. "User-Agent: aria2\r\n"
  302. "Accept: */*\r\n"
  303. "Host: tt.localhost\r\n"
  304. "Pragma: no-cache\r\n"
  305. "Cache-Control: no-cache\r\n"
  306. "Connection: close\r\n"
  307. "Cookie: name1=value1;name2=value2;name3=value3;\r\n"
  308. "\r\n";
  309. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  310. request->setUrl("https://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  311. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  312. "User-Agent: aria2\r\n"
  313. "Accept: */*\r\n"
  314. "Host: tt.localhost\r\n"
  315. "Pragma: no-cache\r\n"
  316. "Cache-Control: no-cache\r\n"
  317. "Connection: close\r\n"
  318. "Cookie: name1=value1;name2=value2;name3=value3;name4=value4;\r\n"
  319. "\r\n";
  320. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  321. }
  322. void HttpRequestTest::testCreateProxyRequest()
  323. {
  324. RequestHandle request = new Request();
  325. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  326. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  327. HttpRequest httpRequest;
  328. httpRequest.setRequest(request);
  329. httpRequest.setSegment(segment);
  330. string expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  331. "User-Agent: aria2\r\n"
  332. "Host: localhost:80\r\n"
  333. "Proxy-Connection: close\r\n"
  334. "\r\n";
  335. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  336. request->setKeepAlive(true);
  337. expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  338. "User-Agent: aria2\r\n"
  339. "Host: localhost:80\r\n"
  340. "Proxy-Connection: Keep-Alive\r\n"
  341. "\r\n";
  342. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  343. }
  344. void HttpRequestTest::testIsRangeSatisfied()
  345. {
  346. RequestHandle request = new Request();
  347. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  348. request->setKeepAlive(false);
  349. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  350. HttpRequest httpRequest;
  351. httpRequest.setRequest(request);
  352. httpRequest.setSegment(segment);
  353. RangeHandle range = new Range(0, 0, 0);
  354. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  355. segment = new PiecedSegment(1024*1024, new Piece(1, 1024*1024));
  356. httpRequest.setSegment(segment);
  357. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  358. int64_t entityLength = segment->getSegmentLength()*10;
  359. range = new Range(segment->getPosition(), 0, entityLength);
  360. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  361. httpRequest.setEntityLength(entityLength-1);
  362. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  363. httpRequest.setEntityLength(entityLength);
  364. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  365. request->setKeepAlive(true);
  366. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  367. range = new Range(segment->getPosition(),
  368. segment->getPosition()+segment->getLength()-1,
  369. entityLength);
  370. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  371. range = new Range(0, segment->getPosition()+segment->getLength()-1,
  372. entityLength);
  373. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  374. }
  375. void HttpRequestTest::testUserAgent()
  376. {
  377. SharedHandle<Option> option = new Option();
  378. RequestFactory requestFactory;
  379. requestFactory.setOption(option.get());
  380. RequestHandle request = requestFactory.createRequest();
  381. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  382. SegmentHandle segment = new PiecedSegment(1024, new Piece(0, 1024));
  383. HttpRequest httpRequest;
  384. httpRequest.setRequest(request);
  385. httpRequest.setSegment(segment);
  386. httpRequest.setUserAgent("aria2 (Linux)");
  387. string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  388. "User-Agent: aria2 (Linux)\r\n"
  389. "Accept: */*\r\n"
  390. "Host: localhost:8080\r\n"
  391. "Pragma: no-cache\r\n"
  392. "Cache-Control: no-cache\r\n"
  393. "Connection: close\r\n"
  394. "\r\n";
  395. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  396. string expectedTextForProxy = "CONNECT localhost:8080 HTTP/1.1\r\n"
  397. "User-Agent: aria2 (Linux)\r\n"
  398. "Host: localhost:8080\r\n"
  399. "Proxy-Connection: close\r\n"
  400. "\r\n";
  401. CPPUNIT_ASSERT_EQUAL(expectedTextForProxy, httpRequest.createProxyRequest());
  402. }