HttpRequestTest.cc 16 KB

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