HttpRequestTest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. AuthConfigHandle authConfig = new AuthConfig();
  61. request->setUserDefinedAuthConfig(authConfig);
  62. SegmentHandle segment = new Segment();
  63. HttpRequest httpRequest;
  64. httpRequest.setRequest(request);
  65. httpRequest.setSegment(segment);
  66. string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  67. "User-Agent: aria2\r\n"
  68. "Accept: */*\r\n"
  69. "Host: localhost:8080\r\n"
  70. "Pragma: no-cache\r\n"
  71. "Cache-Control: no-cache\r\n"
  72. "\r\n";
  73. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  74. request->setKeepAlive(false);
  75. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  76. "User-Agent: aria2\r\n"
  77. "Accept: */*\r\n"
  78. "Host: localhost:8080\r\n"
  79. "Pragma: no-cache\r\n"
  80. "Cache-Control: no-cache\r\n"
  81. "Connection: close\r\n"
  82. "\r\n";
  83. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  84. segment->index = 1;
  85. segment->length = 1024*1024;
  86. segment->segmentLength = 1024*1024;
  87. segment->writtenLength = 0;
  88. 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. "Connection: close\r\n"
  95. "Range: bytes=1048576-\r\n"
  96. "\r\n";
  97. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  98. request->setKeepAlive(true);
  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. "Range: bytes=1048576-2097151\r\n"
  106. "\r\n";
  107. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  108. httpRequest.setSegment(new Segment());
  109. request->redirectUrl("http://localhost:8080/archives/download/aria2-1.0.0.tar.bz2");
  110. expectedText = "GET /archives/download/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. "Referer: http://localhost:8080/archives/aria2-1.0.0.tar.bz2\r\n"
  117. "\r\n";
  118. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  119. request->resetUrl();
  120. SharedHandle<Option> option = new Option();
  121. option->put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
  122. option->put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  123. option->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  124. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  125. option->put(PREF_HTTP_USER, "aria2user");
  126. option->put(PREF_HTTP_PASSWD, "aria2passwd");
  127. option->put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  128. option->put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  129. authConfig->configure(option.get());
  130. httpRequest.configure(option.get());
  131. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  132. "User-Agent: aria2\r\n"
  133. "Accept: */*\r\n"
  134. "Host: localhost:8080\r\n"
  135. "Pragma: no-cache\r\n"
  136. "Cache-Control: no-cache\r\n"
  137. "\r\n";
  138. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  139. option->put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  140. httpRequest.configure(option.get());
  141. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  142. "User-Agent: aria2\r\n"
  143. "Accept: */*\r\n"
  144. "Host: localhost:8080\r\n"
  145. "Pragma: no-cache\r\n"
  146. "Cache-Control: no-cache\r\n"
  147. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  148. "\r\n";
  149. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  150. option->put(PREF_HTTP_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. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  159. "\r\n";
  160. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  161. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  162. httpRequest.configure(option.get());
  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. "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.get());
  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. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  181. "\r\n";
  182. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  183. option->put(PREF_HTTP_PROXY_METHOD, V_GET);
  184. httpRequest.configure(option.get());
  185. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  186. "User-Agent: aria2\r\n"
  187. "Accept: */*\r\n"
  188. "Host: localhost:8080\r\n"
  189. "Pragma: no-cache\r\n"
  190. "Cache-Control: no-cache\r\n"
  191. "Proxy-Connection: close\r\n"
  192. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  193. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  194. "\r\n";
  195. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  196. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  197. httpRequest.configure(option.get());
  198. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  199. "User-Agent: aria2\r\n"
  200. "Accept: */*\r\n"
  201. "Host: localhost:8080\r\n"
  202. "Pragma: no-cache\r\n"
  203. "Cache-Control: no-cache\r\n"
  204. "Proxy-Connection: close\r\n"
  205. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  206. "\r\n";
  207. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  208. }
  209. void HttpRequestTest::testCreateRequest_ftp()
  210. {
  211. RequestHandle request = new Request();
  212. request->setUrl("ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  213. AuthConfigHandle authConfig = new AuthConfig();
  214. request->setUserDefinedAuthConfig(authConfig);
  215. SegmentHandle segment = new Segment();
  216. HttpRequest httpRequest;
  217. httpRequest.setRequest(request);
  218. httpRequest.setSegment(segment);
  219. SharedHandle<Option> option = new Option();
  220. option->put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
  221. option->put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  222. option->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  223. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  224. option->put(PREF_HTTP_USER, "aria2user");
  225. option->put(PREF_HTTP_PASSWD, "aria2passwd");
  226. option->put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  227. option->put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  228. httpRequest.configure(option.get());
  229. authConfig->configure(option.get());
  230. string expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  231. "User-Agent: aria2\r\n"
  232. "Accept: */*\r\n"
  233. "Host: localhost:8080\r\n"
  234. "Pragma: no-cache\r\n"
  235. "Cache-Control: no-cache\r\n"
  236. "\r\n";
  237. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  238. // How to enable HTTP proxy authorization in FTP download via HTTP proxy
  239. option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  240. option->put(PREF_HTTP_PROXY_METHOD, V_GET);
  241. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  242. httpRequest.configure(option.get());
  243. expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  244. "User-Agent: aria2\r\n"
  245. "Accept: */*\r\n"
  246. "Host: localhost:8080\r\n"
  247. "Pragma: no-cache\r\n"
  248. "Cache-Control: no-cache\r\n"
  249. "Proxy-Connection: close\r\n"
  250. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  251. "\r\n";
  252. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  253. }
  254. void HttpRequestTest::testCreateRequest_with_cookie()
  255. {
  256. RequestHandle request = new Request();
  257. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  258. SegmentHandle segment = new Segment();
  259. Cookie cookie1("name1", "value1", "2007/1/1", "/archives", "localhost", false);
  260. Cookie cookie2("name2", "value2", "2007/1/1", "/archives/download", "localhost", false);
  261. Cookie cookie3("name3", "value3", "2007/1/1", "/archives/download", "tt.localhost", false);
  262. Cookie cookie4("name4", "value4", "2007/1/1", "/archives/download", "tt.localhost", true);
  263. request->cookieBox->add(cookie1);
  264. request->cookieBox->add(cookie2);
  265. request->cookieBox->add(cookie3);
  266. request->cookieBox->add(cookie4);
  267. HttpRequest httpRequest;
  268. httpRequest.setRequest(request);
  269. httpRequest.setSegment(segment);
  270. string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  271. "User-Agent: aria2\r\n"
  272. "Accept: */*\r\n"
  273. "Host: localhost\r\n"
  274. "Pragma: no-cache\r\n"
  275. "Cache-Control: no-cache\r\n"
  276. "Cookie: name1=value1;\r\n"
  277. "\r\n";
  278. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  279. request->setUrl("http://localhost/archives/download/aria2-1.0.0.tar.bz2");
  280. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  281. "User-Agent: aria2\r\n"
  282. "Accept: */*\r\n"
  283. "Host: localhost\r\n"
  284. "Pragma: no-cache\r\n"
  285. "Cache-Control: no-cache\r\n"
  286. "Cookie: name1=value1;name2=value2;\r\n"
  287. "\r\n";
  288. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  289. request->setUrl("http://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  290. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  291. "User-Agent: aria2\r\n"
  292. "Accept: */*\r\n"
  293. "Host: tt.localhost\r\n"
  294. "Pragma: no-cache\r\n"
  295. "Cache-Control: no-cache\r\n"
  296. "Cookie: name1=value1;name2=value2;name3=value3;\r\n"
  297. "\r\n";
  298. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  299. request->setUrl("https://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. "Cookie: name1=value1;name2=value2;name3=value3;name4=value4;\r\n"
  307. "\r\n";
  308. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  309. }
  310. void HttpRequestTest::testCreateProxyRequest()
  311. {
  312. RequestHandle request = new Request();
  313. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  314. SegmentHandle segment = new Segment();
  315. HttpRequest httpRequest;
  316. httpRequest.setRequest(request);
  317. httpRequest.setSegment(segment);
  318. string expectedText = "CONNECT localhost:8080 HTTP/1.1\r\n"
  319. "User-Agent: aria2\r\n"
  320. "Proxy-Connection: close\r\n"
  321. "Host: localhost:8080\r\n"
  322. "\r\n";
  323. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  324. }
  325. void HttpRequestTest::testIsRangeSatisfied()
  326. {
  327. RequestHandle request = new Request();
  328. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  329. request->setKeepAlive(false);
  330. SegmentHandle segment = new Segment();
  331. HttpRequest httpRequest;
  332. httpRequest.setRequest(request);
  333. httpRequest.setSegment(segment);
  334. RangeHandle range = new Range(0, 0, 0);
  335. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  336. segment->index = 1;
  337. segment->length = 1024*1024;
  338. segment->segmentLength = 1024*1024;
  339. segment->writtenLength = 0;
  340. int64_t entityLength = segment->segmentLength*10;
  341. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  342. range = new Range(segment->getPosition(), 0, entityLength);
  343. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  344. httpRequest.setEntityLength(entityLength-1);
  345. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  346. httpRequest.setEntityLength(entityLength);
  347. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  348. request->setKeepAlive(true);
  349. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  350. range = new Range(segment->getPosition(),
  351. segment->getPosition()+segment->length-1, entityLength);
  352. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  353. range = new Range(0, segment->getPosition()+segment->length-1, entityLength);
  354. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  355. }