| OLD | NEW |
| 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/url_request_unittest.h" | 5 #include "net/url_request/url_request_unittest.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include <windows.h> | 10 #include <windows.h> |
| 11 #include <shlobj.h> | 11 #include <shlobj.h> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 #include "net/url_request/url_request_file_dir_job.h" | 44 #include "net/url_request/url_request_file_dir_job.h" |
| 45 #include "net/url_request/url_request_http_job.h" | 45 #include "net/url_request/url_request_http_job.h" |
| 46 #include "net/url_request/url_request_test_job.h" | 46 #include "net/url_request/url_request_test_job.h" |
| 47 #include "testing/gtest/include/gtest/gtest.h" | 47 #include "testing/gtest/include/gtest/gtest.h" |
| 48 #include "testing/platform_test.h" | 48 #include "testing/platform_test.h" |
| 49 | 49 |
| 50 using base::Time; | 50 using base::Time; |
| 51 | 51 |
| 52 namespace { | 52 namespace { |
| 53 | 53 |
| 54 const string16 kChrome(ASCIIToUTF16("chrome")); |
| 55 const string16 kSecret(ASCIIToUTF16("secret")); |
| 56 const string16 kUser(ASCIIToUTF16("user")); |
| 57 |
| 54 base::StringPiece TestNetResourceProvider(int key) { | 58 base::StringPiece TestNetResourceProvider(int key) { |
| 55 return "header"; | 59 return "header"; |
| 56 } | 60 } |
| 57 | 61 |
| 58 // Do a case-insensitive search through |haystack| for |needle|. | 62 // Do a case-insensitive search through |haystack| for |needle|. |
| 59 bool ContainsString(const std::string& haystack, const char* needle) { | 63 bool ContainsString(const std::string& haystack, const char* needle) { |
| 60 std::string::const_iterator it = | 64 std::string::const_iterator it = |
| 61 std::search(haystack.begin(), | 65 std::search(haystack.begin(), |
| 62 haystack.end(), | 66 haystack.end(), |
| 63 needle, | 67 needle, |
| (...skipping 1094 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1158 } | 1162 } |
| 1159 } | 1163 } |
| 1160 | 1164 |
| 1161 TEST_F(URLRequestTestHTTP, BasicAuth) { | 1165 TEST_F(URLRequestTestHTTP, BasicAuth) { |
| 1162 scoped_refptr<URLRequestContext> context = new TestURLRequestContext(); | 1166 scoped_refptr<URLRequestContext> context = new TestURLRequestContext(); |
| 1163 ASSERT_TRUE(NULL != server_.get()); | 1167 ASSERT_TRUE(NULL != server_.get()); |
| 1164 | 1168 |
| 1165 // populate the cache | 1169 // populate the cache |
| 1166 { | 1170 { |
| 1167 TestDelegate d; | 1171 TestDelegate d; |
| 1168 d.set_username(L"user"); | 1172 d.set_username(kUser); |
| 1169 d.set_password(L"secret"); | 1173 d.set_password(kSecret); |
| 1170 | 1174 |
| 1171 URLRequest r(server_->TestServerPage("auth-basic"), &d); | 1175 URLRequest r(server_->TestServerPage("auth-basic"), &d); |
| 1172 r.set_context(context); | 1176 r.set_context(context); |
| 1173 r.Start(); | 1177 r.Start(); |
| 1174 | 1178 |
| 1175 MessageLoop::current()->Run(); | 1179 MessageLoop::current()->Run(); |
| 1176 | 1180 |
| 1177 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); | 1181 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 1178 } | 1182 } |
| 1179 | 1183 |
| 1180 // repeat request with end-to-end validation. since auth-basic results in a | 1184 // repeat request with end-to-end validation. since auth-basic results in a |
| 1181 // cachable page, we expect this test to result in a 304. in which case, the | 1185 // cachable page, we expect this test to result in a 304. in which case, the |
| 1182 // response should be fetched from the cache. | 1186 // response should be fetched from the cache. |
| 1183 { | 1187 { |
| 1184 TestDelegate d; | 1188 TestDelegate d; |
| 1185 d.set_username(L"user"); | 1189 d.set_username(kUser); |
| 1186 d.set_password(L"secret"); | 1190 d.set_password(kSecret); |
| 1187 | 1191 |
| 1188 URLRequest r(server_->TestServerPage("auth-basic"), &d); | 1192 URLRequest r(server_->TestServerPage("auth-basic"), &d); |
| 1189 r.set_context(context); | 1193 r.set_context(context); |
| 1190 r.set_load_flags(net::LOAD_VALIDATE_CACHE); | 1194 r.set_load_flags(net::LOAD_VALIDATE_CACHE); |
| 1191 r.Start(); | 1195 r.Start(); |
| 1192 | 1196 |
| 1193 MessageLoop::current()->Run(); | 1197 MessageLoop::current()->Run(); |
| 1194 | 1198 |
| 1195 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); | 1199 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 1196 | 1200 |
| 1197 // Should be the same cached document. | 1201 // Should be the same cached document. |
| 1198 EXPECT_TRUE(r.was_cached()); | 1202 EXPECT_TRUE(r.was_cached()); |
| 1199 } | 1203 } |
| 1200 } | 1204 } |
| 1201 | 1205 |
| 1202 // Check that Set-Cookie headers in 401 responses are respected. | 1206 // Check that Set-Cookie headers in 401 responses are respected. |
| 1203 // http://crbug.com/6450 | 1207 // http://crbug.com/6450 |
| 1204 TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { | 1208 TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
| 1205 ASSERT_TRUE(NULL != server_.get()); | 1209 ASSERT_TRUE(NULL != server_.get()); |
| 1206 | 1210 |
| 1207 GURL url_requiring_auth = | 1211 GURL url_requiring_auth = |
| 1208 server_->TestServerPage("auth-basic?set-cookie-if-challenged"); | 1212 server_->TestServerPage("auth-basic?set-cookie-if-challenged"); |
| 1209 | 1213 |
| 1210 // Request a page that will give a 401 containing a Set-Cookie header. | 1214 // Request a page that will give a 401 containing a Set-Cookie header. |
| 1211 // Verify that when the transaction is restarted, it includes the new cookie. | 1215 // Verify that when the transaction is restarted, it includes the new cookie. |
| 1212 { | 1216 { |
| 1213 scoped_refptr<URLRequestContext> context = new TestURLRequestContext(); | 1217 scoped_refptr<URLRequestContext> context = new TestURLRequestContext(); |
| 1214 TestDelegate d; | 1218 TestDelegate d; |
| 1215 d.set_username(L"user"); | 1219 d.set_username(kUser); |
| 1216 d.set_password(L"secret"); | 1220 d.set_password(kSecret); |
| 1217 | 1221 |
| 1218 URLRequest r(url_requiring_auth, &d); | 1222 URLRequest r(url_requiring_auth, &d); |
| 1219 r.set_context(context); | 1223 r.set_context(context); |
| 1220 r.Start(); | 1224 r.Start(); |
| 1221 | 1225 |
| 1222 MessageLoop::current()->Run(); | 1226 MessageLoop::current()->Run(); |
| 1223 | 1227 |
| 1224 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); | 1228 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 1225 | 1229 |
| 1226 // Make sure we sent the cookie in the restarted transaction. | 1230 // Make sure we sent the cookie in the restarted transaction. |
| (...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1764 // Custom URLRequestJobs for use with interceptor tests | 1768 // Custom URLRequestJobs for use with interceptor tests |
| 1765 class RestartTestJob : public URLRequestTestJob { | 1769 class RestartTestJob : public URLRequestTestJob { |
| 1766 public: | 1770 public: |
| 1767 explicit RestartTestJob(URLRequest* request) | 1771 explicit RestartTestJob(URLRequest* request) |
| 1768 : URLRequestTestJob(request, true) {} | 1772 : URLRequestTestJob(request, true) {} |
| 1769 protected: | 1773 protected: |
| 1770 virtual void StartAsync() { | 1774 virtual void StartAsync() { |
| 1771 this->NotifyRestartRequired(); | 1775 this->NotifyRestartRequired(); |
| 1772 } | 1776 } |
| 1773 private: | 1777 private: |
| 1774 ~RestartTestJob() {} | 1778 ~RestartTestJob() {} |
| 1775 }; | 1779 }; |
| 1776 | 1780 |
| 1777 class CancelTestJob : public URLRequestTestJob { | 1781 class CancelTestJob : public URLRequestTestJob { |
| 1778 public: | 1782 public: |
| 1779 explicit CancelTestJob(URLRequest* request) | 1783 explicit CancelTestJob(URLRequest* request) |
| 1780 : URLRequestTestJob(request, true) {} | 1784 : URLRequestTestJob(request, true) {} |
| 1781 protected: | 1785 protected: |
| 1782 virtual void StartAsync() { | 1786 virtual void StartAsync() { |
| 1783 request_->Cancel(); | 1787 request_->Cancel(); |
| 1784 } | 1788 } |
| (...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2328 | 2332 |
| 2329 // Flaky, see http://crbug.com/25045. | 2333 // Flaky, see http://crbug.com/25045. |
| 2330 TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPasswordRestart) { | 2334 TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPasswordRestart) { |
| 2331 ASSERT_TRUE(NULL != server_.get()); | 2335 ASSERT_TRUE(NULL != server_.get()); |
| 2332 FilePath app_path; | 2336 FilePath app_path; |
| 2333 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); | 2337 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 2334 app_path = app_path.AppendASCII("LICENSE"); | 2338 app_path = app_path.AppendASCII("LICENSE"); |
| 2335 TestDelegate d; | 2339 TestDelegate d; |
| 2336 // Set correct login credentials. The delegate will be asked for them when | 2340 // Set correct login credentials. The delegate will be asked for them when |
| 2337 // the initial login with wrong credentials will fail. | 2341 // the initial login with wrong credentials will fail. |
| 2338 d.set_username(L"chrome"); | 2342 d.set_username(kChrome); |
| 2339 d.set_password(L"chrome"); | 2343 d.set_password(kChrome); |
| 2340 { | 2344 { |
| 2341 TestURLRequest r(server_->TestServerPage("/LICENSE", | 2345 TestURLRequest r(server_->TestServerPage("/LICENSE", |
| 2342 "chrome", "wrong_password"), &d); | 2346 "chrome", "wrong_password"), &d); |
| 2343 r.Start(); | 2347 r.Start(); |
| 2344 EXPECT_TRUE(r.is_pending()); | 2348 EXPECT_TRUE(r.is_pending()); |
| 2345 | 2349 |
| 2346 MessageLoop::current()->Run(); | 2350 MessageLoop::current()->Run(); |
| 2347 | 2351 |
| 2348 int64 file_size = 0; | 2352 int64 file_size = 0; |
| 2349 file_util::GetFileSize(app_path, &file_size); | 2353 file_util::GetFileSize(app_path, &file_size); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2381 | 2385 |
| 2382 // Flaky, see http://crbug.com/25045. | 2386 // Flaky, see http://crbug.com/25045. |
| 2383 TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUserRestart) { | 2387 TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUserRestart) { |
| 2384 ASSERT_TRUE(NULL != server_.get()); | 2388 ASSERT_TRUE(NULL != server_.get()); |
| 2385 FilePath app_path; | 2389 FilePath app_path; |
| 2386 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); | 2390 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 2387 app_path = app_path.AppendASCII("LICENSE"); | 2391 app_path = app_path.AppendASCII("LICENSE"); |
| 2388 TestDelegate d; | 2392 TestDelegate d; |
| 2389 // Set correct login credentials. The delegate will be asked for them when | 2393 // Set correct login credentials. The delegate will be asked for them when |
| 2390 // the initial login with wrong credentials will fail. | 2394 // the initial login with wrong credentials will fail. |
| 2391 d.set_username(L"chrome"); | 2395 d.set_username(kChrome); |
| 2392 d.set_password(L"chrome"); | 2396 d.set_password(kChrome); |
| 2393 { | 2397 { |
| 2394 TestURLRequest r(server_->TestServerPage("/LICENSE", | 2398 TestURLRequest r(server_->TestServerPage("/LICENSE", |
| 2395 "wrong_user", "chrome"), &d); | 2399 "wrong_user", "chrome"), &d); |
| 2396 r.Start(); | 2400 r.Start(); |
| 2397 EXPECT_TRUE(r.is_pending()); | 2401 EXPECT_TRUE(r.is_pending()); |
| 2398 | 2402 |
| 2399 MessageLoop::current()->Run(); | 2403 MessageLoop::current()->Run(); |
| 2400 | 2404 |
| 2401 int64 file_size = 0; | 2405 int64 file_size = 0; |
| 2402 file_util::GetFileSize(app_path, &file_size); | 2406 file_util::GetFileSize(app_path, &file_size); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2457 // Flaky, see http://crbug.com/25045. | 2461 // Flaky, see http://crbug.com/25045. |
| 2458 TEST_F(URLRequestTestFTP, FLAKY_FTPCacheLoginBoxCredentials) { | 2462 TEST_F(URLRequestTestFTP, FLAKY_FTPCacheLoginBoxCredentials) { |
| 2459 ASSERT_TRUE(NULL != server_.get()); | 2463 ASSERT_TRUE(NULL != server_.get()); |
| 2460 FilePath app_path; | 2464 FilePath app_path; |
| 2461 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); | 2465 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 2462 app_path = app_path.AppendASCII("LICENSE"); | 2466 app_path = app_path.AppendASCII("LICENSE"); |
| 2463 | 2467 |
| 2464 scoped_ptr<TestDelegate> d(new TestDelegate); | 2468 scoped_ptr<TestDelegate> d(new TestDelegate); |
| 2465 // Set correct login credentials. The delegate will be asked for them when | 2469 // Set correct login credentials. The delegate will be asked for them when |
| 2466 // the initial login with wrong credentials will fail. | 2470 // the initial login with wrong credentials will fail. |
| 2467 d->set_username(L"chrome"); | 2471 d->set_username(kChrome); |
| 2468 d->set_password(L"chrome"); | 2472 d->set_password(kChrome); |
| 2469 { | 2473 { |
| 2470 TestURLRequest r(server_->TestServerPage("/LICENSE", | 2474 TestURLRequest r(server_->TestServerPage("/LICENSE", |
| 2471 "chrome", "wrong_password"), | 2475 "chrome", "wrong_password"), |
| 2472 d.get()); | 2476 d.get()); |
| 2473 r.Start(); | 2477 r.Start(); |
| 2474 EXPECT_TRUE(r.is_pending()); | 2478 EXPECT_TRUE(r.is_pending()); |
| 2475 | 2479 |
| 2476 MessageLoop::current()->Run(); | 2480 MessageLoop::current()->Run(); |
| 2477 | 2481 |
| 2478 int64 file_size = 0; | 2482 int64 file_size = 0; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2552 TestURLRequest | 2556 TestURLRequest |
| 2553 req(server_->TestServerPage("echoheaderoverride?Accept-Charset"), &d); | 2557 req(server_->TestServerPage("echoheaderoverride?Accept-Charset"), &d); |
| 2554 req.set_context(new TestURLRequestContext()); | 2558 req.set_context(new TestURLRequestContext()); |
| 2555 net::HttpRequestHeaders headers; | 2559 net::HttpRequestHeaders headers; |
| 2556 headers.SetHeader(net::HttpRequestHeaders::kAcceptCharset, "koi-8r"); | 2560 headers.SetHeader(net::HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
| 2557 req.SetExtraRequestHeaders(headers); | 2561 req.SetExtraRequestHeaders(headers); |
| 2558 req.Start(); | 2562 req.Start(); |
| 2559 MessageLoop::current()->Run(); | 2563 MessageLoop::current()->Run(); |
| 2560 EXPECT_EQ(std::string("koi-8r"), d.data_received()); | 2564 EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 2561 } | 2565 } |
| OLD | NEW |