HttpRequestTest.cc 14 KB

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