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_service.h" | 5 #include "net/proxy/proxy_service.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 3065 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3076 // since the PAC script poller experienced a failure. | 3076 // since the PAC script poller experienced a failure. |
3077 ProxyInfo info3; | 3077 ProxyInfo info3; |
3078 TestCompletionCallback callback3; | 3078 TestCompletionCallback callback3; |
3079 rv = service.ResolveProxy( | 3079 rv = service.ResolveProxy( |
3080 GURL("http://request3"), net::LOAD_NORMAL, &info3, callback3.callback(), | 3080 GURL("http://request3"), net::LOAD_NORMAL, &info3, callback3.callback(), |
3081 NULL, NULL, BoundNetLog()); | 3081 NULL, NULL, BoundNetLog()); |
3082 EXPECT_EQ(OK, rv); | 3082 EXPECT_EQ(OK, rv); |
3083 EXPECT_TRUE(info3.is_direct()); | 3083 EXPECT_TRUE(info3.is_direct()); |
3084 } | 3084 } |
3085 | 3085 |
3086 // This tests the polling of the PAC script. Specifically, it tests that | |
Sam McNally
2015/03/09 03:36:15
Copy-paste?
Anand Mistry (off Chromium)
2015/03/10 07:24:24
Done.
| |
3087 // polling occurs in response to user activity. | |
3088 TEST_F(ProxyServiceTest, PACScriptRefetchAfterTerminatedResolveProxy) { | |
3089 ProxyConfig config( | |
3090 ProxyConfig::CreateFromCustomPacURL(GURL("http://foopy/proxy.pac"))); | |
3091 config.set_pac_mandatory(true); | |
3092 MockProxyConfigService* config_service = new MockProxyConfigService(config); | |
3093 | |
3094 MockAsyncProxyResolverExpectsBytes* resolver = | |
3095 new MockAsyncProxyResolverExpectsBytes; | |
3096 | |
3097 ProxyService service(config_service, resolver, NULL); | |
3098 | |
3099 MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher; | |
3100 service.SetProxyScriptFetchers(fetcher, | |
3101 new DoNothingDhcpProxyScriptFetcher()); | |
3102 | |
3103 // Start 1 request. | |
3104 ProxyInfo info1; | |
3105 TestCompletionCallback callback1; | |
3106 int rv = | |
3107 service.ResolveProxy(GURL("http://request1"), net::LOAD_NORMAL, &info1, | |
3108 callback1.callback(), NULL, NULL, BoundNetLog()); | |
3109 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3110 | |
3111 // The first request should have triggered initial download of PAC script. | |
3112 EXPECT_TRUE(fetcher->has_pending_request()); | |
3113 EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url()); | |
3114 | |
3115 // Nothing has been sent to the resolver yet. | |
3116 EXPECT_TRUE(resolver->pending_requests().empty()); | |
3117 | |
3118 // At this point the ProxyService should be waiting for the | |
3119 // ProxyScriptFetcher to invoke its completion callback, notifying it of | |
3120 // PAC script download completion. | |
3121 fetcher->NotifyFetchCompletion(OK, kValidPacScript1); | |
3122 | |
3123 // Now that the PAC script is downloaded, the request will have been sent to | |
3124 // the proxy resolver. | |
3125 EXPECT_EQ(ASCIIToUTF16(kValidPacScript1), | |
3126 resolver->pending_set_pac_script_request()->script_data()->utf16()); | |
3127 resolver->pending_set_pac_script_request()->CompleteNow(OK); | |
3128 | |
3129 ASSERT_EQ(1u, resolver->pending_requests().size()); | |
3130 EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url()); | |
3131 | |
3132 // Complete the pending request. | |
3133 resolver->pending_requests()[0]->results()->UseNamedProxy("request1:80"); | |
3134 resolver->pending_requests()[0]->CompleteNow(OK); | |
3135 | |
3136 // Wait for completion callback, and verify that the request ran as expected. | |
3137 EXPECT_EQ(OK, callback1.WaitForResult()); | |
3138 EXPECT_EQ("request1:80", info1.proxy_server().ToURI()); | |
3139 | |
3140 // At this point we have initialized the proxy service using a PAC script. | |
3141 // Our PAC poller is set to update ONLY in response to network activity, | |
3142 // (i.e. another call to ResolveProxy()). | |
3143 | |
3144 ASSERT_FALSE(fetcher->has_pending_request()); | |
3145 ASSERT_TRUE(resolver->pending_requests().empty()); | |
3146 | |
3147 // Start a second request and third request. | |
3148 ProxyInfo info2; | |
3149 TestCompletionCallback callback2; | |
3150 rv = service.ResolveProxy(GURL("http://request2"), net::LOAD_NORMAL, &info2, | |
3151 callback2.callback(), NULL, NULL, BoundNetLog()); | |
3152 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3153 | |
3154 ProxyInfo info3; | |
3155 TestCompletionCallback callback3; | |
3156 rv = service.ResolveProxy(GURL("http://request3"), net::LOAD_NORMAL, &info3, | |
3157 callback3.callback(), NULL, NULL, BoundNetLog()); | |
3158 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3159 | |
3160 // Fail request 2 with ERR_PAC_SCRIPT_TERMINATED, which indicates the PAC | |
3161 // script has terminated fatally and needs to be reloaded. Request 3's resolve | |
3162 // request should be cancelled, but ProxyService keeps it and retries it | |
3163 // later. | |
3164 ASSERT_EQ(2u, resolver->pending_requests().size()); | |
3165 EXPECT_EQ(GURL("http://request2"), resolver->pending_requests()[0]->url()); | |
3166 EXPECT_EQ(GURL("http://request3"), resolver->pending_requests()[1]->url()); | |
3167 resolver->pending_requests()[0]->CompleteNow(ERR_PAC_SCRIPT_TERMINATED); | |
3168 EXPECT_EQ(ERR_MANDATORY_PROXY_CONFIGURATION_FAILED, | |
3169 callback2.WaitForResult()); | |
3170 | |
3171 ASSERT_EQ(0u, resolver->pending_requests().size()); | |
3172 ASSERT_EQ(1u, resolver->cancelled_requests().size()); | |
3173 | |
3174 // Since there's an active request, the proxy script should automatically be | |
3175 // re-fetched. | |
3176 EXPECT_TRUE(fetcher->has_pending_request()); | |
3177 fetcher->NotifyFetchCompletion(OK, kValidPacScript1); | |
3178 EXPECT_EQ(ASCIIToUTF16(kValidPacScript1), | |
3179 resolver->pending_set_pac_script_request()->script_data()->utf16()); | |
3180 resolver->pending_set_pac_script_request()->CompleteNow(OK); | |
3181 ASSERT_EQ(1u, resolver->pending_requests().size()); | |
3182 EXPECT_EQ(GURL("http://request3"), resolver->pending_requests()[0]->url()); | |
3183 resolver->pending_requests()[0]->results()->UseNamedProxy("request3:80"); | |
3184 resolver->pending_requests()[0]->CompleteNow(OK); | |
3185 | |
3186 EXPECT_EQ(OK, callback3.WaitForResult()); | |
3187 | |
3188 // Do another request which fails with ERR_PAC_SCRIPT_TERMINATED. This time, | |
3189 // there are no pending requests, so don't re-fetch the script until the next | |
3190 // request comes in. | |
3191 ProxyInfo info4; | |
3192 TestCompletionCallback callback4; | |
3193 rv = service.ResolveProxy(GURL("http://request4"), net::LOAD_NORMAL, &info4, | |
3194 callback4.callback(), NULL, NULL, BoundNetLog()); | |
3195 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3196 ASSERT_EQ(1u, resolver->pending_requests().size()); | |
3197 resolver->pending_requests()[0]->CompleteNow(ERR_PAC_SCRIPT_TERMINATED); | |
3198 | |
3199 // There should be no automatic fetch request this time. | |
3200 EXPECT_FALSE(fetcher->has_pending_request()); | |
3201 | |
3202 // Now, the next request should cause a fetch. | |
3203 ProxyInfo info5; | |
3204 TestCompletionCallback callback5; | |
3205 rv = service.ResolveProxy(GURL("http://request5"), net::LOAD_NORMAL, &info5, | |
3206 callback5.callback(), NULL, NULL, BoundNetLog()); | |
3207 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3208 // No pending requests to the resolver since a script fetch is in progress. | |
3209 EXPECT_TRUE(resolver->pending_requests().empty()); | |
3210 | |
3211 // Now there should be a PAC script fetch. | |
3212 EXPECT_TRUE(fetcher->has_pending_request()); | |
3213 | |
3214 // Finish and verify. | |
3215 fetcher->NotifyFetchCompletion(OK, kValidPacScript1); | |
3216 EXPECT_EQ(ASCIIToUTF16(kValidPacScript1), | |
3217 resolver->pending_set_pac_script_request()->script_data()->utf16()); | |
3218 resolver->pending_set_pac_script_request()->CompleteNow(OK); | |
3219 ASSERT_EQ(1u, resolver->pending_requests().size()); | |
3220 EXPECT_EQ(GURL("http://request5"), resolver->pending_requests()[0]->url()); | |
3221 resolver->pending_requests()[0]->results()->UseNamedProxy("request5:80"); | |
3222 resolver->pending_requests()[0]->CompleteNow(OK); | |
3223 EXPECT_EQ(OK, callback5.WaitForResult()); | |
3224 EXPECT_EQ("request5:80", info5.proxy_server().ToURI()); | |
3225 } | |
3226 | |
3086 // Test that the synchronous resolution fails when a PAC script is active. | 3227 // Test that the synchronous resolution fails when a PAC script is active. |
3087 TEST_F(ProxyServiceTest, SynchronousWithPAC) { | 3228 TEST_F(ProxyServiceTest, SynchronousWithPAC) { |
3088 MockProxyConfigService* config_service = | 3229 MockProxyConfigService* config_service = |
3089 new MockProxyConfigService("http://foopy/proxy.pac"); | 3230 new MockProxyConfigService("http://foopy/proxy.pac"); |
3090 | 3231 |
3091 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver(); | 3232 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver(); |
3092 | 3233 |
3093 ProxyService service(config_service, resolver, NULL); | 3234 ProxyService service(config_service, resolver, NULL); |
3094 | 3235 |
3095 GURL url("http://www.google.com/"); | 3236 GURL url("http://www.google.com/"); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3129 url, net::LOAD_NORMAL, &info, NULL, log.bound()); | 3270 url, net::LOAD_NORMAL, &info, NULL, log.bound()); |
3130 EXPECT_TRUE(synchronous_success); | 3271 EXPECT_TRUE(synchronous_success); |
3131 EXPECT_FALSE(info.is_direct()); | 3272 EXPECT_FALSE(info.is_direct()); |
3132 EXPECT_EQ("foopy1", info.proxy_server().host_port_pair().host()); | 3273 EXPECT_EQ("foopy1", info.proxy_server().host_port_pair().host()); |
3133 | 3274 |
3134 // No request should have been queued. | 3275 // No request should have been queued. |
3135 EXPECT_EQ(0u, resolver->pending_requests().size()); | 3276 EXPECT_EQ(0u, resolver->pending_requests().size()); |
3136 } | 3277 } |
3137 | 3278 |
3138 } // namespace net | 3279 } // namespace net |
OLD | NEW |