HttpRequestTest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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_SUITE_END();
  18. private:
  19. public:
  20. void setUp() {}
  21. void testGetStartByte();
  22. void testGetEndByte();
  23. void testCreateRequest();
  24. void testCreateRequest_ftp();
  25. void testCreateRequest_with_cookie();
  26. void testCreateProxyRequest();
  27. void testIsRangeSatisfied();
  28. };
  29. CPPUNIT_TEST_SUITE_REGISTRATION( HttpRequestTest );
  30. void HttpRequestTest::testGetStartByte()
  31. {
  32. HttpRequest httpRequest;
  33. SegmentHandle segment = new PiecedSegment(1024, new Piece(1, 1024));
  34. CPPUNIT_ASSERT_EQUAL((int64_t)0, httpRequest.getStartByte());
  35. httpRequest.setSegment(segment);
  36. CPPUNIT_ASSERT_EQUAL((int64_t)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 PiecedSegment(segmentLength,
  46. new Piece(index, length));
  47. CPPUNIT_ASSERT_EQUAL((int64_t)0, httpRequest.getEndByte());
  48. httpRequest.setSegment(segment);
  49. CPPUNIT_ASSERT_EQUAL((int64_t)0, 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 PiecedSegment(1024, new Piece(0, 1024));
  74. HttpRequest httpRequest;
  75. httpRequest.setRequest(request);
  76. httpRequest.setSegment(segment);
  77. request->setKeepAlive(true);
  78. string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  79. "User-Agent: aria2\r\n"
  80. "Accept: */*\r\n"
  81. "Host: localhost:8080\r\n"
  82. "Pragma: no-cache\r\n"
  83. "Cache-Control: no-cache\r\n"
  84. "Range: bytes=0-1023\r\n"
  85. "\r\n";
  86. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  87. request->setKeepAlive(false);
  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. "\r\n";
  96. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  97. segment = new PiecedSegment(1024*1024, new Piece(1, 1024*1024));
  98. httpRequest.setSegment(segment);
  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. request->setKeepAlive(false);
  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. "Connection: close\r\n"
  128. "Range: bytes=1048576-\r\n"
  129. "\r\n";
  130. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  131. request->resetUrl();
  132. segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  133. httpRequest.setSegment(segment);
  134. // enable http auth
  135. option->put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  136. httpRequest.configure(option.get());
  137. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  138. "User-Agent: aria2\r\n"
  139. "Accept: */*\r\n"
  140. "Host: localhost:8080\r\n"
  141. "Pragma: no-cache\r\n"
  142. "Cache-Control: no-cache\r\n"
  143. "Connection: close\r\n"
  144. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  145. "\r\n";
  146. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  147. // enable http proxy auth
  148. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  149. httpRequest.configure(option.get());
  150. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  151. "User-Agent: aria2\r\n"
  152. "Accept: */*\r\n"
  153. "Host: localhost:8080\r\n"
  154. "Pragma: no-cache\r\n"
  155. "Cache-Control: no-cache\r\n"
  156. "Connection: close\r\n"
  157. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  158. "\r\n";
  159. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  160. option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  161. httpRequest.configure(option.get());
  162. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  163. "User-Agent: aria2\r\n"
  164. "Accept: */*\r\n"
  165. "Host: localhost:8080\r\n"
  166. "Pragma: no-cache\r\n"
  167. "Cache-Control: no-cache\r\n"
  168. "Connection: close\r\n"
  169. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  170. "\r\n";
  171. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  172. option->put(PREF_HTTP_PROXY_METHOD, V_GET);
  173. httpRequest.configure(option.get());
  174. expectedText = "GET http://localhost:8080/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. "Connection: close\r\n"
  181. "Proxy-Connection: close\r\n"
  182. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  183. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  184. "\r\n";
  185. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  186. request->setKeepAlive(true);
  187. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  188. "User-Agent: aria2\r\n"
  189. "Accept: */*\r\n"
  190. "Host: localhost:8080\r\n"
  191. "Pragma: no-cache\r\n"
  192. "Cache-Control: no-cache\r\n"
  193. "Range: bytes=0-1048575\r\n"
  194. "Proxy-Connection: Keep-Alive\r\n"
  195. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  196. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  197. "\r\n";
  198. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  199. request->setKeepAlive(false);
  200. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  201. httpRequest.configure(option.get());
  202. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  203. "User-Agent: aria2\r\n"
  204. "Accept: */*\r\n"
  205. "Host: localhost:8080\r\n"
  206. "Pragma: no-cache\r\n"
  207. "Cache-Control: no-cache\r\n"
  208. "Connection: close\r\n"
  209. "Proxy-Connection: close\r\n"
  210. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  211. "\r\n";
  212. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  213. }
  214. void HttpRequestTest::testCreateRequest_ftp()
  215. {
  216. SharedHandle<Option> option = new Option();
  217. option->put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
  218. option->put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  219. option->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  220. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  221. option->put(PREF_HTTP_USER, "aria2user");
  222. option->put(PREF_HTTP_PASSWD, "aria2passwd");
  223. option->put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  224. option->put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  225. RequestFactory requestFactory;
  226. requestFactory.setOption(option.get());
  227. RequestHandle request = requestFactory.createRequest();
  228. request->setUrl("ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  229. HttpRequest httpRequest;
  230. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  231. httpRequest.setRequest(request);
  232. httpRequest.setSegment(segment);
  233. httpRequest.configure(option.get());
  234. string expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  235. "User-Agent: aria2\r\n"
  236. "Accept: */*\r\n"
  237. "Host: localhost:8080\r\n"
  238. "Pragma: no-cache\r\n"
  239. "Cache-Control: no-cache\r\n"
  240. "Connection: close\r\n"
  241. "\r\n";
  242. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  243. // How to enable HTTP proxy authorization in FTP download via HTTP proxy
  244. option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  245. option->put(PREF_HTTP_PROXY_METHOD, V_GET);
  246. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  247. httpRequest.configure(option.get());
  248. expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  249. "User-Agent: aria2\r\n"
  250. "Accept: */*\r\n"
  251. "Host: localhost:8080\r\n"
  252. "Pragma: no-cache\r\n"
  253. "Cache-Control: no-cache\r\n"
  254. "Connection: close\r\n"
  255. "Proxy-Connection: close\r\n"
  256. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  257. "\r\n";
  258. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  259. }
  260. void HttpRequestTest::testCreateRequest_with_cookie()
  261. {
  262. RequestHandle request = new Request();
  263. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  264. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  265. Cookie cookie1("name1", "value1", "/archives", "localhost", false);
  266. Cookie cookie2("name2", "value2", "/archives/download", "localhost", false);
  267. Cookie cookie3("name3", "value3", "/archives/download", "tt.localhost", false);
  268. Cookie cookie4("name4", "value4", "/archives/download", "tt.localhost", true);
  269. request->cookieBox->add(cookie1);
  270. request->cookieBox->add(cookie2);
  271. request->cookieBox->add(cookie3);
  272. request->cookieBox->add(cookie4);
  273. HttpRequest httpRequest;
  274. httpRequest.setRequest(request);
  275. httpRequest.setSegment(segment);
  276. string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  277. "User-Agent: aria2\r\n"
  278. "Accept: */*\r\n"
  279. "Host: localhost\r\n"
  280. "Pragma: no-cache\r\n"
  281. "Cache-Control: no-cache\r\n"
  282. "Connection: close\r\n"
  283. "Cookie: name1=value1;\r\n"
  284. "\r\n";
  285. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  286. request->setUrl("http://localhost/archives/download/aria2-1.0.0.tar.bz2");
  287. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  288. "User-Agent: aria2\r\n"
  289. "Accept: */*\r\n"
  290. "Host: localhost\r\n"
  291. "Pragma: no-cache\r\n"
  292. "Cache-Control: no-cache\r\n"
  293. "Connection: close\r\n"
  294. "Cookie: name1=value1;name2=value2;\r\n"
  295. "\r\n";
  296. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  297. request->setUrl("http://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  298. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  299. "User-Agent: aria2\r\n"
  300. "Accept: */*\r\n"
  301. "Host: tt.localhost\r\n"
  302. "Pragma: no-cache\r\n"
  303. "Cache-Control: no-cache\r\n"
  304. "Connection: close\r\n"
  305. "Cookie: name1=value1;name2=value2;name3=value3;\r\n"
  306. "\r\n";
  307. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  308. request->setUrl("https://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  309. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  310. "User-Agent: aria2\r\n"
  311. "Accept: */*\r\n"
  312. "Host: tt.localhost\r\n"
  313. "Pragma: no-cache\r\n"
  314. "Cache-Control: no-cache\r\n"
  315. "Connection: close\r\n"
  316. "Cookie: name1=value1;name2=value2;name3=value3;name4=value4;\r\n"
  317. "\r\n";
  318. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  319. }
  320. void HttpRequestTest::testCreateProxyRequest()
  321. {
  322. RequestHandle request = new Request();
  323. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  324. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  325. HttpRequest httpRequest;
  326. httpRequest.setRequest(request);
  327. httpRequest.setSegment(segment);
  328. string expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  329. "User-Agent: aria2\r\n"
  330. "Host: localhost:80\r\n"
  331. "Proxy-Connection: close\r\n"
  332. "\r\n";
  333. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  334. request->setKeepAlive(true);
  335. expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  336. "User-Agent: aria2\r\n"
  337. "Host: localhost:80\r\n"
  338. "Proxy-Connection: Keep-Alive\r\n"
  339. "\r\n";
  340. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  341. }
  342. void HttpRequestTest::testIsRangeSatisfied()
  343. {
  344. RequestHandle request = new Request();
  345. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  346. request->setKeepAlive(false);
  347. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  348. HttpRequest httpRequest;
  349. httpRequest.setRequest(request);
  350. httpRequest.setSegment(segment);
  351. RangeHandle range = new Range(0, 0, 0);
  352. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  353. segment = new PiecedSegment(1024*1024, new Piece(1, 1024*1024));
  354. httpRequest.setSegment(segment);
  355. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  356. int64_t entityLength = segment->getSegmentLength()*10;
  357. range = new Range(segment->getPosition(), 0, entityLength);
  358. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  359. httpRequest.setEntityLength(entityLength-1);
  360. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  361. httpRequest.setEntityLength(entityLength);
  362. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  363. request->setKeepAlive(true);
  364. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  365. range = new Range(segment->getPosition(),
  366. segment->getPosition()+segment->getLength()-1,
  367. entityLength);
  368. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  369. range = new Range(0, segment->getPosition()+segment->getLength()-1,
  370. entityLength);
  371. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  372. }