HttpRequestTest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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_SUITE_END();
  16. private:
  17. public:
  18. void setUp() {}
  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. SharedHandle<Option> option = new Option();
  59. option->put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
  60. option->put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  61. option->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  62. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  63. option->put(PREF_HTTP_USER, "aria2user");
  64. option->put(PREF_HTTP_PASSWD, "aria2passwd");
  65. option->put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  66. option->put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  67. RequestFactory requestFactory;
  68. requestFactory.setOption(option.get());
  69. RequestHandle request = requestFactory.createRequest();
  70. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  71. SegmentHandle segment = new Segment();
  72. HttpRequest httpRequest;
  73. httpRequest.setRequest(request);
  74. httpRequest.setSegment(segment);
  75. string 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. "\r\n";
  82. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  83. request->setKeepAlive(false);
  84. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  85. "User-Agent: aria2\r\n"
  86. "Accept: */*\r\n"
  87. "Host: localhost:8080\r\n"
  88. "Pragma: no-cache\r\n"
  89. "Cache-Control: no-cache\r\n"
  90. "Connection: close\r\n"
  91. "\r\n";
  92. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  93. segment->index = 1;
  94. segment->length = 1024*1024;
  95. segment->segmentLength = 1024*1024;
  96. segment->writtenLength = 0;
  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. "Connection: close\r\n"
  104. "Range: bytes=1048576-\r\n"
  105. "\r\n";
  106. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  107. request->setKeepAlive(true);
  108. expectedText = "GET /archives/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. "Range: bytes=1048576-2097151\r\n"
  115. "\r\n";
  116. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  117. httpRequest.setSegment(new Segment());
  118. request->redirectUrl("http://localhost:8080/archives/download/aria2-1.0.0.tar.bz2");
  119. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  120. "User-Agent: aria2\r\n"
  121. "Accept: */*\r\n"
  122. "Host: localhost:8080\r\n"
  123. "Pragma: no-cache\r\n"
  124. "Cache-Control: no-cache\r\n"
  125. "\r\n";
  126. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  127. request->resetUrl();
  128. // enable http auth
  129. option->put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  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. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  138. "\r\n";
  139. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  140. // enable http proxy auth
  141. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  142. httpRequest.configure(option.get());
  143. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  144. "User-Agent: aria2\r\n"
  145. "Accept: */*\r\n"
  146. "Host: localhost:8080\r\n"
  147. "Pragma: no-cache\r\n"
  148. "Cache-Control: no-cache\r\n"
  149. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  150. "\r\n";
  151. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  152. option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  153. httpRequest.configure(option.get());
  154. expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  155. "User-Agent: aria2\r\n"
  156. "Accept: */*\r\n"
  157. "Host: localhost:8080\r\n"
  158. "Pragma: no-cache\r\n"
  159. "Cache-Control: no-cache\r\n"
  160. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  161. "\r\n";
  162. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  163. option->put(PREF_HTTP_PROXY_METHOD, V_GET);
  164. httpRequest.configure(option.get());
  165. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  166. "User-Agent: aria2\r\n"
  167. "Accept: */*\r\n"
  168. "Host: localhost:8080\r\n"
  169. "Pragma: no-cache\r\n"
  170. "Cache-Control: no-cache\r\n"
  171. "Proxy-Connection: close\r\n"
  172. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  173. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  174. "\r\n";
  175. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  176. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  177. httpRequest.configure(option.get());
  178. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  179. "User-Agent: aria2\r\n"
  180. "Accept: */*\r\n"
  181. "Host: localhost:8080\r\n"
  182. "Pragma: no-cache\r\n"
  183. "Cache-Control: no-cache\r\n"
  184. "Proxy-Connection: close\r\n"
  185. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  186. "\r\n";
  187. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  188. }
  189. void HttpRequestTest::testCreateRequest_ftp()
  190. {
  191. SharedHandle<Option> option = new Option();
  192. option->put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
  193. option->put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  194. option->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  195. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  196. option->put(PREF_HTTP_USER, "aria2user");
  197. option->put(PREF_HTTP_PASSWD, "aria2passwd");
  198. option->put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  199. option->put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  200. RequestFactory requestFactory;
  201. requestFactory.setOption(option.get());
  202. RequestHandle request = requestFactory.createRequest();
  203. request->setUrl("ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  204. SegmentHandle segment = new Segment();
  205. HttpRequest httpRequest;
  206. httpRequest.setRequest(request);
  207. httpRequest.setSegment(segment);
  208. httpRequest.configure(option.get());
  209. string expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  210. "User-Agent: aria2\r\n"
  211. "Accept: */*\r\n"
  212. "Host: localhost:8080\r\n"
  213. "Pragma: no-cache\r\n"
  214. "Cache-Control: no-cache\r\n"
  215. "\r\n";
  216. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  217. // How to enable HTTP proxy authorization in FTP download via HTTP proxy
  218. option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  219. option->put(PREF_HTTP_PROXY_METHOD, V_GET);
  220. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  221. httpRequest.configure(option.get());
  222. expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  223. "User-Agent: aria2\r\n"
  224. "Accept: */*\r\n"
  225. "Host: localhost:8080\r\n"
  226. "Pragma: no-cache\r\n"
  227. "Cache-Control: no-cache\r\n"
  228. "Proxy-Connection: close\r\n"
  229. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  230. "\r\n";
  231. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  232. }
  233. void HttpRequestTest::testCreateRequest_with_cookie()
  234. {
  235. RequestHandle request = new Request();
  236. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  237. SegmentHandle segment = new Segment();
  238. Cookie cookie1("name1", "value1", "/archives", "localhost", false);
  239. Cookie cookie2("name2", "value2", "/archives/download", "localhost", false);
  240. Cookie cookie3("name3", "value3", "/archives/download", "tt.localhost", false);
  241. Cookie cookie4("name4", "value4", "/archives/download", "tt.localhost", true);
  242. request->cookieBox->add(cookie1);
  243. request->cookieBox->add(cookie2);
  244. request->cookieBox->add(cookie3);
  245. request->cookieBox->add(cookie4);
  246. HttpRequest httpRequest;
  247. httpRequest.setRequest(request);
  248. httpRequest.setSegment(segment);
  249. string expectedText = "GET /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\r\n"
  253. "Pragma: no-cache\r\n"
  254. "Cache-Control: no-cache\r\n"
  255. "Cookie: name1=value1;\r\n"
  256. "\r\n";
  257. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  258. request->setUrl("http://localhost/archives/download/aria2-1.0.0.tar.bz2");
  259. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  260. "User-Agent: aria2\r\n"
  261. "Accept: */*\r\n"
  262. "Host: localhost\r\n"
  263. "Pragma: no-cache\r\n"
  264. "Cache-Control: no-cache\r\n"
  265. "Cookie: name1=value1;name2=value2;\r\n"
  266. "\r\n";
  267. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  268. request->setUrl("http://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  269. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  270. "User-Agent: aria2\r\n"
  271. "Accept: */*\r\n"
  272. "Host: tt.localhost\r\n"
  273. "Pragma: no-cache\r\n"
  274. "Cache-Control: no-cache\r\n"
  275. "Cookie: name1=value1;name2=value2;name3=value3;\r\n"
  276. "\r\n";
  277. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  278. request->setUrl("https://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  279. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  280. "User-Agent: aria2\r\n"
  281. "Accept: */*\r\n"
  282. "Host: tt.localhost\r\n"
  283. "Pragma: no-cache\r\n"
  284. "Cache-Control: no-cache\r\n"
  285. "Cookie: name1=value1;name2=value2;name3=value3;name4=value4;\r\n"
  286. "\r\n";
  287. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  288. }
  289. void HttpRequestTest::testCreateProxyRequest()
  290. {
  291. RequestHandle request = new Request();
  292. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  293. SegmentHandle segment = new Segment();
  294. HttpRequest httpRequest;
  295. httpRequest.setRequest(request);
  296. httpRequest.setSegment(segment);
  297. string expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  298. "User-Agent: aria2\r\n"
  299. "Proxy-Connection: close\r\n"
  300. "Host: localhost:80\r\n"
  301. "\r\n";
  302. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  303. }
  304. void HttpRequestTest::testIsRangeSatisfied()
  305. {
  306. RequestHandle request = new Request();
  307. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  308. request->setKeepAlive(false);
  309. SegmentHandle segment = new Segment();
  310. HttpRequest httpRequest;
  311. httpRequest.setRequest(request);
  312. httpRequest.setSegment(segment);
  313. RangeHandle range = new Range(0, 0, 0);
  314. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  315. segment->index = 1;
  316. segment->length = 1024*1024;
  317. segment->segmentLength = 1024*1024;
  318. segment->writtenLength = 0;
  319. int64_t entityLength = segment->segmentLength*10;
  320. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  321. range = new Range(segment->getPosition(), 0, entityLength);
  322. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  323. httpRequest.setEntityLength(entityLength-1);
  324. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  325. httpRequest.setEntityLength(entityLength);
  326. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  327. request->setKeepAlive(true);
  328. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  329. range = new Range(segment->getPosition(),
  330. segment->getPosition()+segment->length-1, entityLength);
  331. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  332. range = new Range(0, segment->getPosition()+segment->length-1, entityLength);
  333. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  334. }