#include "Request.h" #include "Netrc.h" #include "DefaultAuthResolver.h" #include "NetrcAuthResolver.h" #include namespace aria2 { class RequestTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(RequestTest); CPPUNIT_TEST(testSetUrl1); CPPUNIT_TEST(testSetUrl2); CPPUNIT_TEST(testSetUrl3); CPPUNIT_TEST(testSetUrl4); CPPUNIT_TEST(testSetUrl5); CPPUNIT_TEST(testSetUrl6); CPPUNIT_TEST(testSetUrl7); CPPUNIT_TEST(testSetUrl8); CPPUNIT_TEST(testSetUrl9); CPPUNIT_TEST(testSetUrl10); CPPUNIT_TEST(testSetUrl11); CPPUNIT_TEST(testSetUrl12); CPPUNIT_TEST(testSetUrl13); CPPUNIT_TEST(testSetUrl14); CPPUNIT_TEST(testSetUrl15); CPPUNIT_TEST(testSetUrl16); CPPUNIT_TEST(testSetUrl17); CPPUNIT_TEST(testSetUrl_username); CPPUNIT_TEST(testSetUrl_usernamePassword); CPPUNIT_TEST(testSetUrl_zeroUsername); CPPUNIT_TEST(testRedirectUrl); CPPUNIT_TEST(testRedirectUrl2); CPPUNIT_TEST(testResetUrl); CPPUNIT_TEST(testInnerLink); CPPUNIT_TEST_SUITE_END(); public: void testSetUrl1(); void testSetUrl2(); void testSetUrl3(); void testSetUrl4(); void testSetUrl5(); void testSetUrl6(); void testSetUrl7(); void testSetUrl8(); void testSetUrl9(); void testSetUrl10(); void testSetUrl11(); void testSetUrl12(); void testSetUrl13(); void testSetUrl14(); void testSetUrl15(); void testSetUrl16(); void testSetUrl17(); void testSetUrl_username(); void testSetUrl_usernamePassword(); void testSetUrl_zeroUsername(); void testRedirectUrl(); void testRedirectUrl2(); void testResetUrl(); void testInnerLink(); }; CPPUNIT_TEST_SUITE_REGISTRATION( RequestTest ); void RequestTest::testSetUrl1() { Request req; bool v = req.setUrl("http://aria.rednoah.com/"); CPPUNIT_ASSERT(v); CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com/"), req.getUrl()); CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com/"), req.getCurrentUrl()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getPreviousUrl()); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL((uint16_t)80, req.getPort()); CPPUNIT_ASSERT_EQUAL(std::string("aria.rednoah.com"), req.getHost()); CPPUNIT_ASSERT_EQUAL(std::string("/"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getQuery()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getUsername()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getPassword()); } void RequestTest::testSetUrl2() { Request req; bool v = req.setUrl("http://aria.rednoah.com:8080/index.html"); req.setReferer("http://aria.rednoah.com:8080"); CPPUNIT_ASSERT(v); // referer is unchaged CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com:8080"), req.getReferer()); // previousUrl must equal to referer; CPPUNIT_ASSERT_EQUAL(req.getReferer(), req.getPreviousUrl()); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL((uint16_t)8080, req.getPort()); CPPUNIT_ASSERT_EQUAL(std::string("aria.rednoah.com"), req.getHost()); CPPUNIT_ASSERT_EQUAL(std::string("/"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string("index.html"), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getQuery()); } void RequestTest::testSetUrl3() { Request req; bool v = req.setUrl("http://aria.rednoah.com/aria2/index.html"); CPPUNIT_ASSERT(v); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL((uint16_t)80, req.getPort()); CPPUNIT_ASSERT_EQUAL(std::string("aria.rednoah.com"), req.getHost()); CPPUNIT_ASSERT_EQUAL(std::string("/aria2"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string("index.html"), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getQuery()); } void RequestTest::testSetUrl4() { Request req; bool v = req.setUrl("http://aria.rednoah.com/aria2/aria3/index.html"); CPPUNIT_ASSERT(v); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL((uint16_t)80, req.getPort()); CPPUNIT_ASSERT_EQUAL(std::string("aria.rednoah.com"), req.getHost()); CPPUNIT_ASSERT_EQUAL(std::string("/aria2/aria3"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string("index.html"), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getQuery()); } void RequestTest::testSetUrl5() { Request req; bool v = req.setUrl("http://aria.rednoah.com/aria2/aria3/"); CPPUNIT_ASSERT(v); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL((uint16_t)80, req.getPort()); CPPUNIT_ASSERT_EQUAL(std::string("aria.rednoah.com"), req.getHost()); CPPUNIT_ASSERT_EQUAL(std::string("/aria2/aria3"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getQuery()); } void RequestTest::testSetUrl6() { Request req; bool v = req.setUrl("http://aria.rednoah.com/aria2/aria3"); CPPUNIT_ASSERT(v); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL((uint16_t)80, req.getPort()); CPPUNIT_ASSERT_EQUAL(std::string("aria.rednoah.com"), req.getHost()); CPPUNIT_ASSERT_EQUAL(std::string("/aria2"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string("aria3"), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getQuery()); } void RequestTest::testSetUrl7() { Request req; bool v = req.setUrl("http://"); CPPUNIT_ASSERT(!v); } void RequestTest::testSetUrl8() { Request req; bool v = req.setUrl("http:/aria.rednoah.com"); CPPUNIT_ASSERT(!v); } void RequestTest::testSetUrl9() { Request req; bool v = req.setUrl("h"); CPPUNIT_ASSERT(!v); } void RequestTest::testSetUrl10() { Request req; bool v = req.setUrl(""); CPPUNIT_ASSERT(!v); } void RequestTest::testSetUrl11() { Request req; bool v = req.setUrl("http://host?query/"); CPPUNIT_ASSERT(v); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL(std::string("host"), req.getHost()); CPPUNIT_ASSERT_EQUAL(std::string("/"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string("?query/"), req.getQuery()); } void RequestTest::testSetUrl12() { Request req; bool v = req.setUrl("http://host?query"); CPPUNIT_ASSERT(v); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL(std::string("host"), req.getHost()); CPPUNIT_ASSERT_EQUAL(std::string("/"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string("?query"), req.getQuery()); } void RequestTest::testSetUrl13() { Request req; bool v = req.setUrl("http://host/?query"); CPPUNIT_ASSERT(v); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL(std::string("host"), req.getHost()); CPPUNIT_ASSERT_EQUAL(std::string("/"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string("?query"), req.getQuery()); } void RequestTest::testSetUrl14() { Request req; bool v = req.setUrl("http://host:8080/abc?query"); CPPUNIT_ASSERT(v); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL(std::string("host"), req.getHost()); CPPUNIT_ASSERT_EQUAL((uint16_t)8080, req.getPort()); CPPUNIT_ASSERT_EQUAL(std::string("/"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string("abc"), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string("?query"), req.getQuery()); } void RequestTest::testSetUrl15() { Request req; // 2 slashes after host name and dir bool v = req.setUrl("http://host//dir1/dir2//file"); CPPUNIT_ASSERT(v); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL(std::string("host"), req.getHost()); CPPUNIT_ASSERT_EQUAL(std::string("/dir1/dir2"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string("file"), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getQuery()); } void RequestTest::testSetUrl16() { Request req; // 2 slashes before file bool v = req.setUrl("http://host//file"); CPPUNIT_ASSERT(v); CPPUNIT_ASSERT_EQUAL(std::string("http"), req.getProtocol()); CPPUNIT_ASSERT_EQUAL(std::string("host"), req.getHost()); CPPUNIT_ASSERT_EQUAL(std::string("/"), req.getDir()); CPPUNIT_ASSERT_EQUAL(std::string("file"), req.getFile()); CPPUNIT_ASSERT_EQUAL(std::string(""), req.getQuery()); } void RequestTest::testSetUrl17() { Request req; bool v = req.setUrl("http://host:80/file