HttpRequestTest.cc 14 KB

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