RequestTest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #include "Request.h"
  2. #include "Netrc.h"
  3. #include "DefaultAuthResolver.h"
  4. #include "NetrcAuthResolver.h"
  5. #include <cppunit/extensions/HelperMacros.h>
  6. class RequestTest:public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(RequestTest);
  8. CPPUNIT_TEST(testSetUrl1);
  9. CPPUNIT_TEST(testSetUrl2);
  10. CPPUNIT_TEST(testSetUrl3);
  11. CPPUNIT_TEST(testSetUrl4);
  12. CPPUNIT_TEST(testSetUrl5);
  13. CPPUNIT_TEST(testSetUrl6);
  14. CPPUNIT_TEST(testSetUrl7);
  15. CPPUNIT_TEST(testSetUrl8);
  16. CPPUNIT_TEST(testSetUrl9);
  17. CPPUNIT_TEST(testSetUrl10);
  18. CPPUNIT_TEST(testSetUrl11);
  19. CPPUNIT_TEST(testSetUrl12);
  20. CPPUNIT_TEST(testSetUrl13);
  21. CPPUNIT_TEST(testSetUrl14);
  22. CPPUNIT_TEST(testRedirectUrl);
  23. CPPUNIT_TEST(testRedirectUrl2);
  24. CPPUNIT_TEST(testResetUrl);
  25. CPPUNIT_TEST(testSafeChar);
  26. CPPUNIT_TEST(testInnerLink);
  27. CPPUNIT_TEST(testMetalink);
  28. CPPUNIT_TEST(testResolveHttpAuthConfig);
  29. CPPUNIT_TEST(testResolveHttpAuthConfig_noCandidate);
  30. CPPUNIT_TEST(testResolveHttpProxyAuthConfig);
  31. CPPUNIT_TEST(testResolveFtpAuthConfig);
  32. CPPUNIT_TEST_SUITE_END();
  33. public:
  34. void testSetUrl1();
  35. void testSetUrl2();
  36. void testSetUrl3();
  37. void testSetUrl4();
  38. void testSetUrl5();
  39. void testSetUrl6();
  40. void testSetUrl7();
  41. void testSetUrl8();
  42. void testSetUrl9();
  43. void testSetUrl10();
  44. void testSetUrl11();
  45. void testSetUrl12();
  46. void testSetUrl13();
  47. void testSetUrl14();
  48. void testRedirectUrl();
  49. void testRedirectUrl2();
  50. void testResetUrl();
  51. void testSafeChar();
  52. void testInnerLink();
  53. void testMetalink();
  54. void testResolveHttpAuthConfig();
  55. void testResolveHttpAuthConfig_noCandidate();
  56. void testResolveHttpProxyAuthConfig();
  57. void testResolveFtpAuthConfig();
  58. };
  59. CPPUNIT_TEST_SUITE_REGISTRATION( RequestTest );
  60. void RequestTest::testSetUrl1() {
  61. Request req;
  62. bool v = req.setUrl("http://aria.rednoah.com/");
  63. CPPUNIT_ASSERT(v);
  64. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/"), req.getUrl());
  65. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/"), req.getCurrentUrl());
  66. CPPUNIT_ASSERT_EQUAL(string(""), req.getPreviousUrl());
  67. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  68. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  69. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  70. CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
  71. CPPUNIT_ASSERT_EQUAL(string(""), req.getFile());
  72. }
  73. void RequestTest::testSetUrl2() {
  74. Request req;
  75. bool v = req.setUrl("http://aria.rednoah.com:8080/index.html");
  76. req.setReferer("http://aria.rednoah.com:8080");
  77. CPPUNIT_ASSERT(v);
  78. // referer is unchaged
  79. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080"), req.getReferer());
  80. // previousUrl must equal to referer;
  81. CPPUNIT_ASSERT_EQUAL(req.getReferer(), req.getPreviousUrl());
  82. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  83. CPPUNIT_ASSERT_EQUAL(8080, req.getPort());
  84. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  85. CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
  86. CPPUNIT_ASSERT_EQUAL(string("index.html"), req.getFile());
  87. }
  88. void RequestTest::testSetUrl3() {
  89. Request req;
  90. bool v = req.setUrl("http://aria.rednoah.com/aria2/index.html");
  91. CPPUNIT_ASSERT(v);
  92. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  93. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  94. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  95. CPPUNIT_ASSERT_EQUAL(string("/aria2"), req.getDir());
  96. CPPUNIT_ASSERT_EQUAL(string("index.html"), req.getFile());
  97. }
  98. void RequestTest::testSetUrl4() {
  99. Request req;
  100. bool v = req.setUrl("http://aria.rednoah.com/aria2/aria3/index.html");
  101. CPPUNIT_ASSERT(v);
  102. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  103. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  104. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  105. CPPUNIT_ASSERT_EQUAL(string("/aria2/aria3"), req.getDir());
  106. CPPUNIT_ASSERT_EQUAL(string("index.html"), req.getFile());
  107. }
  108. void RequestTest::testSetUrl5() {
  109. Request req;
  110. bool v = req.setUrl("http://aria.rednoah.com/aria2/aria3/");
  111. CPPUNIT_ASSERT(v);
  112. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  113. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  114. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  115. CPPUNIT_ASSERT_EQUAL(string("/aria2/aria3"), req.getDir());
  116. CPPUNIT_ASSERT_EQUAL(string(""), req.getFile());
  117. }
  118. void RequestTest::testSetUrl6() {
  119. Request req;
  120. bool v = req.setUrl("http://aria.rednoah.com/aria2/aria3");
  121. CPPUNIT_ASSERT(v);
  122. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  123. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  124. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  125. CPPUNIT_ASSERT_EQUAL(string("/aria2"), req.getDir());
  126. CPPUNIT_ASSERT_EQUAL(string("aria3"), req.getFile());
  127. }
  128. void RequestTest::testSetUrl7() {
  129. Request req;
  130. bool v = req.setUrl("http://");
  131. CPPUNIT_ASSERT(!v);
  132. }
  133. void RequestTest::testSetUrl8() {
  134. Request req;
  135. bool v = req.setUrl("http:/aria.rednoah.com");
  136. CPPUNIT_ASSERT(!v);
  137. }
  138. void RequestTest::testSetUrl9() {
  139. Request req;
  140. bool v = req.setUrl("h");
  141. CPPUNIT_ASSERT(!v);
  142. }
  143. void RequestTest::testSetUrl10() {
  144. Request req;
  145. bool v = req.setUrl("");
  146. CPPUNIT_ASSERT(!v);
  147. }
  148. void RequestTest::testSetUrl11() {
  149. Request req;
  150. bool v = req.setUrl("http://host?query/");
  151. CPPUNIT_ASSERT(v);
  152. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  153. CPPUNIT_ASSERT_EQUAL(string("host"), req.getHost());
  154. CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
  155. CPPUNIT_ASSERT_EQUAL(string("?query/"), req.getFile());
  156. }
  157. void RequestTest::testSetUrl12() {
  158. Request req;
  159. bool v = req.setUrl("http://host?query");
  160. CPPUNIT_ASSERT(v);
  161. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  162. CPPUNIT_ASSERT_EQUAL(string("host"), req.getHost());
  163. CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
  164. CPPUNIT_ASSERT_EQUAL(string("?query"), req.getFile());
  165. }
  166. void RequestTest::testSetUrl13() {
  167. Request req;
  168. bool v = req.setUrl("http://host/?query");
  169. CPPUNIT_ASSERT(v);
  170. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  171. CPPUNIT_ASSERT_EQUAL(string("host"), req.getHost());
  172. CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
  173. CPPUNIT_ASSERT_EQUAL(string("?query"), req.getFile());
  174. }
  175. void RequestTest::testSetUrl14() {
  176. Request req;
  177. bool v = req.setUrl("http://host:8080/abc?query");
  178. CPPUNIT_ASSERT(v);
  179. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  180. CPPUNIT_ASSERT_EQUAL(string("host"), req.getHost());
  181. CPPUNIT_ASSERT_EQUAL(8080, req.getPort());
  182. CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
  183. CPPUNIT_ASSERT_EQUAL(string("abc?query"), req.getFile());
  184. }
  185. void RequestTest::testRedirectUrl() {
  186. Request req;
  187. bool v = req.setUrl("http://aria.rednoah.com:8080/aria2/index.html");
  188. bool v2 = req.redirectUrl("http://aria.rednoah.co.jp/");
  189. CPPUNIT_ASSERT(v2);
  190. // url must be the same
  191. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080/aria2/index.html"),
  192. req.getUrl());
  193. // currentUrl must be updated
  194. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.co.jp/"), req.getCurrentUrl());
  195. // previousUrl must be updated
  196. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080/aria2/index.html"), req.getPreviousUrl());
  197. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  198. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.co.jp"), req.getHost());
  199. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  200. CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
  201. CPPUNIT_ASSERT_EQUAL(string(""), req.getFile());
  202. }
  203. void RequestTest::testRedirectUrl2() {
  204. Request req;
  205. bool v = req.setUrl("http://aria.rednoah.com/download.html");
  206. CPPUNIT_ASSERT_EQUAL(string(""), req.getPreviousUrl());
  207. req.setReferer("http://aria.rednoah.com/");
  208. // previousUrl is updated when referer is specified
  209. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/"), req.getPreviousUrl());
  210. bool v2 = req.redirectUrl("http://aria.rednoah.com/403.html");
  211. // previousUrl is updated
  212. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/download.html"), req.getPreviousUrl());
  213. // referer is unchagned
  214. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/"), req.getReferer());
  215. bool v3 = req.redirectUrl("http://aria.rednoah.com/error.html");
  216. // previousUrl is update
  217. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/403.html"), req.getPreviousUrl());
  218. }
  219. void RequestTest::testResetUrl() {
  220. Request req;
  221. bool v = req.setUrl("http://aria.rednoah.com:8080/aria2/index.html");
  222. req.setReferer("http://aria.rednoah.com:8080/");
  223. bool v2 = req.redirectUrl("ftp://aria.rednoah.co.jp/");
  224. bool v3 = req.resetUrl();
  225. CPPUNIT_ASSERT(v3);
  226. // currentUrl must equal to url
  227. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080/aria2/index.html"), req.getUrl());
  228. CPPUNIT_ASSERT_EQUAL(req.getUrl(), req.getCurrentUrl());
  229. // previousUrl must equal to referer
  230. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080/"), req.getPreviousUrl());
  231. // referer is unchanged
  232. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080/"), req.getReferer());
  233. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  234. CPPUNIT_ASSERT_EQUAL(8080, req.getPort());
  235. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  236. CPPUNIT_ASSERT_EQUAL(string("/aria2"), req.getDir());
  237. CPPUNIT_ASSERT_EQUAL(string("index.html"), req.getFile());
  238. }
  239. void RequestTest::testSafeChar() {
  240. Request req;
  241. bool v = req.setUrl("http://aria.rednoah.com/|<>");
  242. CPPUNIT_ASSERT(!v);
  243. }
  244. void RequestTest::testInnerLink() {
  245. Request req;
  246. bool v = req.setUrl("http://aria.rednoah.com/index.html#download");
  247. CPPUNIT_ASSERT(v);
  248. CPPUNIT_ASSERT_EQUAL(string("index.html"), req.getFile());
  249. }
  250. void RequestTest::testMetalink() {
  251. Request req;
  252. bool v = req.setUrl("http://aria.rednoah.com/download/aria.tar.bz2#!metalink3!http://aria2.sourceforge.net/download/aria.metalink");
  253. CPPUNIT_ASSERT(v);
  254. #ifdef ENABLE_METALINK
  255. CPPUNIT_ASSERT_EQUAL(string("aria2.sourceforge.net"), req.getHost());
  256. CPPUNIT_ASSERT_EQUAL(string("/download"), req.getDir());
  257. CPPUNIT_ASSERT_EQUAL(string("aria.metalink"), req.getFile());
  258. bool v2 = req.setUrl("http://aria.rednoah.com/download/aria.tar.bz2#!metalink3!");
  259. CPPUNIT_ASSERT(!v2);
  260. #else
  261. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  262. CPPUNIT_ASSERT_EQUAL(string("/download"), req.getDir());
  263. CPPUNIT_ASSERT_EQUAL(string("aria.tar.bz2"), req.getFile());
  264. #endif // ENABLE_METALINK
  265. }
  266. void RequestTest::testResolveHttpAuthConfig()
  267. {
  268. Request req;
  269. req.setUrl("http://localhost/download/aria2-1.0.0.tar.bz2");
  270. // with DefaultAuthResolver
  271. DefaultAuthResolverHandle defaultAuthResolver = new DefaultAuthResolver();
  272. req.setHttpAuthResolver(defaultAuthResolver);
  273. CPPUNIT_ASSERT(!req.resolveHttpAuthConfig().isNull());
  274. CPPUNIT_ASSERT_EQUAL(string(":"),
  275. req.resolveHttpAuthConfig()->getAuthText());
  276. // with Netrc
  277. NetrcHandle netrc = new Netrc();
  278. netrc->addAuthenticator(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount"));
  279. NetrcAuthResolverHandle netrcAuthResolver = new NetrcAuthResolver();
  280. netrcAuthResolver->setNetrc(netrc);
  281. req.setHttpAuthResolver(netrcAuthResolver);
  282. AuthConfigHandle authConfig1 = req.resolveHttpAuthConfig();
  283. CPPUNIT_ASSERT(!authConfig1.isNull());
  284. CPPUNIT_ASSERT_EQUAL(string("default:defaultpassword"),
  285. authConfig1->getAuthText());
  286. // with Netrc + user defined
  287. AuthConfigHandle authConfig =
  288. new AuthConfig("userDefinedUser", "userDefinedPassword");
  289. netrcAuthResolver->setUserDefinedAuthConfig(authConfig);
  290. AuthConfigHandle authConfig2 = req.resolveHttpAuthConfig();
  291. CPPUNIT_ASSERT(!authConfig2.isNull());
  292. CPPUNIT_ASSERT_EQUAL(string("userDefinedUser:userDefinedPassword"),
  293. authConfig2->getAuthText());
  294. }
  295. void RequestTest::testResolveHttpAuthConfig_noCandidate()
  296. {
  297. Request req;
  298. req.setUrl("http://localhost/download/aria2-1.0.0.tar.bz2");
  299. DefaultAuthResolverHandle defaultAuthResolver = new DefaultAuthResolver();
  300. req.setHttpAuthResolver(defaultAuthResolver);
  301. CPPUNIT_ASSERT_EQUAL(string(":"),
  302. req.resolveHttpAuthConfig()->getAuthText());
  303. }
  304. void RequestTest::testResolveHttpProxyAuthConfig()
  305. {
  306. Request req;
  307. req.setUrl("http://localhost/download/aria2-1.0.0.tar.bz2");
  308. // with Netrc
  309. NetrcHandle netrc = new Netrc();
  310. netrc->addAuthenticator(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount"));
  311. NetrcAuthResolverHandle netrcAuthResolver = new NetrcAuthResolver();
  312. netrcAuthResolver->setNetrc(netrc);
  313. req.setHttpProxyAuthResolver(netrcAuthResolver);
  314. AuthConfigHandle authConfig1 = req.resolveHttpProxyAuthConfig();
  315. CPPUNIT_ASSERT(!authConfig1.isNull());
  316. CPPUNIT_ASSERT_EQUAL(string("default:defaultpassword"),
  317. authConfig1->getAuthText());
  318. }
  319. void RequestTest::testResolveFtpAuthConfig()
  320. {
  321. Request req;
  322. req.setUrl("http://localhost/download/aria2-1.0.0.tar.bz2");
  323. // with Netrc
  324. NetrcHandle netrc = new Netrc();
  325. netrc->addAuthenticator(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount"));
  326. NetrcAuthResolverHandle netrcAuthResolver = new NetrcAuthResolver();
  327. netrcAuthResolver->setNetrc(netrc);
  328. req.setFtpAuthResolver(netrcAuthResolver);
  329. AuthConfigHandle authConfig1 = req.resolveFtpAuthConfig();
  330. CPPUNIT_ASSERT(!authConfig1.isNull());
  331. CPPUNIT_ASSERT_EQUAL(string("default:defaultpassword"),
  332. authConfig1->getAuthText());
  333. }