| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_impl.h" | 5 #include "net/proxy/proxy_script_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "net/base/filename_util.h" | 13 #include "net/base/filename_util.h" |
| 14 #include "net/base/load_flags.h" | 14 #include "net/base/load_flags.h" |
| 15 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
| 16 #include "net/cert/mock_cert_verifier.h" | 16 #include "net/cert/mock_cert_verifier.h" |
| 17 #include "net/disk_cache/disk_cache.h" | 17 #include "net/disk_cache/disk_cache.h" |
| 18 #include "net/dns/mock_host_resolver.h" | 18 #include "net/dns/mock_host_resolver.h" |
| 19 #include "net/http/http_cache.h" | 19 #include "net/http/http_cache.h" |
| 20 #include "net/http/http_network_session.h" | 20 #include "net/http/http_network_session.h" |
| 21 #include "net/http/http_server_properties_impl.h" | 21 #include "net/http/http_server_properties_impl.h" |
| 22 #include "net/http/transport_security_state.h" | 22 #include "net/http/transport_security_state.h" |
| 23 #include "net/ssl/ssl_config_service_defaults.h" | 23 #include "net/ssl/ssl_config_service_defaults.h" |
| 24 #include "net/test/spawned_test_server/spawned_test_server.h" | 24 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 25 #include "net/url_request/file_protocol_handler.h" | |
| 26 #include "net/url_request/url_request_context_storage.h" | 25 #include "net/url_request/url_request_context_storage.h" |
| 27 #include "net/url_request/url_request_file_job.h" | 26 #include "net/url_request/url_request_file_job.h" |
| 28 #include "net/url_request/url_request_job_factory_impl.h" | 27 #include "net/url_request/url_request_job_factory_impl.h" |
| 29 #include "net/url_request/url_request_test_util.h" | 28 #include "net/url_request/url_request_test_util.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "testing/platform_test.h" | 30 #include "testing/platform_test.h" |
| 32 | 31 |
| 32 #if !defined(DISABLE_FILE_SUPPORT) |
| 33 #include "net/url_request/file_protocol_handler.h" |
| 34 #endif |
| 35 |
| 33 using base::ASCIIToUTF16; | 36 using base::ASCIIToUTF16; |
| 34 | 37 |
| 35 namespace net { | 38 namespace net { |
| 36 | 39 |
| 37 // TODO(eroman): | 40 // TODO(eroman): |
| 38 // - Test canceling an outstanding request. | 41 // - Test canceling an outstanding request. |
| 39 // - Test deleting ProxyScriptFetcher while a request is in progress. | 42 // - Test deleting ProxyScriptFetcher while a request is in progress. |
| 40 | 43 |
| 41 namespace { | 44 namespace { |
| 42 | 45 |
| 43 const base::FilePath::CharType kDocRoot[] = | 46 const base::FilePath::CharType kDocRoot[] = |
| 44 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest"); | 47 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest"); |
| 45 | 48 |
| 46 struct FetchResult { | 49 struct FetchResult { |
| 47 int code; | 50 int code; |
| 48 base::string16 text; | 51 base::string16 text; |
| 49 }; | 52 }; |
| 50 | 53 |
| 51 // A non-mock URL request which can access http:// and file:// urls. | 54 // A non-mock URL request which can access http:// and file:// urls, in the case |
| 55 // the tests were built with file support. |
| 52 class RequestContext : public URLRequestContext { | 56 class RequestContext : public URLRequestContext { |
| 53 public: | 57 public: |
| 54 RequestContext() : storage_(this) { | 58 RequestContext() : storage_(this) { |
| 55 ProxyConfig no_proxy; | 59 ProxyConfig no_proxy; |
| 56 storage_.set_host_resolver(scoped_ptr<HostResolver>(new MockHostResolver)); | 60 storage_.set_host_resolver(scoped_ptr<HostResolver>(new MockHostResolver)); |
| 57 storage_.set_cert_verifier(new MockCertVerifier); | 61 storage_.set_cert_verifier(new MockCertVerifier); |
| 58 storage_.set_transport_security_state(new TransportSecurityState); | 62 storage_.set_transport_security_state(new TransportSecurityState); |
| 59 storage_.set_proxy_service(ProxyService::CreateFixed(no_proxy)); | 63 storage_.set_proxy_service(ProxyService::CreateFixed(no_proxy)); |
| 60 storage_.set_ssl_config_service(new SSLConfigServiceDefaults); | 64 storage_.set_ssl_config_service(new SSLConfigServiceDefaults); |
| 61 storage_.set_http_server_properties( | 65 storage_.set_http_server_properties( |
| 62 scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl())); | 66 scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl())); |
| 63 | 67 |
| 64 HttpNetworkSession::Params params; | 68 HttpNetworkSession::Params params; |
| 65 params.host_resolver = host_resolver(); | 69 params.host_resolver = host_resolver(); |
| 66 params.cert_verifier = cert_verifier(); | 70 params.cert_verifier = cert_verifier(); |
| 67 params.transport_security_state = transport_security_state(); | 71 params.transport_security_state = transport_security_state(); |
| 68 params.proxy_service = proxy_service(); | 72 params.proxy_service = proxy_service(); |
| 69 params.ssl_config_service = ssl_config_service(); | 73 params.ssl_config_service = ssl_config_service(); |
| 70 params.http_server_properties = http_server_properties(); | 74 params.http_server_properties = http_server_properties(); |
| 71 scoped_refptr<HttpNetworkSession> network_session( | 75 scoped_refptr<HttpNetworkSession> network_session( |
| 72 new HttpNetworkSession(params)); | 76 new HttpNetworkSession(params)); |
| 73 storage_.set_http_transaction_factory(new HttpCache( | 77 storage_.set_http_transaction_factory(new HttpCache( |
| 74 network_session.get(), HttpCache::DefaultBackend::InMemory(0))); | 78 network_session.get(), HttpCache::DefaultBackend::InMemory(0))); |
| 75 URLRequestJobFactoryImpl* job_factory = new URLRequestJobFactoryImpl(); | 79 URLRequestJobFactoryImpl* job_factory = new URLRequestJobFactoryImpl(); |
| 80 #if !defined(DISABLE_FILE_SUPPORT) |
| 76 job_factory->SetProtocolHandler( | 81 job_factory->SetProtocolHandler( |
| 77 "file", new FileProtocolHandler(base::MessageLoopProxy::current())); | 82 "file", new FileProtocolHandler(base::MessageLoopProxy::current())); |
| 83 #endif |
| 78 storage_.set_job_factory(job_factory); | 84 storage_.set_job_factory(job_factory); |
| 79 } | 85 } |
| 80 | 86 |
| 81 virtual ~RequestContext() { | 87 virtual ~RequestContext() { |
| 82 } | 88 } |
| 83 | 89 |
| 84 private: | 90 private: |
| 85 URLRequestContextStorage storage_; | 91 URLRequestContextStorage storage_; |
| 86 }; | 92 }; |
| 87 | 93 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 base::FilePath(kDocRoot)) { | 198 base::FilePath(kDocRoot)) { |
| 193 context_.set_network_delegate(&network_delegate_); | 199 context_.set_network_delegate(&network_delegate_); |
| 194 } | 200 } |
| 195 | 201 |
| 196 protected: | 202 protected: |
| 197 SpawnedTestServer test_server_; | 203 SpawnedTestServer test_server_; |
| 198 BasicNetworkDelegate network_delegate_; | 204 BasicNetworkDelegate network_delegate_; |
| 199 RequestContext context_; | 205 RequestContext context_; |
| 200 }; | 206 }; |
| 201 | 207 |
| 208 #if !defined(DISABLE_FILE_SUPPORT) |
| 202 TEST_F(ProxyScriptFetcherImplTest, FileUrl) { | 209 TEST_F(ProxyScriptFetcherImplTest, FileUrl) { |
| 203 ProxyScriptFetcherImpl pac_fetcher(&context_); | 210 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 204 | 211 |
| 205 { // Fetch a non-existent file. | 212 { // Fetch a non-existent file. |
| 206 base::string16 text; | 213 base::string16 text; |
| 207 TestCompletionCallback callback; | 214 TestCompletionCallback callback; |
| 208 int result = pac_fetcher.Fetch(GetTestFileUrl("does-not-exist"), | 215 int result = pac_fetcher.Fetch(GetTestFileUrl("does-not-exist"), |
| 209 &text, callback.callback()); | 216 &text, callback.callback()); |
| 210 EXPECT_EQ(ERR_IO_PENDING, result); | 217 EXPECT_EQ(ERR_IO_PENDING, result); |
| 211 EXPECT_EQ(ERR_FILE_NOT_FOUND, callback.WaitForResult()); | 218 EXPECT_EQ(ERR_FILE_NOT_FOUND, callback.WaitForResult()); |
| 212 EXPECT_TRUE(text.empty()); | 219 EXPECT_TRUE(text.empty()); |
| 213 } | 220 } |
| 214 { // Fetch a file that exists. | 221 { // Fetch a file that exists. |
| 215 base::string16 text; | 222 base::string16 text; |
| 216 TestCompletionCallback callback; | 223 TestCompletionCallback callback; |
| 217 int result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt"), | 224 int result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt"), |
| 218 &text, callback.callback()); | 225 &text, callback.callback()); |
| 219 EXPECT_EQ(ERR_IO_PENDING, result); | 226 EXPECT_EQ(ERR_IO_PENDING, result); |
| 220 EXPECT_EQ(OK, callback.WaitForResult()); | 227 EXPECT_EQ(OK, callback.WaitForResult()); |
| 221 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); | 228 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); |
| 222 } | 229 } |
| 223 } | 230 } |
| 231 #endif // !defined(DISABLE_FILE_SUPPORT) |
| 224 | 232 |
| 225 // Note that all mime types are allowed for PAC file, to be consistent | 233 // Note that all mime types are allowed for PAC file, to be consistent |
| 226 // with other browsers. | 234 // with other browsers. |
| 227 TEST_F(ProxyScriptFetcherImplTest, HttpMimeType) { | 235 TEST_F(ProxyScriptFetcherImplTest, HttpMimeType) { |
| 228 ASSERT_TRUE(test_server_.Start()); | 236 ASSERT_TRUE(test_server_.Start()); |
| 229 | 237 |
| 230 ProxyScriptFetcherImpl pac_fetcher(&context_); | 238 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 231 | 239 |
| 232 { // Fetch a PAC with mime type "text/plain" | 240 { // Fetch a PAC with mime type "text/plain" |
| 233 GURL url(test_server_.GetURL("files/pac.txt")); | 241 GURL url(test_server_.GetURL("files/pac.txt")); |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 { | 479 { |
| 472 GURL url(kEncodedUrlBroken); | 480 GURL url(kEncodedUrlBroken); |
| 473 base::string16 text; | 481 base::string16 text; |
| 474 TestCompletionCallback callback; | 482 TestCompletionCallback callback; |
| 475 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 483 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 476 EXPECT_EQ(ERR_FAILED, result); | 484 EXPECT_EQ(ERR_FAILED, result); |
| 477 } | 485 } |
| 478 } | 486 } |
| 479 | 487 |
| 480 } // namespace net | 488 } // namespace net |
| OLD | NEW |