| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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/proxy/proxy_script_fetcher.h" | 5 #include "net/proxy/proxy_script_fetcher.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
| 11 #include "net/disk_cache/disk_cache.h" |
| 12 #include "net/http/http_cache.h" |
| 11 #include "net/url_request/url_request_unittest.h" | 13 #include "net/url_request/url_request_unittest.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 14 | 16 |
| 15 // TODO(eroman): | 17 // TODO(eroman): |
| 16 // - Test canceling an outstanding request. | 18 // - Test canceling an outstanding request. |
| 17 // - Test deleting ProxyScriptFetcher while a request is in progress. | 19 // - Test deleting ProxyScriptFetcher while a request is in progress. |
| 18 | 20 |
| 19 const wchar_t kDocRoot[] = L"net/data/proxy_script_fetcher_unittest"; | 21 const wchar_t kDocRoot[] = L"net/data/proxy_script_fetcher_unittest"; |
| 20 | 22 |
| 21 struct FetchResult { | 23 struct FetchResult { |
| 22 int code; | 24 int code; |
| 23 std::string bytes; | 25 std::string bytes; |
| 24 }; | 26 }; |
| 25 | 27 |
| 26 // A non-mock URL request which can access http:// and file:// urls. | 28 // A non-mock URL request which can access http:// and file:// urls. |
| 27 class RequestContext : public URLRequestContext { | 29 class RequestContext : public URLRequestContext { |
| 28 public: | 30 public: |
| 29 RequestContext() { | 31 RequestContext() { |
| 30 net::ProxyConfig no_proxy; | 32 net::ProxyConfig no_proxy; |
| 31 proxy_service_ = net::ProxyService::CreateFixed(no_proxy); | 33 proxy_service_ = net::ProxyService::CreateFixed(no_proxy); |
| 32 http_transaction_factory_ = net::HttpNetworkLayer::CreateFactory( | 34 |
| 33 proxy_service_); | 35 http_transaction_factory_ = |
| 36 new net::HttpCache(net::HttpNetworkLayer::CreateFactory(proxy_service_), |
| 37 disk_cache::CreateInMemoryCacheBackend(0)); |
| 34 } | 38 } |
| 35 ~RequestContext() { | 39 ~RequestContext() { |
| 36 delete http_transaction_factory_; | 40 delete http_transaction_factory_; |
| 37 delete proxy_service_; | 41 delete proxy_service_; |
| 38 } | 42 } |
| 39 }; | 43 }; |
| 40 | 44 |
| 41 // Helper for doing synch fetches. This object lives in SynchFetcher's | 45 // Helper for doing synch fetches. This object lives in SynchFetcher's |
| 42 // |io_thread_| and communicates with SynchFetcher though (|result|, |event|). | 46 // |io_thread_| and communicates with SynchFetcher though (|result|, |event|). |
| 43 class SynchFetcherThreadHelper { | 47 class SynchFetcherThreadHelper { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 SynchFetcher pac_fetcher; | 237 SynchFetcher pac_fetcher; |
| 234 | 238 |
| 235 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should | 239 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should |
| 236 // have no effect. | 240 // have no effect. |
| 237 GURL url = server->TestServerPage("files/downloadable.pac"); | 241 GURL url = server->TestServerPage("files/downloadable.pac"); |
| 238 FetchResult result = pac_fetcher.Fetch(url); | 242 FetchResult result = pac_fetcher.Fetch(url); |
| 239 EXPECT_EQ(net::OK, result.code); | 243 EXPECT_EQ(net::OK, result.code); |
| 240 EXPECT_EQ("-downloadable.pac-\n", result.bytes); | 244 EXPECT_EQ("-downloadable.pac-\n", result.bytes); |
| 241 } | 245 } |
| 242 | 246 |
| 247 TEST_F(ProxyScriptFetcherTest, NoCache) { |
| 248 scoped_refptr<HTTPTestServer> server = |
| 249 HTTPTestServer::CreateServer(kDocRoot, NULL); |
| 250 ASSERT_TRUE(NULL != server.get()); |
| 251 SynchFetcher pac_fetcher; |
| 252 |
| 253 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour. |
| 254 GURL url = server->TestServerPage("files/cacheable_1hr.pac"); |
| 255 FetchResult result = pac_fetcher.Fetch(url); |
| 256 EXPECT_EQ(net::OK, result.code); |
| 257 EXPECT_EQ("-cacheable_1hr.pac-\n", result.bytes); |
| 258 |
| 259 // Now kill the HTTP server. |
| 260 server->SendQuit(); |
| 261 EXPECT_TRUE(server->WaitToFinish(20000)); |
| 262 server = NULL; |
| 263 |
| 264 // Try to fetch the file again -- if should fail, since the server is not |
| 265 // running anymore. (If it were instead being loaded from cache, we would |
| 266 // get a success. |
| 267 result = pac_fetcher.Fetch(url); |
| 268 EXPECT_EQ(net::ERR_CONNECTION_REFUSED, result.code); |
| 269 } |
| 270 |
| 243 TEST_F(ProxyScriptFetcherTest, TooLarge) { | 271 TEST_F(ProxyScriptFetcherTest, TooLarge) { |
| 244 scoped_refptr<HTTPTestServer> server = | 272 scoped_refptr<HTTPTestServer> server = |
| 245 HTTPTestServer::CreateServer(kDocRoot, NULL); | 273 HTTPTestServer::CreateServer(kDocRoot, NULL); |
| 246 ASSERT_TRUE(NULL != server.get()); | 274 ASSERT_TRUE(NULL != server.get()); |
| 247 SynchFetcher pac_fetcher; | 275 SynchFetcher pac_fetcher; |
| 248 | 276 |
| 249 // Set the maximum response size to 50 bytes. | 277 // Set the maximum response size to 50 bytes. |
| 250 int prev_size = net::ProxyScriptFetcher::SetSizeConstraintForUnittest(50); | 278 int prev_size = net::ProxyScriptFetcher::SetSizeConstraintForUnittest(50); |
| 251 | 279 |
| 252 // These two URLs are the same file, but are http:// vs file:// | 280 // These two URLs are the same file, but are http:// vs file:// |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 | 326 |
| 299 { // Make sure we can still fetch regular URLs. | 327 { // Make sure we can still fetch regular URLs. |
| 300 GURL url = server->TestServerPage("files/pac.nsproxy"); | 328 GURL url = server->TestServerPage("files/pac.nsproxy"); |
| 301 FetchResult result = pac_fetcher.Fetch(url); | 329 FetchResult result = pac_fetcher.Fetch(url); |
| 302 EXPECT_EQ(net::OK, result.code); | 330 EXPECT_EQ(net::OK, result.code); |
| 303 EXPECT_EQ("-pac.nsproxy-\n", result.bytes); | 331 EXPECT_EQ("-pac.nsproxy-\n", result.bytes); |
| 304 } | 332 } |
| 305 } | 333 } |
| 306 | 334 |
| 307 } // namespace net | 335 } // namespace net |
| OLD | NEW |