OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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 <math.h> // ceil | 5 #include <math.h> // ceil |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "net/base/completion_callback.h" | 8 #include "net/base/completion_callback.h" |
9 #include "net/base/host_resolver_unittest.h" | 9 #include "net/base/mock_host_resolver.h" |
10 #include "net/base/ssl_info.h" | 10 #include "net/base/ssl_info.h" |
11 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
12 #include "net/base/upload_data.h" | 12 #include "net/base/upload_data.h" |
13 #include "net/http/http_auth_handler_ntlm.h" | 13 #include "net/http/http_auth_handler_ntlm.h" |
14 #include "net/http/http_network_session.h" | 14 #include "net/http/http_network_session.h" |
15 #include "net/http/http_network_transaction.h" | 15 #include "net/http/http_network_transaction.h" |
16 #include "net/http/http_transaction_unittest.h" | 16 #include "net/http/http_transaction_unittest.h" |
17 #include "net/proxy/proxy_config_service_fixed.h" | 17 #include "net/proxy/proxy_config_service_fixed.h" |
18 #include "net/socket/client_socket_factory.h" | 18 #include "net/socket/client_socket_factory.h" |
19 #include "net/socket/socket_test_util.h" | 19 #include "net/socket/socket_test_util.h" |
20 #include "net/socket/ssl_client_socket.h" | 20 #include "net/socket/ssl_client_socket.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
23 | 23 |
24 //----------------------------------------------------------------------------- | 24 //----------------------------------------------------------------------------- |
25 | 25 |
26 namespace net { | 26 namespace net { |
27 | 27 |
28 // Create a proxy service which fails on all requests (falls back to direct). | 28 // Create a proxy service which fails on all requests (falls back to direct). |
29 ProxyService* CreateNullProxyService() { | 29 ProxyService* CreateNullProxyService() { |
30 return ProxyService::CreateNull(); | 30 return ProxyService::CreateNull(); |
31 } | 31 } |
32 | 32 |
33 // Helper to manage the lifetimes of the dependencies for a | 33 // Helper to manage the lifetimes of the dependencies for a |
34 // HttpNetworkTransaction. | 34 // HttpNetworkTransaction. |
35 class SessionDependencies { | 35 class SessionDependencies { |
36 public: | 36 public: |
37 // Default set of dependencies -- "null" proxy service. | 37 // Default set of dependencies -- "null" proxy service. |
38 SessionDependencies() : host_resolver(new HostResolver), | 38 SessionDependencies() : host_resolver(new MockHostResolver), |
39 proxy_service(CreateNullProxyService()) {} | 39 proxy_service(CreateNullProxyService()) {} |
40 | 40 |
41 // Custom proxy service dependency. | 41 // Custom proxy service dependency. |
42 explicit SessionDependencies(ProxyService* proxy_service) | 42 explicit SessionDependencies(ProxyService* proxy_service) |
43 : host_resolver(new HostResolver), proxy_service(proxy_service) {} | 43 : host_resolver(new MockHostResolver), proxy_service(proxy_service) {} |
44 | 44 |
45 scoped_refptr<HostResolver> host_resolver; | 45 scoped_refptr<MockHostResolver> host_resolver; |
46 scoped_ptr<ProxyService> proxy_service; | 46 scoped_ptr<ProxyService> proxy_service; |
47 MockClientSocketFactory socket_factory; | 47 MockClientSocketFactory socket_factory; |
48 }; | 48 }; |
49 | 49 |
50 ProxyService* CreateFixedProxyService(const std::string& proxy) { | 50 ProxyService* CreateFixedProxyService(const std::string& proxy) { |
51 net::ProxyConfig proxy_config; | 51 net::ProxyConfig proxy_config; |
52 proxy_config.proxy_rules.ParseFromString(proxy); | 52 proxy_config.proxy_rules.ParseFromString(proxy); |
53 return ProxyService::CreateFixed(proxy_config); | 53 return ProxyService::CreateFixed(proxy_config); |
54 } | 54 } |
55 | 55 |
(...skipping 3288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3344 TestCompletionCallback callback; | 3344 TestCompletionCallback callback; |
3345 | 3345 |
3346 // We do not complete this request, the dtor will clean the transaction up. | 3346 // We do not complete this request, the dtor will clean the transaction up. |
3347 EXPECT_EQ(ERR_IO_PENDING, trans->Start(&request, &callback)); | 3347 EXPECT_EQ(ERR_IO_PENDING, trans->Start(&request, &callback)); |
3348 EXPECT_EQ(tests[i].expected_group_name, | 3348 EXPECT_EQ(tests[i].expected_group_name, |
3349 conn_pool->last_group_name_received()); | 3349 conn_pool->last_group_name_received()); |
3350 } | 3350 } |
3351 } | 3351 } |
3352 | 3352 |
3353 TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) { | 3353 TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) { |
3354 scoped_refptr<RuleBasedHostMapper> host_mapper(new RuleBasedHostMapper()); | |
3355 ScopedHostMapper scoped_host_mapper(host_mapper.get()); | |
3356 host_mapper->AddSimulatedFailure("*"); | |
3357 | |
3358 SessionDependencies session_deps( | 3354 SessionDependencies session_deps( |
3359 CreateFixedProxyService("myproxy:70;foobar:80")); | 3355 CreateFixedProxyService("myproxy:70;foobar:80")); |
| 3356 |
| 3357 session_deps.host_resolver->rules()->AddSimulatedFailure("*"); |
| 3358 |
3360 scoped_ptr<HttpTransaction> trans( | 3359 scoped_ptr<HttpTransaction> trans( |
3361 new HttpNetworkTransaction( | 3360 new HttpNetworkTransaction( |
3362 CreateSession(&session_deps), | 3361 CreateSession(&session_deps), |
3363 &session_deps.socket_factory)); | 3362 &session_deps.socket_factory)); |
3364 | 3363 |
3365 HttpRequestInfo request; | 3364 HttpRequestInfo request; |
3366 request.method = "GET"; | 3365 request.method = "GET"; |
3367 request.url = GURL("http://www.google.com/"); | 3366 request.url = GURL("http://www.google.com/"); |
3368 | 3367 |
3369 TestCompletionCallback callback; | 3368 TestCompletionCallback callback; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3450 EXPECT_EQ(ERR_FAILED, rv); | 3449 EXPECT_EQ(ERR_FAILED, rv); |
3451 | 3450 |
3452 // Check that the host resolution observer saw |referrer|. | 3451 // Check that the host resolution observer saw |referrer|. |
3453 EXPECT_TRUE(resolution_observer.did_complete_with_expected_referrer()); | 3452 EXPECT_TRUE(resolution_observer.did_complete_with_expected_referrer()); |
3454 } | 3453 } |
3455 | 3454 |
3456 // Make sure that when the load flags contain LOAD_BYPASS_CACHE, the resolver's | 3455 // Make sure that when the load flags contain LOAD_BYPASS_CACHE, the resolver's |
3457 // host cache is bypassed. | 3456 // host cache is bypassed. |
3458 TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh) { | 3457 TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh) { |
3459 SessionDependencies session_deps; | 3458 SessionDependencies session_deps; |
| 3459 |
| 3460 // Enable caching in the mock host resolver (it is off by default). |
| 3461 session_deps.host_resolver->Reset(NULL, 100, 60000); |
| 3462 |
3460 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( | 3463 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
3461 CreateSession(&session_deps), &session_deps.socket_factory)); | 3464 CreateSession(&session_deps), &session_deps.socket_factory)); |
3462 | 3465 |
3463 // Warm up the host cache so it has an entry for "www.google.com" (by doing | 3466 // Warm up the host cache so it has an entry for "www.google.com" (by doing |
3464 // a synchronous lookup.) | 3467 // a synchronous lookup.) |
3465 AddressList addrlist; | 3468 AddressList addrlist; |
3466 int rv = session_deps.host_resolver->Resolve( | 3469 int rv = session_deps.host_resolver->Resolve( |
3467 HostResolver::RequestInfo("www.google.com", 80), &addrlist, NULL, NULL); | 3470 HostResolver::RequestInfo("www.google.com", 80), &addrlist, NULL, NULL); |
3468 EXPECT_EQ(OK, rv); | 3471 EXPECT_EQ(OK, rv); |
3469 | 3472 |
3470 // Verify that it was added to host cache, by doing a subsequent async lookup | 3473 // Verify that it was added to host cache, by doing a subsequent async lookup |
3471 // and confirming it completes synchronously. | 3474 // and confirming it completes synchronously. |
3472 TestCompletionCallback resolve_callback; | 3475 TestCompletionCallback resolve_callback; |
3473 rv = session_deps.host_resolver->Resolve( | 3476 rv = session_deps.host_resolver->Resolve( |
3474 HostResolver::RequestInfo("www.google.com", 80), &addrlist, | 3477 HostResolver::RequestInfo("www.google.com", 80), &addrlist, |
3475 &resolve_callback, NULL); | 3478 &resolve_callback, NULL); |
3476 EXPECT_EQ(OK, rv); | 3479 ASSERT_EQ(OK, rv); |
3477 | 3480 |
3478 // Inject a failure the next time that "www.google.com" is resolved. This way | 3481 // Inject a failure the next time that "www.google.com" is resolved. This way |
3479 // we can tell if the next lookup hit the cache, or the "network". | 3482 // we can tell if the next lookup hit the cache, or the "network". |
3480 // (cache --> success, "network" --> failure). | 3483 // (cache --> success, "network" --> failure). |
3481 scoped_refptr<RuleBasedHostMapper> host_mapper(new RuleBasedHostMapper()); | 3484 session_deps.host_resolver->rules()->AddSimulatedFailure("www.google.com"); |
3482 ScopedHostMapper scoped_host_mapper(host_mapper.get()); | |
3483 host_mapper->AddSimulatedFailure("www.google.com"); | |
3484 | 3485 |
3485 // Connect up a mock socket which will fail with ERR_UNEXPECTED during the | 3486 // Connect up a mock socket which will fail with ERR_UNEXPECTED during the |
3486 // first read -- this won't be reached as the host resolution will fail first. | 3487 // first read -- this won't be reached as the host resolution will fail first. |
3487 MockRead data_reads[] = { MockRead(false, ERR_UNEXPECTED) }; | 3488 MockRead data_reads[] = { MockRead(false, ERR_UNEXPECTED) }; |
3488 StaticMockSocket data(data_reads, NULL); | 3489 StaticMockSocket data(data_reads, NULL); |
3489 session_deps.socket_factory.AddMockSocket(&data); | 3490 session_deps.socket_factory.AddMockSocket(&data); |
3490 | 3491 |
3491 // Issue a request, asking to bypass the cache(s). | 3492 // Issue a request, asking to bypass the cache(s). |
3492 HttpRequestInfo request; | 3493 HttpRequestInfo request; |
3493 request.method = "GET"; | 3494 request.method = "GET"; |
3494 request.load_flags = LOAD_BYPASS_CACHE; | 3495 request.load_flags = LOAD_BYPASS_CACHE; |
3495 request.url = GURL("http://www.google.com/"); | 3496 request.url = GURL("http://www.google.com/"); |
3496 | 3497 |
3497 // Run the request. | 3498 // Run the request. |
3498 TestCompletionCallback callback; | 3499 TestCompletionCallback callback; |
3499 rv = trans->Start(&request, &callback); | 3500 rv = trans->Start(&request, &callback); |
3500 ASSERT_EQ(ERR_IO_PENDING, rv); | 3501 ASSERT_EQ(ERR_IO_PENDING, rv); |
3501 rv = callback.WaitForResult(); | 3502 rv = callback.WaitForResult(); |
3502 | 3503 |
3503 // If we bypassed the cache, we would have gotten a failure while resolving | 3504 // If we bypassed the cache, we would have gotten a failure while resolving |
3504 // "www.google.com". | 3505 // "www.google.com". |
3505 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 3506 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
3506 } | 3507 } |
3507 | 3508 |
3508 } // namespace net | 3509 } // namespace net |
OLD | NEW |