diff options
author | erikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-08 20:45:27 +0000 |
---|---|---|
committer | erikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-08 20:45:27 +0000 |
commit | dd26501ef315e241911a31a8a98027c9bbb65560 (patch) | |
tree | 1c02dfce0035199f0c073ebf793607d6e7934be5 /net/url_request/url_request_unittest.cc | |
parent | 7ecec17feb965bab7cd348ab16a9da8d772f70dc (diff) | |
download | chromium_src-dd26501ef315e241911a31a8a98027c9bbb65560.zip chromium_src-dd26501ef315e241911a31a8a98027c9bbb65560.tar.gz chromium_src-dd26501ef315e241911a31a8a98027c9bbb65560.tar.bz2 |
Add FTP unit test in preparation for portable FTP implementation.
Clean up test server code.
Originally from issue 12939, written by Ibrar Ahmed (ibrar.ahmed@gmail.com)
(take 2)
Review URL: http://codereview.chromium.org/17040
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7755 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/url_request/url_request_unittest.cc')
-rw-r--r-- | net/url_request/url_request_unittest.cc | 291 |
1 files changed, 218 insertions, 73 deletions
diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index be26da9..6f965d3 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -83,11 +83,12 @@ class URLRequestTest : public PlatformTest { }; TEST_F(URLRequestTest, GetTest_NoCache) { - TestServer server(L""); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L""); + ASSERT_TRUE(NULL != server.get()); TestDelegate d; { - TestURLRequest r(server.TestServerPage(""), &d); + TestURLRequest r(server->TestServerPage(""), &d); r.Start(); EXPECT_TRUE(r.is_pending()); @@ -104,11 +105,12 @@ TEST_F(URLRequestTest, GetTest_NoCache) { } TEST_F(URLRequestTest, GetTest) { - TestServer server(L""); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L""); + ASSERT_TRUE(NULL != server.get()); TestDelegate d; { - TestURLRequest r(server.TestServerPage(""), &d); + TestURLRequest r(server->TestServerPage(""), &d); r.Start(); EXPECT_TRUE(r.is_pending()); @@ -127,7 +129,7 @@ TEST_F(URLRequestTest, GetTest) { class HTTPSRequestTest : public testing::Test { protected: HTTPSRequestTest() : util_() {}; - + SSLTestUtil util_; }; @@ -143,14 +145,15 @@ TEST_F(HTTPSRequestTest, MAYBE_HTTPSGetTest) { // a working document root to server the pages / and /hello.html, // so this test doesn't really need to specify a document root. // But if it did, a good one would be net/data/ssl. - HTTPSTestServer https_server(util_.kHostName, util_.kOKHTTPSPort, - L"net/data/ssl", - util_.GetOKCertPath().ToWStringHack()); + scoped_refptr<HTTPSTestServer> server = + HTTPSTestServer::CreateServer(util_.kHostName, util_.kOKHTTPSPort, + L"net/data/ssl", util_.GetOKCertPath().ToWStringHack()); + ASSERT_TRUE(NULL != server.get()); EXPECT_TRUE(util_.CheckCATrusted()); TestDelegate d; { - TestURLRequest r(https_server.TestServerPage(""), &d); + TestURLRequest r(server->TestServerPage(""), &d); r.Start(); EXPECT_TRUE(r.is_pending()); @@ -190,11 +193,16 @@ TEST_F(URLRequestTest, CancelTest) { } TEST_F(URLRequestTest, CancelTest2) { - TestServer server(L""); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L""); + ASSERT_TRUE(NULL != server.get()); + + // error C2446: '!=' : no conversion from 'HTTPTestServer *const ' + // to 'const int' + TestDelegate d; { - TestURLRequest r(server.TestServerPage(""), &d); + TestURLRequest r(server->TestServerPage(""), &d); d.set_cancel_in_response_started(true); @@ -214,11 +222,12 @@ TEST_F(URLRequestTest, CancelTest2) { } TEST_F(URLRequestTest, CancelTest3) { - TestServer server(L""); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L""); + ASSERT_TRUE(NULL != server.get()); TestDelegate d; { - TestURLRequest r(server.TestServerPage(""), &d); + TestURLRequest r(server->TestServerPage(""), &d); d.set_cancel_in_received_data(true); @@ -241,11 +250,12 @@ TEST_F(URLRequestTest, CancelTest3) { } TEST_F(URLRequestTest, CancelTest4) { - TestServer server(L""); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L""); + ASSERT_TRUE(NULL != server.get()); TestDelegate d; { - TestURLRequest r(server.TestServerPage(""), &d); + TestURLRequest r(server->TestServerPage(""), &d); r.Start(); EXPECT_TRUE(r.is_pending()); @@ -266,14 +276,15 @@ TEST_F(URLRequestTest, CancelTest4) { } TEST_F(URLRequestTest, CancelTest5) { - TestServer server(L""); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L""); + ASSERT_TRUE(NULL != server.get()); scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); // populate cache { TestDelegate d; - URLRequest r(server.TestServerPage("cachetime"), &d); + URLRequest r(server->TestServerPage("cachetime"), &d); r.set_context(context); r.Start(); MessageLoop::current()->Run(); @@ -283,7 +294,7 @@ TEST_F(URLRequestTest, CancelTest5) { // cancel read from cache (see bug 990242) { TestDelegate d; - URLRequest r(server.TestServerPage("cachetime"), &d); + URLRequest r(server->TestServerPage("cachetime"), &d); r.set_context(context); r.Start(); r.Cancel(); @@ -301,9 +312,9 @@ TEST_F(URLRequestTest, CancelTest5) { } TEST_F(URLRequestTest, PostTest) { - TestServer server(L"net/data"); - ASSERT_TRUE(server.init_successful()); - + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data"); + ASSERT_TRUE(NULL != server.get()); const int kMsgSize = 20000; // multiple of 10 const int kIterations = 50; char *uploadBytes = new char[kMsgSize+1]; @@ -327,7 +338,7 @@ TEST_F(URLRequestTest, PostTest) { for (int i = 0; i < kIterations; ++i) { TestDelegate d; - URLRequest r(server.TestServerPage("echo"), &d); + URLRequest r(server->TestServerPage("echo"), &d); r.set_context(context); r.set_method("POST"); @@ -353,11 +364,12 @@ TEST_F(URLRequestTest, PostTest) { } TEST_F(URLRequestTest, PostEmptyTest) { - TestServer server(L"net/data"); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data"); + ASSERT_TRUE(NULL != server.get()); TestDelegate d; { - TestURLRequest r(server.TestServerPage("echo"), &d); + TestURLRequest r(server->TestServerPage("echo"), &d); r.set_method("POST"); r.Start(); @@ -377,11 +389,12 @@ TEST_F(URLRequestTest, PostEmptyTest) { } TEST_F(URLRequestTest, PostFileTest) { - TestServer server(L"net/data"); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data"); + ASSERT_TRUE(NULL != server.get()); TestDelegate d; { - TestURLRequest r(server.TestServerPage("echo"), &d); + TestURLRequest r(server->TestServerPage("echo"), &d); r.set_method("POST"); std::wstring dir; @@ -409,7 +422,8 @@ TEST_F(URLRequestTest, PostFileTest) { int size = static_cast<int>(longsize); scoped_array<char> buf(new char[size]); - int size_read = static_cast<int>(file_util::ReadFile(path, buf.get(), size)); + int size_read = static_cast<int>(file_util::ReadFile(path, + buf.get(), size)); ASSERT_EQ(size, size_read); ASSERT_EQ(1, d.response_started_count()) << "request failed: " << @@ -506,10 +520,11 @@ TEST_F(URLRequestTest, DISABLED_DnsFailureTest) { } TEST_F(URLRequestTest, ResponseHeadersTest) { - TestServer server(L"net/data/url_request_unittest"); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data/url_request_unittest"); + ASSERT_TRUE(NULL != server.get()); TestDelegate d; - TestURLRequest req(server.TestServerPage("files/with-headers.html"), &d); + TestURLRequest req(server->TestServerPage("files/with-headers.html"), &d); req.Start(); MessageLoop::current()->Run(); @@ -530,13 +545,14 @@ TEST_F(URLRequestTest, ResponseHeadersTest) { } TEST_F(URLRequestTest, BZip2ContentTest) { - TestServer server(L"net/data/filter_unittests"); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data/filter_unittests"); + ASSERT_TRUE(NULL != server.get()); // for localhost domain, we also should support bzip2 encoding // first, get the original file TestDelegate d1; - TestURLRequest req1(server.TestServerPage("realfiles/google.txt"), &d1); + TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1); req1.Start(); MessageLoop::current()->Run(); @@ -544,7 +560,7 @@ TEST_F(URLRequestTest, BZip2ContentTest) { // second, get bzip2 content TestDelegate d2; - TestURLRequest req2(server.TestServerPage("realbz2files/google.txt"), &d2); + TestURLRequest req2(server->TestServerPage("realbz2files/google.txt"), &d2); req2.Start(); MessageLoop::current()->Run(); @@ -555,13 +571,14 @@ TEST_F(URLRequestTest, BZip2ContentTest) { } TEST_F(URLRequestTest, BZip2ContentTest_IncrementalHeader) { - TestServer server(L"net/data/filter_unittests"); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data/filter_unittests"); + ASSERT_TRUE(NULL != server.get()); // for localhost domain, we also should support bzip2 encoding // first, get the original file TestDelegate d1; - TestURLRequest req1(server.TestServerPage("realfiles/google.txt"), &d1); + TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1); req1.Start(); MessageLoop::current()->Run(); @@ -570,7 +587,8 @@ TEST_F(URLRequestTest, BZip2ContentTest_IncrementalHeader) { // second, get bzip2 content. ask the testserver to send the BZ2 header in // two chunks with a delay between them. this tests our fix for bug 867161. TestDelegate d2; - TestURLRequest req2(server.TestServerPage("realbz2files/google.txt?incremental-header"), &d2); + TestURLRequest req2(server->TestServerPage( + "realbz2files/google.txt?incremental-header"), &d2); req2.Start(); MessageLoop::current()->Run(); @@ -654,10 +672,12 @@ TEST_F(URLRequestTest, ResolveShortcutTest) { #endif // defined(OS_WIN) TEST_F(URLRequestTest, ContentTypeNormalizationTest) { - TestServer server(L"net/data/url_request_unittest"); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data/url_request_unittest"); + ASSERT_TRUE(NULL != server.get()); + TestDelegate d; - TestURLRequest req(server.TestServerPage( + TestURLRequest req(server->TestServerPage( "files/content-type-normalization.html"), &d); req.Start(); MessageLoop::current()->Run(); @@ -701,10 +721,12 @@ TEST_F(URLRequestTest, FileDirCancelTest) { } TEST_F(URLRequestTest, RestrictRedirects) { - TestServer server(L"net/data/url_request_unittest"); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data/url_request_unittest"); + ASSERT_TRUE(NULL != server.get()); + TestDelegate d; - TestURLRequest req(server.TestServerPage( + TestURLRequest req(server->TestServerPage( "files/redirect-to-file.html"), &d); req.Start(); MessageLoop::current()->Run(); @@ -714,10 +736,11 @@ TEST_F(URLRequestTest, RestrictRedirects) { } TEST_F(URLRequestTest, NoUserPassInReferrer) { - TestServer server(L"net/data/url_request_unittest"); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data/url_request_unittest"); + ASSERT_TRUE(NULL != server.get()); TestDelegate d; - TestURLRequest req(server.TestServerPage( + TestURLRequest req(server->TestServerPage( "echoheader?Referer"), &d); req.set_referrer("http://user:pass@foo.com/"); req.Start(); @@ -727,12 +750,13 @@ TEST_F(URLRequestTest, NoUserPassInReferrer) { } TEST_F(URLRequestTest, CancelRedirect) { - TestServer server(L"net/data/url_request_unittest"); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data/url_request_unittest"); + ASSERT_TRUE(NULL != server.get()); TestDelegate d; { d.set_cancel_in_received_redirect(true); - TestURLRequest req(server.TestServerPage( + TestURLRequest req(server->TestServerPage( "files/redirect-test.html"), &d); req.Start(); MessageLoop::current()->Run(); @@ -745,8 +769,9 @@ TEST_F(URLRequestTest, CancelRedirect) { } TEST_F(URLRequestTest, VaryHeader) { - TestServer server(L"net/data/url_request_unittest"); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data/url_request_unittest"); + ASSERT_TRUE(NULL != server.get()); scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); @@ -755,7 +780,7 @@ TEST_F(URLRequestTest, VaryHeader) { // populate the cache { TestDelegate d; - URLRequest req(server.TestServerPage("echoheader?foo"), &d); + URLRequest req(server->TestServerPage("echoheader?foo"), &d); req.set_context(context); req.SetExtraRequestHeaders("foo:1"); req.Start(); @@ -771,7 +796,7 @@ TEST_F(URLRequestTest, VaryHeader) { // expect a cache hit { TestDelegate d; - URLRequest req(server.TestServerPage("echoheader?foo"), &d); + URLRequest req(server->TestServerPage("echoheader?foo"), &d); req.set_context(context); req.SetExtraRequestHeaders("foo:1"); req.Start(); @@ -783,7 +808,7 @@ TEST_F(URLRequestTest, VaryHeader) { // expect a cache miss { TestDelegate d; - URLRequest req(server.TestServerPage("echoheader?foo"), &d); + URLRequest req(server->TestServerPage("echoheader?foo"), &d); req.set_context(context); req.SetExtraRequestHeaders("foo:2"); req.Start(); @@ -795,8 +820,9 @@ TEST_F(URLRequestTest, VaryHeader) { TEST_F(URLRequestTest, BasicAuth) { scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); - TestServer server(L""); - ASSERT_TRUE(server.init_successful()); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L""); + ASSERT_TRUE(NULL != server.get()); Time response_time; @@ -806,7 +832,7 @@ TEST_F(URLRequestTest, BasicAuth) { d.set_username(L"user"); d.set_password(L"secret"); - URLRequest r(server.TestServerPage("auth-basic"), &d); + URLRequest r(server->TestServerPage("auth-basic"), &d); r.set_context(context); r.Start(); @@ -829,7 +855,7 @@ TEST_F(URLRequestTest, BasicAuth) { d.set_username(L"user"); d.set_password(L"secret"); - URLRequest r(server.TestServerPage("auth-basic"), &d); + URLRequest r(server->TestServerPage("auth-basic"), &d); r.set_context(context); r.set_load_flags(net::LOAD_VALIDATE_CACHE); r.Start(); @@ -849,16 +875,20 @@ TEST_F(URLRequestTest, BasicAuth) { // Content-Type header. // http://code.google.com/p/chromium/issues/detail?id=843 TEST_F(URLRequestTest, Post302RedirectGet) { - TestServer server(L"net/data/url_request_unittest"); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data/url_request_unittest"); + ASSERT_TRUE(NULL != server.get()); TestDelegate d; - TestURLRequest req(server.TestServerPage("files/redirect-to-echoall"), &d); + TestURLRequest req(server->TestServerPage("files/redirect-to-echoall"), &d); req.set_method("POST"); // Set headers (some of which are specific to the POST). // ("Content-Length: 10" is just a junk value to make sure it gets stripped). req.SetExtraRequestHeaders( - "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" - "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" + "Content-Type: multipart/form-data; " + "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," + "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" "Accept-Language: en-US,en\r\n" "Accept-Charset: ISO-8859-1,*,utf-8\r\n" "Content-Length: 10\r\n" @@ -885,11 +915,126 @@ TEST_F(URLRequestTest, Post302RedirectGet) { } TEST_F(URLRequestTest, Post307RedirectPost) { - TestServer server(L"net/data/url_request_unittest"); + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"net/data/url_request_unittest"); + ASSERT_TRUE(NULL != server.get()); TestDelegate d; - TestURLRequest req(server.TestServerPage("files/redirect307-to-echoall"), &d); + TestURLRequest req(server->TestServerPage("files/redirect307-to-echoall"), + &d); req.set_method("POST"); req.Start(); MessageLoop::current()->Run(); EXPECT_EQ(req.method(), "POST"); } + +#if !defined(OS_WIN) + #define MAYBE_FTPGetTestAnonymous DISABLED_FTPGetTestAnonymous + #define MAYBE_FTPGetTest DISABLED_FTPGetTest + #define MAYBE_FTPCheckWrongUser DISABLED_FTPCheckWrongUser + #define MAYBE_FTPCheckWrongPassword DISABLED_FTPCheckWrongPassword +#else + #define MAYBE_FTPGetTestAnonymous FTPGetTestAnonymous + #define MAYBE_FTPGetTest FTPGetTest + #define MAYBE_FTPCheckWrongUser FTPCheckWrongUser + #define MAYBE_FTPCheckWrongPassword FTPCheckWrongPassword +#endif + +TEST_F(URLRequestTest, MAYBE_FTPGetTestAnonymous) { + scoped_refptr<FTPTestServer> server = FTPTestServer::CreateServer(L""); + ASSERT_TRUE(NULL != server.get()); + std::wstring app_path; + PathService::Get(base::DIR_SOURCE_ROOT, &app_path); + app_path.append(L"\\LICENSE"); + TestDelegate d; + { + TestURLRequest r(server->TestServerPage("/LICENSE"), &d); + r.Start(); + EXPECT_TRUE(r.is_pending()); + + MessageLoop::current()->Run(); + + int64 file_size = 0; + file_util::GetFileSize(app_path, &file_size); + + EXPECT_TRUE(!r.is_pending()); + EXPECT_EQ(1, d.response_started_count()); + EXPECT_FALSE(d.received_data_before_response()); + EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); + } +} + +TEST_F(URLRequestTest, MAYBE_FTPGetTest) { + scoped_refptr<FTPTestServer> server = + FTPTestServer::CreateServer(L"", "chrome", "chrome"); + ASSERT_TRUE(NULL != server.get()); + std::wstring app_path; + PathService::Get(base::DIR_SOURCE_ROOT, &app_path); + app_path.append(L"\\LICENSE"); + TestDelegate d; + { + TestURLRequest r(server->TestServerPage("/LICENSE"), &d); + r.Start(); + EXPECT_TRUE(r.is_pending()); + + MessageLoop::current()->Run(); + + int64 file_size = 0; + file_util::GetFileSize(app_path, &file_size); + + EXPECT_TRUE(!r.is_pending()); + EXPECT_EQ(1, d.response_started_count()); + EXPECT_FALSE(d.received_data_before_response()); + EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); + } +} + +TEST_F(URLRequestTest, MAYBE_FTPCheckWrongPassword) { + scoped_refptr<FTPTestServer> server = + FTPTestServer::CreateServer(L"", "chrome", "wrong_password"); + ASSERT_TRUE(NULL != server.get()); + std::wstring app_path; + PathService::Get(base::DIR_SOURCE_ROOT, &app_path); + app_path.append(L"\\LICENSE"); + TestDelegate d; + { + TestURLRequest r(server->TestServerPage("/LICENSE"), &d); + r.Start(); + EXPECT_TRUE(r.is_pending()); + + MessageLoop::current()->Run(); + + int64 file_size = 0; + file_util::GetFileSize(app_path, &file_size); + + EXPECT_TRUE(!r.is_pending()); + EXPECT_EQ(1, d.response_started_count()); + EXPECT_FALSE(d.received_data_before_response()); + EXPECT_EQ(d.bytes_received(), 0); + } +} + +TEST_F(URLRequestTest, MAYBE_FTPCheckWrongUser) { + scoped_refptr<FTPTestServer> server = + FTPTestServer::CreateServer(L"", "wrong_user", "chrome"); + ASSERT_TRUE(NULL != server.get()); + std::wstring app_path; + PathService::Get(base::DIR_SOURCE_ROOT, &app_path); + app_path.append(L"\\LICENSE"); + TestDelegate d; + { + TestURLRequest r(server->TestServerPage("/LICENSE"), &d); + r.Start(); + EXPECT_TRUE(r.is_pending()); + + MessageLoop::current()->Run(); + + int64 file_size = 0; + file_util::GetFileSize(app_path, &file_size); + + EXPECT_TRUE(!r.is_pending()); + EXPECT_EQ(1, d.response_started_count()); + EXPECT_FALSE(d.received_data_before_response()); + EXPECT_EQ(d.bytes_received(), 0); + } +} + |