| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
| 12 #include "net/proxy/proxy_config_service.h" | 12 #include "net/proxy/proxy_config_service.h" |
| 13 #include "net/proxy/proxy_resolver.h" | 13 #include "net/proxy/proxy_resolver.h" |
| 14 #include "net/proxy/proxy_script_fetcher.h" | 14 #include "net/proxy/proxy_script_fetcher.h" |
| 15 #include "net/proxy/proxy_service.h" | 15 #include "net/proxy/proxy_service.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 // TODO(eroman): Write a test which exercises |
| 19 // ProxyService::SuspendAllPendingRequests(). |
| 18 namespace net { | 20 namespace net { |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 class MockProxyConfigService: public ProxyConfigService { | 23 class MockProxyConfigService: public ProxyConfigService { |
| 22 public: | 24 public: |
| 23 MockProxyConfigService() {} // Direct connect. | 25 MockProxyConfigService() {} // Direct connect. |
| 24 explicit MockProxyConfigService(const ProxyConfig& pc) : config(pc) {} | 26 explicit MockProxyConfigService(const ProxyConfig& pc) : config(pc) {} |
| 25 explicit MockProxyConfigService(const std::string& pac_url) { | 27 explicit MockProxyConfigService(const std::string& pac_url) { |
| 26 config.pac_url = GURL(pac_url); | 28 config.pac_url = GURL(pac_url); |
| 27 } | 29 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 } | 67 } |
| 66 | 68 |
| 67 private: | 69 private: |
| 68 MockAsyncProxyResolverBase* resolver_; | 70 MockAsyncProxyResolverBase* resolver_; |
| 69 const GURL url_; | 71 const GURL url_; |
| 70 ProxyInfo* results_; | 72 ProxyInfo* results_; |
| 71 CompletionCallback* callback_; | 73 CompletionCallback* callback_; |
| 72 MessageLoop* origin_loop_; | 74 MessageLoop* origin_loop_; |
| 73 }; | 75 }; |
| 74 | 76 |
| 77 class SetPacScriptRequest { |
| 78 public: |
| 79 SetPacScriptRequest(MockAsyncProxyResolverBase* resolver, |
| 80 const GURL& pac_url, |
| 81 const std::string& pac_bytes, |
| 82 CompletionCallback* callback) |
| 83 : resolver_(resolver), |
| 84 pac_url_(pac_url), |
| 85 pac_bytes_(pac_bytes), |
| 86 callback_(callback), |
| 87 origin_loop_(MessageLoop::current()) { |
| 88 } |
| 89 |
| 90 const GURL& pac_url() const { return pac_url_; } |
| 91 const std::string& pac_bytes() const { return pac_bytes_; } |
| 92 |
| 93 void CompleteNow(int rv) { |
| 94 CompletionCallback* callback = callback_; |
| 95 |
| 96 // Will delete |this|. |
| 97 resolver_->RemovePendingSetPacScriptRequest(this); |
| 98 |
| 99 callback->Run(rv); |
| 100 } |
| 101 |
| 102 private: |
| 103 MockAsyncProxyResolverBase* resolver_; |
| 104 const GURL pac_url_; |
| 105 const std::string pac_bytes_; |
| 106 CompletionCallback* callback_; |
| 107 MessageLoop* origin_loop_; |
| 108 }; |
| 109 |
| 75 typedef std::vector<scoped_refptr<Request> > RequestsList; | 110 typedef std::vector<scoped_refptr<Request> > RequestsList; |
| 76 | 111 |
| 77 // ProxyResolver implementation: | 112 // ProxyResolver implementation: |
| 78 virtual int GetProxyForURL(const GURL& url, | 113 virtual int GetProxyForURL(const GURL& url, |
| 79 ProxyInfo* results, | 114 ProxyInfo* results, |
| 80 CompletionCallback* callback, | 115 CompletionCallback* callback, |
| 81 RequestHandle* request_handle) { | 116 RequestHandle* request_handle) { |
| 82 scoped_refptr<Request> request = new Request(this, url, results, callback); | 117 scoped_refptr<Request> request = new Request(this, url, results, callback); |
| 83 pending_requests_.push_back(request); | 118 pending_requests_.push_back(request); |
| 84 | 119 |
| 85 if (request_handle) | 120 if (request_handle) |
| 86 *request_handle = reinterpret_cast<RequestHandle>(request.get()); | 121 *request_handle = reinterpret_cast<RequestHandle>(request.get()); |
| 87 | 122 |
| 88 // Test code completes the request by calling request->CompleteNow(). | 123 // Test code completes the request by calling request->CompleteNow(). |
| 89 return ERR_IO_PENDING; | 124 return ERR_IO_PENDING; |
| 90 } | 125 } |
| 91 | 126 |
| 92 virtual void CancelRequest(RequestHandle request_handle) { | 127 virtual void CancelRequest(RequestHandle request_handle) { |
| 93 scoped_refptr<Request> request = reinterpret_cast<Request*>(request_handle); | 128 scoped_refptr<Request> request = reinterpret_cast<Request*>(request_handle); |
| 94 cancelled_requests_.push_back(request); | 129 cancelled_requests_.push_back(request); |
| 95 RemovePendingRequest(request); | 130 RemovePendingRequest(request); |
| 96 } | 131 } |
| 97 | 132 |
| 98 virtual void SetPacScriptByUrlInternal(const GURL& pac_url) { | 133 virtual int SetPacScript(const GURL& pac_url, |
| 99 pac_url_ = pac_url; | 134 const std::string& pac_bytes, |
| 100 } | 135 CompletionCallback* callback) { |
| 101 | 136 EXPECT_EQ(NULL, pending_set_pac_script_request_.get()); |
| 102 virtual void SetPacScriptByDataInternal(const std::string& bytes) { | 137 pending_set_pac_script_request_.reset( |
| 103 pac_bytes_ = bytes; | 138 new SetPacScriptRequest(this, pac_url, pac_bytes, callback)); |
| 139 // Finished when user calls SetPacScriptRequest::CompleteNow(). |
| 140 return ERR_IO_PENDING; |
| 104 } | 141 } |
| 105 | 142 |
| 106 const RequestsList& pending_requests() const { | 143 const RequestsList& pending_requests() const { |
| 107 return pending_requests_; | 144 return pending_requests_; |
| 108 } | 145 } |
| 109 | 146 |
| 110 const RequestsList& cancelled_requests() const { | 147 const RequestsList& cancelled_requests() const { |
| 111 return cancelled_requests_; | 148 return cancelled_requests_; |
| 112 } | 149 } |
| 113 | 150 |
| 114 const GURL& pac_url() const { | 151 SetPacScriptRequest* pending_set_pac_script_request() const { |
| 115 return pac_url_; | 152 return pending_set_pac_script_request_.get(); |
| 116 } | |
| 117 | |
| 118 const std::string& pac_bytes() const { | |
| 119 return pac_bytes_; | |
| 120 } | 153 } |
| 121 | 154 |
| 122 void RemovePendingRequest(Request* request) { | 155 void RemovePendingRequest(Request* request) { |
| 123 RequestsList::iterator it = std::find( | 156 RequestsList::iterator it = std::find( |
| 124 pending_requests_.begin(), pending_requests_.end(), request); | 157 pending_requests_.begin(), pending_requests_.end(), request); |
| 125 DCHECK(it != pending_requests_.end()); | 158 DCHECK(it != pending_requests_.end()); |
| 126 pending_requests_.erase(it); | 159 pending_requests_.erase(it); |
| 127 } | 160 } |
| 128 | 161 |
| 162 void RemovePendingSetPacScriptRequest(SetPacScriptRequest* request) { |
| 163 EXPECT_EQ(request, pending_set_pac_script_request()); |
| 164 pending_set_pac_script_request_.reset(); |
| 165 } |
| 166 |
| 129 protected: | 167 protected: |
| 130 explicit MockAsyncProxyResolverBase(bool expects_pac_bytes) | 168 explicit MockAsyncProxyResolverBase(bool expects_pac_bytes) |
| 131 : ProxyResolver(expects_pac_bytes) {} | 169 : ProxyResolver(expects_pac_bytes) {} |
| 132 | 170 |
| 133 private: | 171 private: |
| 134 RequestsList pending_requests_; | 172 RequestsList pending_requests_; |
| 135 RequestsList cancelled_requests_; | 173 RequestsList cancelled_requests_; |
| 136 GURL pac_url_; | 174 scoped_ptr<SetPacScriptRequest> pending_set_pac_script_request_; |
| 137 std::string pac_bytes_; | |
| 138 }; | 175 }; |
| 139 | 176 |
| 140 class MockAsyncProxyResolver : public MockAsyncProxyResolverBase { | 177 class MockAsyncProxyResolver : public MockAsyncProxyResolverBase { |
| 141 public: | 178 public: |
| 142 MockAsyncProxyResolver() | 179 MockAsyncProxyResolver() |
| 143 : MockAsyncProxyResolverBase(false /*expects_pac_bytes*/) {} | 180 : MockAsyncProxyResolverBase(false /*expects_pac_bytes*/) {} |
| 144 }; | 181 }; |
| 145 | 182 |
| 146 class MockAsyncProxyResolverExpectsBytes : public MockAsyncProxyResolverBase { | 183 class MockAsyncProxyResolverExpectsBytes : public MockAsyncProxyResolverBase { |
| 147 public: | 184 public: |
| 148 MockAsyncProxyResolverExpectsBytes() | 185 MockAsyncProxyResolverExpectsBytes() |
| 149 : MockAsyncProxyResolverBase(true /*expects_pac_bytes*/) {} | 186 : MockAsyncProxyResolverBase(true /*expects_pac_bytes*/) {} |
| 150 }; | 187 }; |
| 151 | 188 |
| 152 } // namespace | 189 } // namespace |
| 153 | 190 |
| 154 // A mock ProxyScriptFetcher. No result will be returned to the fetch client | 191 // A mock ProxyScriptFetcher. No result will be returned to the fetch client |
| 155 // until we call NotifyFetchCompletion() to set the results. | 192 // until we call NotifyFetchCompletion() to set the results. |
| 156 class MockProxyScriptFetcher : public ProxyScriptFetcher { | 193 class MockProxyScriptFetcher : public ProxyScriptFetcher { |
| 157 public: | 194 public: |
| 158 MockProxyScriptFetcher() | 195 MockProxyScriptFetcher() |
| 159 : pending_request_callback_(NULL), pending_request_bytes_(NULL) { | 196 : pending_request_callback_(NULL), pending_request_bytes_(NULL) { |
| 160 } | 197 } |
| 161 | 198 |
| 162 // ProxyScriptFetcher implementation. | 199 // ProxyScriptFetcher implementation. |
| 163 virtual void Fetch(const GURL& url, | 200 virtual int Fetch(const GURL& url, |
| 164 std::string* bytes, | 201 std::string* bytes, |
| 165 CompletionCallback* callback) { | 202 CompletionCallback* callback) { |
| 166 DCHECK(!has_pending_request()); | 203 DCHECK(!has_pending_request()); |
| 167 | 204 |
| 168 // Save the caller's information, and have them wait. | 205 // Save the caller's information, and have them wait. |
| 169 pending_request_url_ = url; | 206 pending_request_url_ = url; |
| 170 pending_request_callback_ = callback; | 207 pending_request_callback_ = callback; |
| 171 pending_request_bytes_ = bytes; | 208 pending_request_bytes_ = bytes; |
| 209 return ERR_IO_PENDING; |
| 172 } | 210 } |
| 173 | 211 |
| 174 void NotifyFetchCompletion(int result, const std::string& bytes) { | 212 void NotifyFetchCompletion(int result, const std::string& bytes) { |
| 175 DCHECK(has_pending_request()); | 213 DCHECK(has_pending_request()); |
| 176 *pending_request_bytes_ = bytes; | 214 *pending_request_bytes_ = bytes; |
| 177 pending_request_callback_->Run(result); | 215 CompletionCallback* callback = pending_request_callback_; |
| 216 pending_request_callback_ = NULL; |
| 217 callback->Run(result); |
| 178 } | 218 } |
| 179 | 219 |
| 180 virtual void Cancel() {} | 220 virtual void Cancel() {} |
| 181 | 221 |
| 182 const GURL& pending_request_url() const { | 222 const GURL& pending_request_url() const { |
| 183 return pending_request_url_; | 223 return pending_request_url_; |
| 184 } | 224 } |
| 185 | 225 |
| 186 bool has_pending_request() const { | 226 bool has_pending_request() const { |
| 187 return pending_request_callback_ != NULL; | 227 return pending_request_callback_ != NULL; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 216 | 256 |
| 217 ProxyService service(config_service, resolver); | 257 ProxyService service(config_service, resolver); |
| 218 | 258 |
| 219 GURL url("http://www.google.com/"); | 259 GURL url("http://www.google.com/"); |
| 220 | 260 |
| 221 ProxyInfo info; | 261 ProxyInfo info; |
| 222 TestCompletionCallback callback; | 262 TestCompletionCallback callback; |
| 223 int rv = service.ResolveProxy(url, &info, &callback, NULL); | 263 int rv = service.ResolveProxy(url, &info, &callback, NULL); |
| 224 EXPECT_EQ(ERR_IO_PENDING, rv); | 264 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 225 | 265 |
| 226 EXPECT_EQ(GURL("http://foopy/proxy.pac"), resolver->pac_url()); | 266 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 267 resolver->pending_set_pac_script_request()->pac_url()); |
| 268 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 269 |
| 227 ASSERT_EQ(1u, resolver->pending_requests().size()); | 270 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 228 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); | 271 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); |
| 229 | 272 |
| 230 // Set the result in proxy resolver. | 273 // Set the result in proxy resolver. |
| 231 resolver->pending_requests()[0]->results()->UseNamedProxy("foopy"); | 274 resolver->pending_requests()[0]->results()->UseNamedProxy("foopy"); |
| 232 resolver->pending_requests()[0]->CompleteNow(OK); | 275 resolver->pending_requests()[0]->CompleteNow(OK); |
| 233 | 276 |
| 234 EXPECT_EQ(OK, callback.WaitForResult()); | 277 EXPECT_EQ(OK, callback.WaitForResult()); |
| 235 EXPECT_FALSE(info.is_direct()); | 278 EXPECT_FALSE(info.is_direct()); |
| 236 EXPECT_EQ("foopy:80", info.proxy_server().ToURI()); | 279 EXPECT_EQ("foopy:80", info.proxy_server().ToURI()); |
| 237 } | 280 } |
| 238 | 281 |
| 239 // Test that the proxy resolver does not see the URL's username/password | 282 // Test that the proxy resolver does not see the URL's username/password |
| 240 // or its reference section. | 283 // or its reference section. |
| 241 TEST(ProxyServiceTest, PAC_NoIdentityOrHash) { | 284 TEST(ProxyServiceTest, PAC_NoIdentityOrHash) { |
| 242 MockProxyConfigService* config_service = | 285 MockProxyConfigService* config_service = |
| 243 new MockProxyConfigService("http://foopy/proxy.pac"); | 286 new MockProxyConfigService("http://foopy/proxy.pac"); |
| 244 | 287 |
| 245 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; | 288 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; |
| 246 | 289 |
| 247 ProxyService service(config_service, resolver); | 290 ProxyService service(config_service, resolver); |
| 248 | 291 |
| 249 GURL url("http://username:password@www.google.com/?ref#hash#hash"); | 292 GURL url("http://username:password@www.google.com/?ref#hash#hash"); |
| 250 | 293 |
| 251 ProxyInfo info; | 294 ProxyInfo info; |
| 252 TestCompletionCallback callback; | 295 TestCompletionCallback callback; |
| 253 int rv = service.ResolveProxy(url, &info, &callback, NULL); | 296 int rv = service.ResolveProxy(url, &info, &callback, NULL); |
| 254 EXPECT_EQ(ERR_IO_PENDING, rv); | 297 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 255 | 298 |
| 256 EXPECT_EQ(GURL("http://foopy/proxy.pac"), resolver->pac_url()); | 299 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 300 resolver->pending_set_pac_script_request()->pac_url()); |
| 301 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 302 |
| 257 ASSERT_EQ(1u, resolver->pending_requests().size()); | 303 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 258 // The URL should have been simplified, stripping the username/password/hash. | 304 // The URL should have been simplified, stripping the username/password/hash. |
| 259 EXPECT_EQ(GURL("http://www.google.com/?ref"), | 305 EXPECT_EQ(GURL("http://www.google.com/?ref"), |
| 260 resolver->pending_requests()[0]->url()); | 306 resolver->pending_requests()[0]->url()); |
| 261 | 307 |
| 262 // We end here without ever completing the request -- destruction of | 308 // We end here without ever completing the request -- destruction of |
| 263 // ProxyService will cancel the outstanding request. | 309 // ProxyService will cancel the outstanding request. |
| 264 } | 310 } |
| 265 | 311 |
| 266 TEST(ProxyServiceTest, PAC_FailoverToDirect) { | 312 TEST(ProxyServiceTest, PAC_FailoverToDirect) { |
| 267 MockProxyConfigService* config_service = | 313 MockProxyConfigService* config_service = |
| 268 new MockProxyConfigService("http://foopy/proxy.pac"); | 314 new MockProxyConfigService("http://foopy/proxy.pac"); |
| 269 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; | 315 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; |
| 270 | 316 |
| 271 ProxyService service(config_service, resolver); | 317 ProxyService service(config_service, resolver); |
| 272 | 318 |
| 273 GURL url("http://www.google.com/"); | 319 GURL url("http://www.google.com/"); |
| 274 | 320 |
| 275 ProxyInfo info; | 321 ProxyInfo info; |
| 276 TestCompletionCallback callback1; | 322 TestCompletionCallback callback1; |
| 277 int rv = service.ResolveProxy(url, &info, &callback1, NULL); | 323 int rv = service.ResolveProxy(url, &info, &callback1, NULL); |
| 278 EXPECT_EQ(ERR_IO_PENDING, rv); | 324 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 279 | 325 |
| 280 EXPECT_EQ(GURL("http://foopy/proxy.pac"), resolver->pac_url()); | 326 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 327 resolver->pending_set_pac_script_request()->pac_url()); |
| 328 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 329 |
| 281 ASSERT_EQ(1u, resolver->pending_requests().size()); | 330 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 282 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); | 331 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); |
| 283 | 332 |
| 284 // Set the result in proxy resolver. | 333 // Set the result in proxy resolver. |
| 285 resolver->pending_requests()[0]->results()->UseNamedProxy("foopy:8080"); | 334 resolver->pending_requests()[0]->results()->UseNamedProxy("foopy:8080"); |
| 286 resolver->pending_requests()[0]->CompleteNow(OK); | 335 resolver->pending_requests()[0]->CompleteNow(OK); |
| 287 | 336 |
| 288 EXPECT_EQ(OK, callback1.WaitForResult()); | 337 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 289 EXPECT_FALSE(info.is_direct()); | 338 EXPECT_FALSE(info.is_direct()); |
| 290 EXPECT_EQ("foopy:8080", info.proxy_server().ToURI()); | 339 EXPECT_EQ("foopy:8080", info.proxy_server().ToURI()); |
| 291 | 340 |
| 292 // Now, imagine that connecting to foopy:8080 fails. | 341 // Now, imagine that connecting to foopy:8080 fails. |
| 293 TestCompletionCallback callback2; | 342 TestCompletionCallback callback2; |
| 294 rv = service.ReconsiderProxyAfterError(url, &info, &callback2, NULL); | 343 rv = service.ReconsiderProxyAfterError(url, &info, &callback2, NULL); |
| 295 EXPECT_EQ(OK, rv); | 344 EXPECT_EQ(OK, rv); |
| 296 EXPECT_TRUE(info.is_direct()); | 345 EXPECT_TRUE(info.is_direct()); |
| 297 } | 346 } |
| 298 | 347 |
| 299 TEST(ProxyServiceTest, ProxyResolverFails) { | 348 TEST(ProxyServiceTest, ProxyResolverFails) { |
| 300 // Test what happens when the ProxyResolver fails (this could represent | 349 // Test what happens when the ProxyResolver fails (this could represent |
| 301 // a failure to download the PAC script in the case of ProxyResolvers which | 350 // a failure to download the PAC script in the case of ProxyResolvers which |
| 302 // do the fetch internally.) | 351 // do the fetch internally.) |
| 352 // TODO(eroman): change this comment. |
| 303 | 353 |
| 304 MockProxyConfigService* config_service = | 354 MockProxyConfigService* config_service = |
| 305 new MockProxyConfigService("http://foopy/proxy.pac"); | 355 new MockProxyConfigService("http://foopy/proxy.pac"); |
| 306 | 356 |
| 307 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; | 357 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; |
| 308 | 358 |
| 309 ProxyService service(config_service, resolver); | 359 ProxyService service(config_service, resolver); |
| 310 | 360 |
| 311 // Start first resolve request. | 361 // Start first resolve request. |
| 312 GURL url("http://www.google.com/"); | 362 GURL url("http://www.google.com/"); |
| 313 ProxyInfo info; | 363 ProxyInfo info; |
| 314 TestCompletionCallback callback1; | 364 TestCompletionCallback callback1; |
| 315 int rv = service.ResolveProxy(url, &info, &callback1, NULL); | 365 int rv = service.ResolveProxy(url, &info, &callback1, NULL); |
| 316 EXPECT_EQ(ERR_IO_PENDING, rv); | 366 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 317 | 367 |
| 318 EXPECT_EQ(GURL("http://foopy/proxy.pac"), resolver->pac_url()); | 368 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 369 resolver->pending_set_pac_script_request()->pac_url()); |
| 370 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 371 |
| 319 ASSERT_EQ(1u, resolver->pending_requests().size()); | 372 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 320 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); | 373 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); |
| 321 | 374 |
| 322 // Fail the first resolve request in MockAsyncProxyResolver. | 375 // Fail the first resolve request in MockAsyncProxyResolver. |
| 323 resolver->pending_requests()[0]->CompleteNow(ERR_FAILED); | 376 resolver->pending_requests()[0]->CompleteNow(ERR_FAILED); |
| 324 | 377 |
| 325 EXPECT_EQ(ERR_FAILED, callback1.WaitForResult()); | 378 EXPECT_EQ(ERR_FAILED, callback1.WaitForResult()); |
| 326 | 379 |
| 327 // The second resolve request will automatically select direct connect, | 380 // The second resolve request will automatically select direct connect, |
| 328 // because it has cached the configuration as being bad. | 381 // because it has cached the configuration as being bad. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 ProxyService service(config_service, resolver); | 415 ProxyService service(config_service, resolver); |
| 363 | 416 |
| 364 GURL url("http://www.google.com/"); | 417 GURL url("http://www.google.com/"); |
| 365 | 418 |
| 366 // Get the proxy information. | 419 // Get the proxy information. |
| 367 ProxyInfo info; | 420 ProxyInfo info; |
| 368 TestCompletionCallback callback1; | 421 TestCompletionCallback callback1; |
| 369 int rv = service.ResolveProxy(url, &info, &callback1, NULL); | 422 int rv = service.ResolveProxy(url, &info, &callback1, NULL); |
| 370 EXPECT_EQ(ERR_IO_PENDING, rv); | 423 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 371 | 424 |
| 372 EXPECT_EQ(GURL("http://foopy/proxy.pac"), resolver->pac_url()); | 425 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 426 resolver->pending_set_pac_script_request()->pac_url()); |
| 427 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 428 |
| 373 ASSERT_EQ(1u, resolver->pending_requests().size()); | 429 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 374 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); | 430 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); |
| 375 | 431 |
| 376 // Set the result in proxy resolver. | 432 // Set the result in proxy resolver. |
| 377 resolver->pending_requests()[0]->results()->UseNamedProxy( | 433 resolver->pending_requests()[0]->results()->UseNamedProxy( |
| 378 "foopy1:8080;foopy2:9090"); | 434 "foopy1:8080;foopy2:9090"); |
| 379 resolver->pending_requests()[0]->CompleteNow(OK); | 435 resolver->pending_requests()[0]->CompleteNow(OK); |
| 380 | 436 |
| 381 // The first item is valid. | 437 // The first item is valid. |
| 382 EXPECT_EQ(OK, callback1.WaitForResult()); | 438 EXPECT_EQ(OK, callback1.WaitForResult()); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 ProxyService service(config_service, resolver); | 495 ProxyService service(config_service, resolver); |
| 440 | 496 |
| 441 GURL url("http://www.google.com/"); | 497 GURL url("http://www.google.com/"); |
| 442 | 498 |
| 443 // Get the proxy information. | 499 // Get the proxy information. |
| 444 ProxyInfo info; | 500 ProxyInfo info; |
| 445 TestCompletionCallback callback1; | 501 TestCompletionCallback callback1; |
| 446 int rv = service.ResolveProxy(url, &info, &callback1, NULL); | 502 int rv = service.ResolveProxy(url, &info, &callback1, NULL); |
| 447 EXPECT_EQ(ERR_IO_PENDING, rv); | 503 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 448 | 504 |
| 449 EXPECT_EQ(GURL("http://foopy/proxy.pac"), resolver->pac_url()); | 505 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 506 resolver->pending_set_pac_script_request()->pac_url()); |
| 507 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 508 |
| 450 ASSERT_EQ(1u, resolver->pending_requests().size()); | 509 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 451 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); | 510 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); |
| 452 | 511 |
| 453 // Set the result in proxy resolver. | 512 // Set the result in proxy resolver. |
| 454 resolver->pending_requests()[0]->results()->UseNamedProxy( | 513 resolver->pending_requests()[0]->results()->UseNamedProxy( |
| 455 "foopy1:8080;foopy2:9090"); | 514 "foopy1:8080;foopy2:9090"); |
| 456 resolver->pending_requests()[0]->CompleteNow(OK); | 515 resolver->pending_requests()[0]->CompleteNow(OK); |
| 457 | 516 |
| 458 // The first item is valid. | 517 // The first item is valid. |
| 459 EXPECT_EQ(OK, callback1.WaitForResult()); | 518 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 460 EXPECT_FALSE(info.is_direct()); | 519 EXPECT_FALSE(info.is_direct()); |
| 461 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); | 520 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); |
| 462 | 521 |
| 463 // Fake an error on the proxy, and also a new configuration on the proxy. | 522 // Fake an error on the proxy, and also a new configuration on the proxy. |
| 464 config_service->config = ProxyConfig(); | 523 config_service->config = ProxyConfig(); |
| 465 config_service->config.pac_url = GURL("http://foopy-new/proxy.pac"); | 524 config_service->config.pac_url = GURL("http://foopy-new/proxy.pac"); |
| 466 | 525 |
| 467 TestCompletionCallback callback2; | 526 TestCompletionCallback callback2; |
| 468 rv = service.ReconsiderProxyAfterError(url, &info, &callback2, NULL); | 527 rv = service.ReconsiderProxyAfterError(url, &info, &callback2, NULL); |
| 469 EXPECT_EQ(ERR_IO_PENDING, rv); | 528 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 470 | 529 |
| 471 EXPECT_EQ(GURL("http://foopy-new/proxy.pac"), resolver->pac_url()); | 530 EXPECT_EQ(GURL("http://foopy-new/proxy.pac"), |
| 531 resolver->pending_set_pac_script_request()->pac_url()); |
| 532 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 533 |
| 472 ASSERT_EQ(1u, resolver->pending_requests().size()); | 534 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 473 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); | 535 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); |
| 474 | 536 |
| 475 resolver->pending_requests()[0]->results()->UseNamedProxy( | 537 resolver->pending_requests()[0]->results()->UseNamedProxy( |
| 476 "foopy1:8080;foopy2:9090"); | 538 "foopy1:8080;foopy2:9090"); |
| 477 resolver->pending_requests()[0]->CompleteNow(OK); | 539 resolver->pending_requests()[0]->CompleteNow(OK); |
| 478 | 540 |
| 479 // The first proxy is still there since the configuration changed. | 541 // The first proxy is still there since the configuration changed. |
| 480 EXPECT_EQ(OK, callback2.WaitForResult()); | 542 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 481 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); | 543 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); |
| 482 | 544 |
| 483 // We fake another error. It should now ignore the first one. | 545 // We fake another error. It should now ignore the first one. |
| 484 TestCompletionCallback callback3; | 546 TestCompletionCallback callback3; |
| 485 rv = service.ReconsiderProxyAfterError(url, &info, &callback3, NULL); | 547 rv = service.ReconsiderProxyAfterError(url, &info, &callback3, NULL); |
| 486 EXPECT_EQ(OK, rv); | 548 EXPECT_EQ(OK, rv); |
| 487 EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI()); | 549 EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI()); |
| 488 | 550 |
| 489 // We simulate a new configuration. | 551 // We simulate a new configuration. |
| 490 config_service->config = ProxyConfig(); | 552 config_service->config = ProxyConfig(); |
| 491 config_service->config.pac_url = GURL("http://foopy-new2/proxy.pac"); | 553 config_service->config.pac_url = GURL("http://foopy-new2/proxy.pac"); |
| 492 | 554 |
| 493 // We fake another error. It should go back to the first proxy. | 555 // We fake another error. It should go back to the first proxy. |
| 494 TestCompletionCallback callback4; | 556 TestCompletionCallback callback4; |
| 495 rv = service.ReconsiderProxyAfterError(url, &info, &callback4, NULL); | 557 rv = service.ReconsiderProxyAfterError(url, &info, &callback4, NULL); |
| 496 EXPECT_EQ(ERR_IO_PENDING, rv); | 558 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 497 | 559 |
| 498 EXPECT_EQ(GURL("http://foopy-new2/proxy.pac"), resolver->pac_url()); | 560 EXPECT_EQ(GURL("http://foopy-new2/proxy.pac"), |
| 561 resolver->pending_set_pac_script_request()->pac_url()); |
| 562 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 563 |
| 499 ASSERT_EQ(1u, resolver->pending_requests().size()); | 564 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 500 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); | 565 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); |
| 501 | 566 |
| 502 resolver->pending_requests()[0]->results()->UseNamedProxy( | 567 resolver->pending_requests()[0]->results()->UseNamedProxy( |
| 503 "foopy1:8080;foopy2:9090"); | 568 "foopy1:8080;foopy2:9090"); |
| 504 resolver->pending_requests()[0]->CompleteNow(OK); | 569 resolver->pending_requests()[0]->CompleteNow(OK); |
| 505 | 570 |
| 506 EXPECT_EQ(OK, callback4.WaitForResult()); | 571 EXPECT_EQ(OK, callback4.WaitForResult()); |
| 507 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); | 572 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); |
| 508 } | 573 } |
| 509 | 574 |
| 510 TEST(ProxyServiceTest, ProxyFallback_BadConfig) { | 575 TEST(ProxyServiceTest, ProxyFallback_BadConfig) { |
| 511 // Test proxy failover when the configuration is bad. | 576 // Test proxy failover when the configuration is bad. |
| 512 | 577 |
| 513 MockProxyConfigService* config_service = | 578 MockProxyConfigService* config_service = |
| 514 new MockProxyConfigService("http://foopy/proxy.pac"); | 579 new MockProxyConfigService("http://foopy/proxy.pac"); |
| 515 | 580 |
| 516 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; | 581 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; |
| 517 | 582 |
| 518 ProxyService service(config_service, resolver); | 583 ProxyService service(config_service, resolver); |
| 519 | 584 |
| 520 GURL url("http://www.google.com/"); | 585 GURL url("http://www.google.com/"); |
| 521 | 586 |
| 522 // Get the proxy information. | 587 // Get the proxy information. |
| 523 ProxyInfo info; | 588 ProxyInfo info; |
| 524 TestCompletionCallback callback1; | 589 TestCompletionCallback callback1; |
| 525 int rv = service.ResolveProxy(url, &info, &callback1, NULL); | 590 int rv = service.ResolveProxy(url, &info, &callback1, NULL); |
| 526 EXPECT_EQ(ERR_IO_PENDING, rv); | 591 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 527 | 592 |
| 528 EXPECT_EQ(GURL("http://foopy/proxy.pac"), resolver->pac_url()); | 593 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 594 resolver->pending_set_pac_script_request()->pac_url()); |
| 595 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 529 ASSERT_EQ(1u, resolver->pending_requests().size()); | 596 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 530 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); | 597 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); |
| 531 | 598 |
| 532 resolver->pending_requests()[0]->results()->UseNamedProxy( | 599 resolver->pending_requests()[0]->results()->UseNamedProxy( |
| 533 "foopy1:8080;foopy2:9090"); | 600 "foopy1:8080;foopy2:9090"); |
| 534 resolver->pending_requests()[0]->CompleteNow(OK); | 601 resolver->pending_requests()[0]->CompleteNow(OK); |
| 535 | 602 |
| 536 // The first item is valid. | 603 // The first item is valid. |
| 537 EXPECT_EQ(OK, callback1.WaitForResult()); | 604 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 538 EXPECT_FALSE(info.is_direct()); | 605 EXPECT_FALSE(info.is_direct()); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 ProxyInfo info; | 762 ProxyInfo info; |
| 696 ProxyConfig config; | 763 ProxyConfig config; |
| 697 config.proxy_rules.ParseFromString("foopy1:8080;foopy2:9090"); | 764 config.proxy_rules.ParseFromString("foopy1:8080;foopy2:9090"); |
| 698 config.auto_detect = false; | 765 config.auto_detect = false; |
| 699 config.proxy_bypass_local_names = false; | 766 config.proxy_bypass_local_names = false; |
| 700 | 767 |
| 701 config.proxy_bypass.clear(); | 768 config.proxy_bypass.clear(); |
| 702 config.proxy_bypass.push_back("*.example.com:99"); | 769 config.proxy_bypass.push_back("*.example.com:99"); |
| 703 { | 770 { |
| 704 ProxyService service(new MockProxyConfigService(config), | 771 ProxyService service(new MockProxyConfigService(config), |
| 705 new MockAsyncProxyResolver); | 772 new MockAsyncProxyResolver); |
| 706 { | 773 { |
| 707 GURL test_url("http://www.example.com:99"); | 774 GURL test_url("http://www.example.com:99"); |
| 708 TestCompletionCallback callback; | 775 TestCompletionCallback callback; |
| 709 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 776 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 710 EXPECT_EQ(OK, rv); | 777 EXPECT_EQ(OK, rv); |
| 711 EXPECT_TRUE(info.is_direct()); | 778 EXPECT_TRUE(info.is_direct()); |
| 712 } | 779 } |
| 713 { | 780 { |
| 714 GURL test_url("http://www.example.com:100"); | 781 GURL test_url("http://www.example.com:100"); |
| 715 TestCompletionCallback callback; | 782 TestCompletionCallback callback; |
| 716 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 783 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 717 EXPECT_EQ(OK, rv); | 784 EXPECT_EQ(OK, rv); |
| 718 EXPECT_FALSE(info.is_direct()); | 785 EXPECT_FALSE(info.is_direct()); |
| 719 } | 786 } |
| 720 { | 787 { |
| 721 GURL test_url("http://www.example.com"); | 788 GURL test_url("http://www.example.com"); |
| 722 TestCompletionCallback callback; | 789 TestCompletionCallback callback; |
| 723 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 790 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 724 EXPECT_EQ(OK, rv); | 791 EXPECT_EQ(OK, rv); |
| 725 EXPECT_FALSE(info.is_direct()); | 792 EXPECT_FALSE(info.is_direct()); |
| 726 } | 793 } |
| 727 } | 794 } |
| 728 | 795 |
| 729 config.proxy_bypass.clear(); | 796 config.proxy_bypass.clear(); |
| 730 config.proxy_bypass.push_back("*.example.com:80"); | 797 config.proxy_bypass.push_back("*.example.com:80"); |
| 731 { | 798 { |
| 732 ProxyService service(new MockProxyConfigService(config), | 799 ProxyService service(new MockProxyConfigService(config), |
| 733 new MockAsyncProxyResolver); | 800 new MockAsyncProxyResolver); |
| 734 GURL test_url("http://www.example.com"); | 801 GURL test_url("http://www.example.com"); |
| 735 TestCompletionCallback callback; | 802 TestCompletionCallback callback; |
| 736 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 803 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 737 EXPECT_EQ(OK, rv); | 804 EXPECT_EQ(OK, rv); |
| 738 EXPECT_TRUE(info.is_direct()); | 805 EXPECT_TRUE(info.is_direct()); |
| 739 } | 806 } |
| 740 | 807 |
| 741 config.proxy_bypass.clear(); | 808 config.proxy_bypass.clear(); |
| 742 config.proxy_bypass.push_back("*.example.com"); | 809 config.proxy_bypass.push_back("*.example.com"); |
| 743 { | 810 { |
| 744 ProxyService service(new MockProxyConfigService(config), | 811 ProxyService service(new MockProxyConfigService(config), |
| 745 new MockAsyncProxyResolver); | 812 new MockAsyncProxyResolver); |
| 746 GURL test_url("http://www.example.com:99"); | 813 GURL test_url("http://www.example.com:99"); |
| 747 TestCompletionCallback callback; | 814 TestCompletionCallback callback; |
| 748 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 815 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 749 EXPECT_EQ(OK, rv); | 816 EXPECT_EQ(OK, rv); |
| 750 EXPECT_TRUE(info.is_direct()); | 817 EXPECT_TRUE(info.is_direct()); |
| 751 } | 818 } |
| 752 | 819 |
| 753 // IPv6 with port. | 820 // IPv6 with port. |
| 754 config.proxy_bypass.clear(); | 821 config.proxy_bypass.clear(); |
| 755 config.proxy_bypass.push_back("[3ffe:2a00:100:7031::1]:99"); | 822 config.proxy_bypass.push_back("[3ffe:2a00:100:7031::1]:99"); |
| 756 { | 823 { |
| 757 ProxyService service(new MockProxyConfigService(config), | 824 ProxyService service(new MockProxyConfigService(config), |
| 758 new MockAsyncProxyResolver); | 825 new MockAsyncProxyResolver); |
| 759 { | 826 { |
| 760 GURL test_url("http://[3ffe:2a00:100:7031::1]:99/"); | 827 GURL test_url("http://[3ffe:2a00:100:7031::1]:99/"); |
| 761 TestCompletionCallback callback; | 828 TestCompletionCallback callback; |
| 762 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 829 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 763 EXPECT_EQ(OK, rv); | 830 EXPECT_EQ(OK, rv); |
| 764 EXPECT_TRUE(info.is_direct()); | 831 EXPECT_TRUE(info.is_direct()); |
| 765 } | 832 } |
| 766 { | 833 { |
| 767 GURL test_url("http://[3ffe:2a00:100:7031::1]/"); | 834 GURL test_url("http://[3ffe:2a00:100:7031::1]/"); |
| 768 TestCompletionCallback callback; | 835 TestCompletionCallback callback; |
| 769 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 836 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 770 EXPECT_EQ(OK, rv); | 837 EXPECT_EQ(OK, rv); |
| 771 EXPECT_FALSE(info.is_direct()); | 838 EXPECT_FALSE(info.is_direct()); |
| 772 } | 839 } |
| 773 } | 840 } |
| 774 | 841 |
| 775 // IPv6 without port. The bypass entry ought to work without the | 842 // IPv6 without port. The bypass entry ought to work without the |
| 776 // brackets, but the bypass matching logic in ProxyService is | 843 // brackets, but the bypass matching logic in ProxyService is |
| 777 // currently limited. | 844 // currently limited. |
| 778 config.proxy_bypass.clear(); | 845 config.proxy_bypass.clear(); |
| 779 config.proxy_bypass.push_back("[3ffe:2a00:100:7031::1]"); | 846 config.proxy_bypass.push_back("[3ffe:2a00:100:7031::1]"); |
| 780 { | 847 { |
| 781 ProxyService service(new MockProxyConfigService(config), | 848 ProxyService service(new MockProxyConfigService(config), |
| 782 new MockAsyncProxyResolver); | 849 new MockAsyncProxyResolver); |
| 783 { | 850 { |
| 784 GURL test_url("http://[3ffe:2a00:100:7031::1]:99/"); | 851 GURL test_url("http://[3ffe:2a00:100:7031::1]:99/"); |
| 785 TestCompletionCallback callback; | 852 TestCompletionCallback callback; |
| 786 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 853 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 787 EXPECT_EQ(OK, rv); | 854 EXPECT_EQ(OK, rv); |
| 788 EXPECT_TRUE(info.is_direct()); | 855 EXPECT_TRUE(info.is_direct()); |
| 789 } | 856 } |
| 790 { | 857 { |
| 791 GURL test_url("http://[3ffe:2a00:100:7031::1]/"); | 858 GURL test_url("http://[3ffe:2a00:100:7031::1]/"); |
| 792 TestCompletionCallback callback; | 859 TestCompletionCallback callback; |
| 793 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 860 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 794 EXPECT_EQ(OK, rv); | 861 EXPECT_EQ(OK, rv); |
| 795 EXPECT_TRUE(info.is_direct()); | 862 EXPECT_TRUE(info.is_direct()); |
| 796 } | 863 } |
| 797 } | 864 } |
| 798 } | 865 } |
| 799 | 866 |
| 800 TEST(ProxyServiceTest, PerProtocolProxyTests) { | 867 TEST(ProxyServiceTest, PerProtocolProxyTests) { |
| 801 ProxyConfig config; | 868 ProxyConfig config; |
| 802 config.proxy_rules.ParseFromString("http=foopy1:8080;https=foopy2:8080"); | 869 config.proxy_rules.ParseFromString("http=foopy1:8080;https=foopy2:8080"); |
| 803 config.auto_detect = false; | 870 config.auto_detect = false; |
| 804 { | 871 { |
| 805 ProxyService service(new MockProxyConfigService(config), | 872 ProxyService service(new MockProxyConfigService(config), |
| 806 new MockAsyncProxyResolver); | 873 new MockAsyncProxyResolver); |
| 807 GURL test_url("http://www.msn.com"); | 874 GURL test_url("http://www.msn.com"); |
| 808 ProxyInfo info; | 875 ProxyInfo info; |
| 809 TestCompletionCallback callback; | 876 TestCompletionCallback callback; |
| 810 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 877 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 811 EXPECT_EQ(OK, rv); | 878 EXPECT_EQ(OK, rv); |
| 812 EXPECT_FALSE(info.is_direct()); | 879 EXPECT_FALSE(info.is_direct()); |
| 813 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); | 880 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); |
| 814 } | 881 } |
| 815 { | 882 { |
| 816 ProxyService service(new MockProxyConfigService(config), | 883 ProxyService service(new MockProxyConfigService(config), |
| 817 new MockAsyncProxyResolver); | 884 new MockAsyncProxyResolver); |
| 818 GURL test_url("ftp://ftp.google.com"); | 885 GURL test_url("ftp://ftp.google.com"); |
| 819 ProxyInfo info; | 886 ProxyInfo info; |
| 820 TestCompletionCallback callback; | 887 TestCompletionCallback callback; |
| 821 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 888 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 822 EXPECT_EQ(OK, rv); | 889 EXPECT_EQ(OK, rv); |
| 823 EXPECT_TRUE(info.is_direct()); | 890 EXPECT_TRUE(info.is_direct()); |
| 824 EXPECT_EQ("direct://", info.proxy_server().ToURI()); | 891 EXPECT_EQ("direct://", info.proxy_server().ToURI()); |
| 825 } | 892 } |
| 826 { | 893 { |
| 827 ProxyService service(new MockProxyConfigService(config), | 894 ProxyService service(new MockProxyConfigService(config), |
| 828 new MockAsyncProxyResolver); | 895 new MockAsyncProxyResolver); |
| 829 GURL test_url("https://webbranch.techcu.com"); | 896 GURL test_url("https://webbranch.techcu.com"); |
| 830 ProxyInfo info; | 897 ProxyInfo info; |
| 831 TestCompletionCallback callback; | 898 TestCompletionCallback callback; |
| 832 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 899 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 833 EXPECT_EQ(OK, rv); | 900 EXPECT_EQ(OK, rv); |
| 834 EXPECT_FALSE(info.is_direct()); | 901 EXPECT_FALSE(info.is_direct()); |
| 835 EXPECT_EQ("foopy2:8080", info.proxy_server().ToURI()); | 902 EXPECT_EQ("foopy2:8080", info.proxy_server().ToURI()); |
| 836 } | 903 } |
| 837 { | 904 { |
| 838 config.proxy_rules.ParseFromString("foopy1:8080"); | 905 config.proxy_rules.ParseFromString("foopy1:8080"); |
| 839 ProxyService service(new MockProxyConfigService(config), | 906 ProxyService service(new MockProxyConfigService(config), |
| 840 new MockAsyncProxyResolver); | 907 new MockAsyncProxyResolver); |
| 841 GURL test_url("http://www.microsoft.com"); | 908 GURL test_url("http://www.microsoft.com"); |
| 842 ProxyInfo info; | 909 ProxyInfo info; |
| 843 TestCompletionCallback callback; | 910 TestCompletionCallback callback; |
| 844 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 911 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 845 EXPECT_EQ(OK, rv); | 912 EXPECT_EQ(OK, rv); |
| 846 EXPECT_FALSE(info.is_direct()); | 913 EXPECT_FALSE(info.is_direct()); |
| 847 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); | 914 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); |
| 848 } | 915 } |
| 849 } | 916 } |
| 850 | 917 |
| 851 // If only HTTP and a SOCKS proxy are specified, check if ftp/https queries | 918 // If only HTTP and a SOCKS proxy are specified, check if ftp/https queries |
| 852 // fall back to the SOCKS proxy. | 919 // fall back to the SOCKS proxy. |
| 853 TEST(ProxyServiceTest, DefaultProxyFallbackToSOCKS) { | 920 TEST(ProxyServiceTest, DefaultProxyFallbackToSOCKS) { |
| 854 ProxyConfig config; | 921 ProxyConfig config; |
| 855 config.proxy_rules.ParseFromString("http=foopy1:8080;socks=foopy2:1080"); | 922 config.proxy_rules.ParseFromString("http=foopy1:8080;socks=foopy2:1080"); |
| 856 config.auto_detect = false; | 923 config.auto_detect = false; |
| 857 EXPECT_EQ(ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | 924 EXPECT_EQ(ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
| 858 config.proxy_rules.type); | 925 config.proxy_rules.type); |
| 859 | 926 |
| 860 { | 927 { |
| 861 ProxyService service(new MockProxyConfigService(config), | 928 ProxyService service(new MockProxyConfigService(config), |
| 862 new MockAsyncProxyResolver); | 929 new MockAsyncProxyResolver); |
| 863 GURL test_url("http://www.msn.com"); | 930 GURL test_url("http://www.msn.com"); |
| 864 ProxyInfo info; | 931 ProxyInfo info; |
| 865 TestCompletionCallback callback; | 932 TestCompletionCallback callback; |
| 866 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 933 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 867 EXPECT_EQ(OK, rv); | 934 EXPECT_EQ(OK, rv); |
| 868 EXPECT_FALSE(info.is_direct()); | 935 EXPECT_FALSE(info.is_direct()); |
| 869 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); | 936 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); |
| 870 } | 937 } |
| 871 { | 938 { |
| 872 ProxyService service(new MockProxyConfigService(config), | 939 ProxyService service(new MockProxyConfigService(config), |
| 873 new MockAsyncProxyResolver); | 940 new MockAsyncProxyResolver); |
| 874 GURL test_url("ftp://ftp.google.com"); | 941 GURL test_url("ftp://ftp.google.com"); |
| 875 ProxyInfo info; | 942 ProxyInfo info; |
| 876 TestCompletionCallback callback; | 943 TestCompletionCallback callback; |
| 877 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 944 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 878 EXPECT_EQ(OK, rv); | 945 EXPECT_EQ(OK, rv); |
| 879 EXPECT_FALSE(info.is_direct()); | 946 EXPECT_FALSE(info.is_direct()); |
| 880 EXPECT_EQ("socks4://foopy2:1080", info.proxy_server().ToURI()); | 947 EXPECT_EQ("socks4://foopy2:1080", info.proxy_server().ToURI()); |
| 881 } | 948 } |
| 882 { | 949 { |
| 883 ProxyService service(new MockProxyConfigService(config), | 950 ProxyService service(new MockProxyConfigService(config), |
| 884 new MockAsyncProxyResolver); | 951 new MockAsyncProxyResolver); |
| 885 GURL test_url("https://webbranch.techcu.com"); | 952 GURL test_url("https://webbranch.techcu.com"); |
| 886 ProxyInfo info; | 953 ProxyInfo info; |
| 887 TestCompletionCallback callback; | 954 TestCompletionCallback callback; |
| 888 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 955 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 889 EXPECT_EQ(OK, rv); | 956 EXPECT_EQ(OK, rv); |
| 890 EXPECT_FALSE(info.is_direct()); | 957 EXPECT_FALSE(info.is_direct()); |
| 891 EXPECT_EQ("socks4://foopy2:1080", info.proxy_server().ToURI()); | 958 EXPECT_EQ("socks4://foopy2:1080", info.proxy_server().ToURI()); |
| 892 } | 959 } |
| 893 { | 960 { |
| 894 ProxyService service(new MockProxyConfigService(config), | 961 ProxyService service(new MockProxyConfigService(config), |
| 895 new MockAsyncProxyResolver); | 962 new MockAsyncProxyResolver); |
| 896 GURL test_url("unknown://www.microsoft.com"); | 963 GURL test_url("unknown://www.microsoft.com"); |
| 897 ProxyInfo info; | 964 ProxyInfo info; |
| 898 TestCompletionCallback callback; | 965 TestCompletionCallback callback; |
| 899 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); | 966 int rv = service.ResolveProxy(test_url, &info, &callback, NULL); |
| 900 EXPECT_EQ(OK, rv); | 967 EXPECT_EQ(OK, rv); |
| 901 EXPECT_FALSE(info.is_direct()); | 968 EXPECT_FALSE(info.is_direct()); |
| 902 EXPECT_EQ("socks4://foopy2:1080", info.proxy_server().ToURI()); | 969 EXPECT_EQ("socks4://foopy2:1080", info.proxy_server().ToURI()); |
| 903 } | 970 } |
| 904 } | 971 } |
| 905 | 972 |
| 906 // Test cancellation of an in-progress request. | 973 // Test cancellation of an in-progress request. |
| 907 TEST(ProxyServiceTest, CancelInProgressRequest) { | 974 TEST(ProxyServiceTest, CancelInProgressRequest) { |
| 908 MockProxyConfigService* config_service = | 975 MockProxyConfigService* config_service = |
| 909 new MockProxyConfigService("http://foopy/proxy.pac"); | 976 new MockProxyConfigService("http://foopy/proxy.pac"); |
| 910 | 977 |
| 911 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; | 978 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; |
| 912 | 979 |
| 913 ProxyService service(config_service, resolver); | 980 ProxyService service(config_service, resolver); |
| 914 | 981 |
| 915 // Start 3 requests. | 982 // Start 3 requests. |
| 916 | 983 |
| 917 ProxyInfo info1; | 984 ProxyInfo info1; |
| 918 TestCompletionCallback callback1; | 985 TestCompletionCallback callback1; |
| 919 int rv = service.ResolveProxy( | 986 int rv = service.ResolveProxy( |
| 920 GURL("http://request1"), &info1, &callback1, NULL); | 987 GURL("http://request1"), &info1, &callback1, NULL); |
| 921 EXPECT_EQ(ERR_IO_PENDING, rv); | 988 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 989 |
| 990 // Nothing has been sent to the proxy resolver yet, since the proxy |
| 991 // resolver has not been configured yet. |
| 992 ASSERT_EQ(0u, resolver->pending_requests().size()); |
| 993 |
| 994 // Successfully initialize the PAC script. |
| 995 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 996 resolver->pending_set_pac_script_request()->pac_url()); |
| 997 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 998 |
| 922 ASSERT_EQ(1u, resolver->pending_requests().size()); | 999 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 923 EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url()); | 1000 EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url()); |
| 924 | 1001 |
| 925 ProxyInfo info2; | 1002 ProxyInfo info2; |
| 926 TestCompletionCallback callback2; | 1003 TestCompletionCallback callback2; |
| 927 ProxyService::PacRequest* request2; | 1004 ProxyService::PacRequest* request2; |
| 928 rv = service.ResolveProxy( | 1005 rv = service.ResolveProxy( |
| 929 GURL("http://request2"), &info2, &callback2, &request2); | 1006 GURL("http://request2"), &info2, &callback2, &request2); |
| 930 EXPECT_EQ(ERR_IO_PENDING, rv); | 1007 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 931 ASSERT_EQ(2u, resolver->pending_requests().size()); | 1008 ASSERT_EQ(2u, resolver->pending_requests().size()); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1004 EXPECT_EQ(ERR_IO_PENDING, rv); | 1081 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1005 | 1082 |
| 1006 // Nothing has been sent to the resolver yet. | 1083 // Nothing has been sent to the resolver yet. |
| 1007 EXPECT_TRUE(resolver->pending_requests().empty()); | 1084 EXPECT_TRUE(resolver->pending_requests().empty()); |
| 1008 | 1085 |
| 1009 // At this point the ProxyService should be waiting for the | 1086 // At this point the ProxyService should be waiting for the |
| 1010 // ProxyScriptFetcher to invoke its completion callback, notifying it of | 1087 // ProxyScriptFetcher to invoke its completion callback, notifying it of |
| 1011 // PAC script download completion. | 1088 // PAC script download completion. |
| 1012 fetcher->NotifyFetchCompletion(OK, "pac-v1"); | 1089 fetcher->NotifyFetchCompletion(OK, "pac-v1"); |
| 1013 | 1090 |
| 1014 // Now that the PAC script is downloaded, everything should have been sent | 1091 // Now that the PAC script is downloaded, it will have been sent to the proxy |
| 1015 // over to the proxy resolver. | 1092 // resolver. |
| 1016 EXPECT_EQ("pac-v1", resolver->pac_bytes()); | 1093 EXPECT_EQ("pac-v1", resolver->pending_set_pac_script_request()->pac_bytes()); |
| 1094 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 1095 |
| 1017 ASSERT_EQ(3u, resolver->pending_requests().size()); | 1096 ASSERT_EQ(3u, resolver->pending_requests().size()); |
| 1018 EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url()); | 1097 EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url()); |
| 1019 EXPECT_EQ(GURL("http://request2"), resolver->pending_requests()[1]->url()); | 1098 EXPECT_EQ(GURL("http://request2"), resolver->pending_requests()[1]->url()); |
| 1020 EXPECT_EQ(GURL("http://request3"), resolver->pending_requests()[2]->url()); | 1099 EXPECT_EQ(GURL("http://request3"), resolver->pending_requests()[2]->url()); |
| 1021 | 1100 |
| 1022 // Complete all the requests (in some order). | 1101 // Complete all the requests (in some order). |
| 1023 // Note that as we complete requests, they shift up in |pending_requests()|. | 1102 // Note that as we complete requests, they shift up in |pending_requests()|. |
| 1024 | 1103 |
| 1025 resolver->pending_requests()[2]->results()->UseNamedProxy("request3:80"); | 1104 resolver->pending_requests()[2]->results()->UseNamedProxy("request3:80"); |
| 1026 resolver->pending_requests()[2]->CompleteNow(OK); | 1105 resolver->pending_requests()[2]->CompleteNow(OK); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1085 | 1164 |
| 1086 // Cancel the first 2 requests. | 1165 // Cancel the first 2 requests. |
| 1087 service.CancelPacRequest(request1); | 1166 service.CancelPacRequest(request1); |
| 1088 service.CancelPacRequest(request2); | 1167 service.CancelPacRequest(request2); |
| 1089 | 1168 |
| 1090 // At this point the ProxyService should be waiting for the | 1169 // At this point the ProxyService should be waiting for the |
| 1091 // ProxyScriptFetcher to invoke its completion callback, notifying it of | 1170 // ProxyScriptFetcher to invoke its completion callback, notifying it of |
| 1092 // PAC script download completion. | 1171 // PAC script download completion. |
| 1093 fetcher->NotifyFetchCompletion(OK, "pac-v1"); | 1172 fetcher->NotifyFetchCompletion(OK, "pac-v1"); |
| 1094 | 1173 |
| 1095 // Now that the PAC script is downloaded, everything should have been sent | 1174 // Now that the PAC script is downloaded, it will have been sent to the |
| 1096 // over to the proxy resolver. | 1175 // proxy resolver. |
| 1097 EXPECT_EQ("pac-v1", resolver->pac_bytes()); | 1176 EXPECT_EQ("pac-v1", resolver->pending_set_pac_script_request()->pac_bytes()); |
| 1177 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 1178 |
| 1098 ASSERT_EQ(1u, resolver->pending_requests().size()); | 1179 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 1099 EXPECT_EQ(GURL("http://request3"), resolver->pending_requests()[0]->url()); | 1180 EXPECT_EQ(GURL("http://request3"), resolver->pending_requests()[0]->url()); |
| 1100 | 1181 |
| 1101 // Complete all the requests. | 1182 // Complete all the requests. |
| 1102 resolver->pending_requests()[0]->results()->UseNamedProxy("request3:80"); | 1183 resolver->pending_requests()[0]->results()->UseNamedProxy("request3:80"); |
| 1103 resolver->pending_requests()[0]->CompleteNow(OK); | 1184 resolver->pending_requests()[0]->CompleteNow(OK); |
| 1104 | 1185 |
| 1105 EXPECT_EQ(OK, callback3.WaitForResult()); | 1186 EXPECT_EQ(OK, callback3.WaitForResult()); |
| 1106 EXPECT_EQ("request3:80", info3.proxy_server().ToURI()); | 1187 EXPECT_EQ("request3:80", info3.proxy_server().ToURI()); |
| 1107 | 1188 |
| 1108 EXPECT_TRUE(resolver->cancelled_requests().empty()); | 1189 EXPECT_TRUE(resolver->cancelled_requests().empty()); |
| 1109 | 1190 |
| 1110 EXPECT_FALSE(callback1.have_result()); // Cancelled. | 1191 EXPECT_FALSE(callback1.have_result()); // Cancelled. |
| 1111 EXPECT_FALSE(callback2.have_result()); // Cancelled. | 1192 EXPECT_FALSE(callback2.have_result()); // Cancelled. |
| 1112 } | 1193 } |
| 1113 | 1194 |
| 1195 // Test that if auto-detect fails, we fall-back to the custom pac. |
| 1196 TEST(ProxyServiceTest, FallbackFromAutodetectToCustomPac) { |
| 1197 ProxyConfig config; |
| 1198 config.auto_detect = true; |
| 1199 config.pac_url = GURL("http://foopy/proxy.pac"); |
| 1200 config.proxy_rules.ParseFromString("http=foopy:80"); // Won't be used. |
| 1201 |
| 1202 MockProxyConfigService* config_service = new MockProxyConfigService(config); |
| 1203 MockAsyncProxyResolverExpectsBytes* resolver = |
| 1204 new MockAsyncProxyResolverExpectsBytes; |
| 1205 ProxyService service(config_service, resolver); |
| 1206 |
| 1207 MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher; |
| 1208 service.SetProxyScriptFetcher(fetcher); |
| 1209 |
| 1210 // Start 2 requests. |
| 1211 |
| 1212 ProxyInfo info1; |
| 1213 TestCompletionCallback callback1; |
| 1214 int rv = service.ResolveProxy( |
| 1215 GURL("http://request1"), &info1, &callback1, NULL); |
| 1216 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1217 |
| 1218 ProxyInfo info2; |
| 1219 TestCompletionCallback callback2; |
| 1220 ProxyService::PacRequest* request2; |
| 1221 rv = service.ResolveProxy( |
| 1222 GURL("http://request2"), &info2, &callback2, &request2); |
| 1223 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1224 |
| 1225 // Check that nothing has been sent to the proxy resolver yet. |
| 1226 ASSERT_EQ(0u, resolver->pending_requests().size()); |
| 1227 |
| 1228 // It should be trying to auto-detect first -- FAIL the autodetect during |
| 1229 // the script download. |
| 1230 EXPECT_TRUE(fetcher->has_pending_request()); |
| 1231 EXPECT_EQ(GURL("http://wpad/wpad.dat"), fetcher->pending_request_url()); |
| 1232 fetcher->NotifyFetchCompletion(ERR_FAILED, ""); |
| 1233 |
| 1234 // Next it should be trying the custom PAC url. |
| 1235 EXPECT_TRUE(fetcher->has_pending_request()); |
| 1236 EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url()); |
| 1237 fetcher->NotifyFetchCompletion(OK, "custom-pac-script"); |
| 1238 |
| 1239 EXPECT_EQ("custom-pac-script", |
| 1240 resolver->pending_set_pac_script_request()->pac_bytes()); |
| 1241 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 1242 |
| 1243 // Now finally, the pending requests should have been sent to the resolver |
| 1244 // (which was initialized with custom PAC script). |
| 1245 |
| 1246 ASSERT_EQ(2u, resolver->pending_requests().size()); |
| 1247 EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url()); |
| 1248 EXPECT_EQ(GURL("http://request2"), resolver->pending_requests()[1]->url()); |
| 1249 |
| 1250 // Complete the pending requests. |
| 1251 resolver->pending_requests()[1]->results()->UseNamedProxy("request2:80"); |
| 1252 resolver->pending_requests()[1]->CompleteNow(OK); |
| 1253 resolver->pending_requests()[0]->results()->UseNamedProxy("request1:80"); |
| 1254 resolver->pending_requests()[0]->CompleteNow(OK); |
| 1255 |
| 1256 // Verify that requests ran as expected. |
| 1257 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 1258 EXPECT_EQ("request1:80", info1.proxy_server().ToURI()); |
| 1259 |
| 1260 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 1261 EXPECT_EQ("request2:80", info2.proxy_server().ToURI()); |
| 1262 } |
| 1263 |
| 1264 // This is the same test as FallbackFromAutodetectToCustomPac, except |
| 1265 // the auto-detect script fails parsing rather than downloading. |
| 1266 TEST(ProxyServiceTest, FallbackFromAutodetectToCustomPac2) { |
| 1267 ProxyConfig config; |
| 1268 config.auto_detect = true; |
| 1269 config.pac_url = GURL("http://foopy/proxy.pac"); |
| 1270 config.proxy_rules.ParseFromString("http=foopy:80"); // Won't be used. |
| 1271 |
| 1272 MockProxyConfigService* config_service = new MockProxyConfigService(config); |
| 1273 MockAsyncProxyResolverExpectsBytes* resolver = |
| 1274 new MockAsyncProxyResolverExpectsBytes; |
| 1275 ProxyService service(config_service, resolver); |
| 1276 |
| 1277 MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher; |
| 1278 service.SetProxyScriptFetcher(fetcher); |
| 1279 |
| 1280 // Start 2 requests. |
| 1281 |
| 1282 ProxyInfo info1; |
| 1283 TestCompletionCallback callback1; |
| 1284 int rv = service.ResolveProxy( |
| 1285 GURL("http://request1"), &info1, &callback1, NULL); |
| 1286 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1287 |
| 1288 ProxyInfo info2; |
| 1289 TestCompletionCallback callback2; |
| 1290 ProxyService::PacRequest* request2; |
| 1291 rv = service.ResolveProxy( |
| 1292 GURL("http://request2"), &info2, &callback2, &request2); |
| 1293 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1294 |
| 1295 // Check that nothing has been sent to the proxy resolver yet. |
| 1296 ASSERT_EQ(0u, resolver->pending_requests().size()); |
| 1297 |
| 1298 // It should be trying to auto-detect first -- succeed the download. |
| 1299 EXPECT_TRUE(fetcher->has_pending_request()); |
| 1300 EXPECT_EQ(GURL("http://wpad/wpad.dat"), fetcher->pending_request_url()); |
| 1301 fetcher->NotifyFetchCompletion(OK, "invalid-script-contents"); |
| 1302 |
| 1303 // Simulate a parse error. |
| 1304 EXPECT_EQ("invalid-script-contents", |
| 1305 resolver->pending_set_pac_script_request()->pac_bytes()); |
| 1306 resolver->pending_set_pac_script_request()->CompleteNow( |
| 1307 ERR_PAC_SCRIPT_FAILED); |
| 1308 |
| 1309 // Next it should be trying the custom PAC url. |
| 1310 EXPECT_TRUE(fetcher->has_pending_request()); |
| 1311 EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url()); |
| 1312 fetcher->NotifyFetchCompletion(OK, "custom-pac-script"); |
| 1313 |
| 1314 EXPECT_EQ("custom-pac-script", |
| 1315 resolver->pending_set_pac_script_request()->pac_bytes()); |
| 1316 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 1317 |
| 1318 // Now finally, the pending requests should have been sent to the resolver |
| 1319 // (which was initialized with custom PAC script). |
| 1320 |
| 1321 ASSERT_EQ(2u, resolver->pending_requests().size()); |
| 1322 EXPECT_EQ(GURL("http://request1"), resolver->pending_requests()[0]->url()); |
| 1323 EXPECT_EQ(GURL("http://request2"), resolver->pending_requests()[1]->url()); |
| 1324 |
| 1325 // Complete the pending requests. |
| 1326 resolver->pending_requests()[1]->results()->UseNamedProxy("request2:80"); |
| 1327 resolver->pending_requests()[1]->CompleteNow(OK); |
| 1328 resolver->pending_requests()[0]->results()->UseNamedProxy("request1:80"); |
| 1329 resolver->pending_requests()[0]->CompleteNow(OK); |
| 1330 |
| 1331 // Verify that requests ran as expected. |
| 1332 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 1333 EXPECT_EQ("request1:80", info1.proxy_server().ToURI()); |
| 1334 |
| 1335 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 1336 EXPECT_EQ("request2:80", info2.proxy_server().ToURI()); |
| 1337 } |
| 1338 |
| 1339 // Test that if all of auto-detect, a custom PAC script, and manual settings |
| 1340 // are given, then we will try them in that order. |
| 1341 TEST(ProxyServiceTest, FallbackFromAutodetectToCustomToManual) { |
| 1342 ProxyConfig config; |
| 1343 config.auto_detect = true; |
| 1344 config.pac_url = GURL("http://foopy/proxy.pac"); |
| 1345 config.proxy_rules.ParseFromString("http=foopy:80"); |
| 1346 |
| 1347 MockProxyConfigService* config_service = new MockProxyConfigService(config); |
| 1348 MockAsyncProxyResolverExpectsBytes* resolver = |
| 1349 new MockAsyncProxyResolverExpectsBytes; |
| 1350 ProxyService service(config_service, resolver); |
| 1351 |
| 1352 MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher; |
| 1353 service.SetProxyScriptFetcher(fetcher); |
| 1354 |
| 1355 // Start 2 requests. |
| 1356 |
| 1357 ProxyInfo info1; |
| 1358 TestCompletionCallback callback1; |
| 1359 int rv = service.ResolveProxy( |
| 1360 GURL("http://request1"), &info1, &callback1, NULL); |
| 1361 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1362 |
| 1363 ProxyInfo info2; |
| 1364 TestCompletionCallback callback2; |
| 1365 ProxyService::PacRequest* request2; |
| 1366 rv = service.ResolveProxy( |
| 1367 GURL("http://request2"), &info2, &callback2, &request2); |
| 1368 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1369 |
| 1370 // Check that nothing has been sent to the proxy resolver yet. |
| 1371 ASSERT_EQ(0u, resolver->pending_requests().size()); |
| 1372 |
| 1373 // It should be trying to auto-detect first -- fail the download. |
| 1374 EXPECT_TRUE(fetcher->has_pending_request()); |
| 1375 EXPECT_EQ(GURL("http://wpad/wpad.dat"), fetcher->pending_request_url()); |
| 1376 fetcher->NotifyFetchCompletion(ERR_FAILED, ""); |
| 1377 |
| 1378 // Next it should be trying the custom PAC url -- fail the download. |
| 1379 EXPECT_TRUE(fetcher->has_pending_request()); |
| 1380 EXPECT_EQ(GURL("http://foopy/proxy.pac"), fetcher->pending_request_url()); |
| 1381 fetcher->NotifyFetchCompletion(ERR_FAILED, ""); |
| 1382 |
| 1383 // Since we never managed to initialize a ProxyResolver, nothing should have |
| 1384 // been sent to it. |
| 1385 ASSERT_EQ(0u, resolver->pending_requests().size()); |
| 1386 |
| 1387 // Verify that requests ran as expected -- they should have fallen back to |
| 1388 // the manual proxy configuration for HTTP urls. |
| 1389 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 1390 EXPECT_EQ("foopy:80", info1.proxy_server().ToURI()); |
| 1391 |
| 1392 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 1393 EXPECT_EQ("foopy:80", info2.proxy_server().ToURI()); |
| 1394 } |
| 1395 |
| 1396 // Test that the bypass rules are NOT applied when using autodetect. |
| 1397 TEST(ProxyServiceTest, BypassDoesntApplyToPac) { |
| 1398 ProxyConfig config; |
| 1399 config.auto_detect = true; |
| 1400 config.pac_url = GURL("http://foopy/proxy.pac"); |
| 1401 config.proxy_rules.ParseFromString("http=foopy:80"); // Not used. |
| 1402 config.proxy_bypass.push_back("www.google.com"); |
| 1403 |
| 1404 MockProxyConfigService* config_service = new MockProxyConfigService(config); |
| 1405 MockAsyncProxyResolverExpectsBytes* resolver = |
| 1406 new MockAsyncProxyResolverExpectsBytes; |
| 1407 ProxyService service(config_service, resolver); |
| 1408 |
| 1409 MockProxyScriptFetcher* fetcher = new MockProxyScriptFetcher; |
| 1410 service.SetProxyScriptFetcher(fetcher); |
| 1411 |
| 1412 // Start 1 requests. |
| 1413 |
| 1414 ProxyInfo info1; |
| 1415 TestCompletionCallback callback1; |
| 1416 int rv = service.ResolveProxy( |
| 1417 GURL("http://www.google.com"), &info1, &callback1, NULL); |
| 1418 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1419 |
| 1420 // Check that nothing has been sent to the proxy resolver yet. |
| 1421 ASSERT_EQ(0u, resolver->pending_requests().size()); |
| 1422 |
| 1423 // It should be trying to auto-detect first -- succeed the download. |
| 1424 EXPECT_TRUE(fetcher->has_pending_request()); |
| 1425 EXPECT_EQ(GURL("http://wpad/wpad.dat"), fetcher->pending_request_url()); |
| 1426 fetcher->NotifyFetchCompletion(OK, "auto-detect"); |
| 1427 |
| 1428 EXPECT_EQ("auto-detect", |
| 1429 resolver->pending_set_pac_script_request()->pac_bytes()); |
| 1430 resolver->pending_set_pac_script_request()->CompleteNow(OK); |
| 1431 |
| 1432 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 1433 EXPECT_EQ(GURL("http://www.google.com"), |
| 1434 resolver->pending_requests()[0]->url()); |
| 1435 |
| 1436 // Complete the pending request. |
| 1437 resolver->pending_requests()[0]->results()->UseNamedProxy("request1:80"); |
| 1438 resolver->pending_requests()[0]->CompleteNow(OK); |
| 1439 |
| 1440 // Verify that request ran as expected. |
| 1441 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 1442 EXPECT_EQ("request1:80", info1.proxy_server().ToURI()); |
| 1443 |
| 1444 // Start another request, it should pickup the bypass item. |
| 1445 ProxyInfo info2; |
| 1446 TestCompletionCallback callback2; |
| 1447 rv = service.ResolveProxy( |
| 1448 GURL("http://www.google.com"), &info2, &callback2, NULL); |
| 1449 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1450 |
| 1451 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 1452 EXPECT_EQ(GURL("http://www.google.com"), |
| 1453 resolver->pending_requests()[0]->url()); |
| 1454 |
| 1455 // Complete the pending request. |
| 1456 resolver->pending_requests()[0]->results()->UseNamedProxy("request2:80"); |
| 1457 resolver->pending_requests()[0]->CompleteNow(OK); |
| 1458 |
| 1459 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 1460 EXPECT_EQ("request2:80", info2.proxy_server().ToURI()); |
| 1461 } |
| 1462 |
| 1114 TEST(ProxyServiceTest, ResetProxyConfigService) { | 1463 TEST(ProxyServiceTest, ResetProxyConfigService) { |
| 1115 ProxyConfig config1; | 1464 ProxyConfig config1; |
| 1116 config1.proxy_rules.ParseFromString("foopy1:8080"); | 1465 config1.proxy_rules.ParseFromString("foopy1:8080"); |
| 1117 config1.auto_detect = false; | 1466 config1.auto_detect = false; |
| 1118 ProxyService service(new MockProxyConfigService(config1), | 1467 ProxyService service(new MockProxyConfigService(config1), |
| 1119 new MockAsyncProxyResolverExpectsBytes); | 1468 new MockAsyncProxyResolverExpectsBytes); |
| 1120 | 1469 |
| 1121 ProxyInfo info; | 1470 ProxyInfo info; |
| 1122 TestCompletionCallback callback1; | 1471 TestCompletionCallback callback1; |
| 1123 int rv = service.ResolveProxy( | 1472 int rv = service.ResolveProxy( |
| 1124 GURL("http://request1"), &info, &callback1, NULL); | 1473 GURL("http://request1"), &info, &callback1, NULL); |
| 1125 EXPECT_EQ(OK, rv); | 1474 EXPECT_EQ(OK, rv); |
| 1126 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); | 1475 EXPECT_EQ("foopy1:8080", info.proxy_server().ToURI()); |
| 1127 | 1476 |
| 1128 ProxyConfig config2; | 1477 ProxyConfig config2; |
| 1129 config2.proxy_rules.ParseFromString("foopy2:8080"); | 1478 config2.proxy_rules.ParseFromString("foopy2:8080"); |
| 1130 config2.auto_detect = false; | 1479 config2.auto_detect = false; |
| 1131 service.ResetConfigService(new MockProxyConfigService(config2)); | 1480 service.ResetConfigService(new MockProxyConfigService(config2)); |
| 1132 TestCompletionCallback callback2; | 1481 TestCompletionCallback callback2; |
| 1133 rv = service.ResolveProxy(GURL("http://request2"), &info, &callback2, NULL); | 1482 rv = service.ResolveProxy(GURL("http://request2"), &info, &callback2, NULL); |
| 1134 EXPECT_EQ(OK, rv); | 1483 EXPECT_EQ(OK, rv); |
| 1135 EXPECT_EQ("foopy2:8080", info.proxy_server().ToURI()); | 1484 EXPECT_EQ("foopy2:8080", info.proxy_server().ToURI()); |
| 1136 } | 1485 } |
| 1137 | 1486 |
| 1138 TEST(ProxyServiceTest, IsLocalName) { | 1487 TEST(ProxyServiceTest, IsLocalName) { |
| 1139 const struct { | 1488 const struct { |
| 1140 const char* url; | 1489 const char* url; |
| 1141 bool expected_is_local; | 1490 bool expected_is_local; |
| 1142 } tests[] = { | 1491 } tests[] = { |
| 1143 // Single-component hostnames are considered local. | 1492 // Single-component hostnames are considered local. |
| 1144 {"http://localhost/x", true}, | 1493 {"http://localhost/x", true}, |
| 1145 {"http://www", true}, | 1494 {"http://www", true}, |
| 1146 | 1495 |
| 1147 // IPv4 loopback interface. | 1496 // IPv4 loopback interface. |
| 1148 {"http://127.0.0.1/x", true}, | 1497 {"http://127.0.0.1/x", true}, |
| 1149 {"http://127.0.0.1:80/x", true}, | 1498 {"http://127.0.0.1:80/x", true}, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1170 }; | 1519 }; |
| 1171 | 1520 |
| 1172 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 1521 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 1173 SCOPED_TRACE(StringPrintf("Test[%d]: %s", i, tests[i].url)); | 1522 SCOPED_TRACE(StringPrintf("Test[%d]: %s", i, tests[i].url)); |
| 1174 bool is_local = ProxyService::IsLocalName(GURL(tests[i].url)); | 1523 bool is_local = ProxyService::IsLocalName(GURL(tests[i].url)); |
| 1175 EXPECT_EQ(tests[i].expected_is_local, is_local); | 1524 EXPECT_EQ(tests[i].expected_is_local, is_local); |
| 1176 } | 1525 } |
| 1177 } | 1526 } |
| 1178 | 1527 |
| 1179 } // namespace net | 1528 } // namespace net |
| OLD | NEW |