HttpRequestTest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  187. httpRequest.configure(option.get());
  188. expectedText = "GET http://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  189. "User-Agent: aria2\r\n"
  190. "Accept: */*\r\n"
  191. "Host: localhost:8080\r\n"
  192. "Pragma: no-cache\r\n"
  193. "Cache-Control: no-cache\r\n"
  194. "Connection: close\r\n"
  195. "Proxy-Connection: close\r\n"
  196. "Authorization: Basic YXJpYTJ1c2VyOmFyaWEycGFzc3dk\r\n"
  197. "\r\n";
  198. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  199. }
  200. void HttpRequestTest::testCreateRequest_ftp()
  201. {
  202. SharedHandle<Option> option = new Option();
  203. option->put(PREF_HTTP_AUTH_ENABLED, V_FALSE);
  204. option->put(PREF_HTTP_PROXY_ENABLED, V_FALSE);
  205. option->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  206. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_FALSE);
  207. option->put(PREF_HTTP_USER, "aria2user");
  208. option->put(PREF_HTTP_PASSWD, "aria2passwd");
  209. option->put(PREF_HTTP_PROXY_USER, "aria2proxyuser");
  210. option->put(PREF_HTTP_PROXY_PASSWD, "aria2proxypasswd");
  211. RequestFactory requestFactory;
  212. requestFactory.setOption(option.get());
  213. RequestHandle request = requestFactory.createRequest();
  214. request->setUrl("ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  215. HttpRequest httpRequest;
  216. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  217. httpRequest.setRequest(request);
  218. httpRequest.setSegment(segment);
  219. httpRequest.configure(option.get());
  220. string expectedText = "GET ftp://localhost:8080/archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  221. "User-Agent: aria2\r\n"
  222. "Accept: */*\r\n"
  223. "Host: localhost:8080\r\n"
  224. "Pragma: no-cache\r\n"
  225. "Cache-Control: no-cache\r\n"
  226. "Connection: close\r\n"
  227. "\r\n";
  228. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  229. // How to enable HTTP proxy authorization in FTP download via HTTP proxy
  230. option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  231. option->put(PREF_HTTP_PROXY_METHOD, V_GET);
  232. option->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  233. httpRequest.configure(option.get());
  234. 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. "Proxy-Connection: close\r\n"
  242. "Proxy-Authorization: Basic YXJpYTJwcm94eXVzZXI6YXJpYTJwcm94eXBhc3N3ZA==\r\n"
  243. "\r\n";
  244. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  245. }
  246. void HttpRequestTest::testCreateRequest_with_cookie()
  247. {
  248. RequestHandle request = new Request();
  249. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  250. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  251. Cookie cookie1("name1", "value1", "/archives", "localhost", false);
  252. Cookie cookie2("name2", "value2", "/archives/download", "localhost", false);
  253. Cookie cookie3("name3", "value3", "/archives/download", "tt.localhost", false);
  254. Cookie cookie4("name4", "value4", "/archives/download", "tt.localhost", true);
  255. request->cookieBox->add(cookie1);
  256. request->cookieBox->add(cookie2);
  257. request->cookieBox->add(cookie3);
  258. request->cookieBox->add(cookie4);
  259. HttpRequest httpRequest;
  260. httpRequest.setRequest(request);
  261. httpRequest.setSegment(segment);
  262. string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  263. "User-Agent: aria2\r\n"
  264. "Accept: */*\r\n"
  265. "Host: localhost\r\n"
  266. "Pragma: no-cache\r\n"
  267. "Cache-Control: no-cache\r\n"
  268. "Connection: close\r\n"
  269. "Cookie: name1=value1;\r\n"
  270. "\r\n";
  271. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  272. request->setUrl("http://localhost/archives/download/aria2-1.0.0.tar.bz2");
  273. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  274. "User-Agent: aria2\r\n"
  275. "Accept: */*\r\n"
  276. "Host: localhost\r\n"
  277. "Pragma: no-cache\r\n"
  278. "Cache-Control: no-cache\r\n"
  279. "Connection: close\r\n"
  280. "Cookie: name1=value1;name2=value2;\r\n"
  281. "\r\n";
  282. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  283. request->setUrl("http://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  284. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  285. "User-Agent: aria2\r\n"
  286. "Accept: */*\r\n"
  287. "Host: tt.localhost\r\n"
  288. "Pragma: no-cache\r\n"
  289. "Cache-Control: no-cache\r\n"
  290. "Connection: close\r\n"
  291. "Cookie: name1=value1;name2=value2;name3=value3;\r\n"
  292. "\r\n";
  293. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  294. request->setUrl("https://tt.localhost/archives/download/aria2-1.0.0.tar.bz2");
  295. expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n"
  296. "User-Agent: aria2\r\n"
  297. "Accept: */*\r\n"
  298. "Host: tt.localhost\r\n"
  299. "Pragma: no-cache\r\n"
  300. "Cache-Control: no-cache\r\n"
  301. "Connection: close\r\n"
  302. "Cookie: name1=value1;name2=value2;name3=value3;name4=value4;\r\n"
  303. "\r\n";
  304. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
  305. }
  306. void HttpRequestTest::testCreateProxyRequest()
  307. {
  308. RequestHandle request = new Request();
  309. request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2");
  310. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  311. HttpRequest httpRequest;
  312. httpRequest.setRequest(request);
  313. httpRequest.setSegment(segment);
  314. string expectedText = "CONNECT localhost:80 HTTP/1.1\r\n"
  315. "User-Agent: aria2\r\n"
  316. "Proxy-Connection: close\r\n"
  317. "Host: localhost:80\r\n"
  318. "\r\n";
  319. CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createProxyRequest());
  320. }
  321. void HttpRequestTest::testIsRangeSatisfied()
  322. {
  323. RequestHandle request = new Request();
  324. request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2");
  325. request->setKeepAlive(false);
  326. SegmentHandle segment = new PiecedSegment(1024*1024, new Piece(0, 1024*1024));
  327. HttpRequest httpRequest;
  328. httpRequest.setRequest(request);
  329. httpRequest.setSegment(segment);
  330. RangeHandle range = new Range(0, 0, 0);
  331. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  332. segment = new PiecedSegment(1024*1024, new Piece(1, 1024*1024));
  333. httpRequest.setSegment(segment);
  334. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  335. int64_t entityLength = segment->getSegmentLength()*10;
  336. range = new Range(segment->getPosition(), 0, entityLength);
  337. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  338. httpRequest.setEntityLength(entityLength-1);
  339. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  340. httpRequest.setEntityLength(entityLength);
  341. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  342. request->setKeepAlive(true);
  343. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  344. range = new Range(segment->getPosition(),
  345. segment->getPosition()+segment->getLength()-1,
  346. entityLength);
  347. CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
  348. range = new Range(0, segment->getPosition()+segment->getLength()-1,
  349. entityLength);
  350. CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
  351. }