HttpRequestTest.cc 13 KB

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