Roll src/third_party/WebKit a2aeeb7:6373c4f (svn 198318:198336)
[chromium-blink-merge.git] / net / proxy / proxy_resolver_factory_mojo_unittest.cc
blob0c3dbabfcc7b650c3932d94aed3b579bd493ba2e
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/proxy/proxy_resolver_factory_mojo.h"
7 #include <list>
8 #include <map>
9 #include <queue>
10 #include <string>
12 #include "base/bind.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/run_loop.h"
16 #include "base/stl_util.h"
17 #include "mojo/common/common_type_converters.h"
18 #include "net/base/load_states.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/test_completion_callback.h"
21 #include "net/log/net_log.h"
22 #include "net/proxy/mojo_proxy_resolver_factory.h"
23 #include "net/proxy/mojo_proxy_type_converters.h"
24 #include "net/proxy/proxy_info.h"
25 #include "net/proxy/proxy_resolver.h"
26 #include "net/proxy/proxy_resolver_error_observer.h"
27 #include "net/proxy/proxy_resolver_script_data.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
30 #include "url/gurl.h"
32 namespace net {
34 namespace {
36 const char kScriptData[] = "FooBarBaz";
37 const char kExampleUrl[] = "http://www.example.com";
39 struct CreateProxyResolverAction {
40 enum Action {
41 COMPLETE,
42 DROP_CLIENT,
43 DROP_RESOLVER,
44 DROP_BOTH,
45 WAIT_FOR_CLIENT_DISCONNECT,
48 static CreateProxyResolverAction ReturnResult(
49 const std::string& expected_pac_script,
50 Error error) {
51 CreateProxyResolverAction result;
52 result.expected_pac_script = expected_pac_script;
53 result.error = error;
54 return result;
57 static CreateProxyResolverAction DropClient(
58 const std::string& expected_pac_script) {
59 CreateProxyResolverAction result;
60 result.expected_pac_script = expected_pac_script;
61 result.action = DROP_CLIENT;
62 return result;
65 static CreateProxyResolverAction DropResolver(
66 const std::string& expected_pac_script) {
67 CreateProxyResolverAction result;
68 result.expected_pac_script = expected_pac_script;
69 result.action = DROP_RESOLVER;
70 return result;
73 static CreateProxyResolverAction DropBoth(
74 const std::string& expected_pac_script) {
75 CreateProxyResolverAction result;
76 result.expected_pac_script = expected_pac_script;
77 result.action = DROP_BOTH;
78 return result;
81 static CreateProxyResolverAction WaitForClientDisconnect(
82 const std::string& expected_pac_script) {
83 CreateProxyResolverAction result;
84 result.expected_pac_script = expected_pac_script;
85 result.action = WAIT_FOR_CLIENT_DISCONNECT;
86 return result;
89 std::string expected_pac_script;
90 Action action = COMPLETE;
91 Error error = OK;
94 struct GetProxyForUrlAction {
95 enum Action {
96 COMPLETE,
97 // Drop the request by closing the reply channel.
98 DROP,
99 // Disconnect the service.
100 DISCONNECT,
101 // Wait for the client pipe to be disconnected.
102 WAIT_FOR_CLIENT_DISCONNECT,
105 GetProxyForUrlAction() {}
106 GetProxyForUrlAction(const GetProxyForUrlAction& old) {
107 action = old.action;
108 error = old.error;
109 expected_url = old.expected_url;
110 proxy_servers = old.proxy_servers.Clone();
113 static GetProxyForUrlAction ReturnError(const GURL& url, Error error) {
114 GetProxyForUrlAction result;
115 result.expected_url = url;
116 result.error = error;
117 return result;
120 static GetProxyForUrlAction ReturnServers(
121 const GURL& url,
122 const mojo::Array<interfaces::ProxyServerPtr>& proxy_servers) {
123 GetProxyForUrlAction result;
124 result.expected_url = url;
125 result.proxy_servers = proxy_servers.Clone();
126 return result;
129 static GetProxyForUrlAction DropRequest(const GURL& url) {
130 GetProxyForUrlAction result;
131 result.expected_url = url;
132 result.action = DROP;
133 return result;
136 static GetProxyForUrlAction Disconnect(const GURL& url) {
137 GetProxyForUrlAction result;
138 result.expected_url = url;
139 result.action = DISCONNECT;
140 return result;
143 static GetProxyForUrlAction WaitForClientDisconnect(const GURL& url) {
144 GetProxyForUrlAction result;
145 result.expected_url = url;
146 result.action = WAIT_FOR_CLIENT_DISCONNECT;
147 return result;
150 Action action = COMPLETE;
151 Error error = OK;
152 mojo::Array<interfaces::ProxyServerPtr> proxy_servers;
153 GURL expected_url;
156 class MockMojoProxyResolver : public interfaces::ProxyResolver {
157 public:
158 MockMojoProxyResolver();
159 ~MockMojoProxyResolver() override;
161 void AddGetProxyAction(GetProxyForUrlAction action);
163 void WaitForNextRequest();
165 void ClearBlockedClients();
167 void AddConnection(mojo::InterfaceRequest<interfaces::ProxyResolver> req);
169 private:
170 // Overridden from interfaces::ProxyResolver:
171 void GetProxyForUrl(
172 const mojo::String& url,
173 interfaces::ProxyResolverRequestClientPtr client) override;
175 void WakeWaiter();
177 std::string pac_script_data_;
179 std::queue<GetProxyForUrlAction> get_proxy_actions_;
181 base::Closure quit_closure_;
183 ScopedVector<interfaces::ProxyResolverRequestClientPtr> blocked_clients_;
184 mojo::Binding<interfaces::ProxyResolver> binding_;
187 MockMojoProxyResolver::~MockMojoProxyResolver() {
188 EXPECT_TRUE(get_proxy_actions_.empty())
189 << "Actions remaining: " << get_proxy_actions_.size();
192 MockMojoProxyResolver::MockMojoProxyResolver() : binding_(this) {
195 void MockMojoProxyResolver::AddGetProxyAction(GetProxyForUrlAction action) {
196 get_proxy_actions_.push(action);
199 void MockMojoProxyResolver::WaitForNextRequest() {
200 base::RunLoop run_loop;
201 quit_closure_ = run_loop.QuitClosure();
202 run_loop.Run();
205 void MockMojoProxyResolver::WakeWaiter() {
206 if (!quit_closure_.is_null())
207 quit_closure_.Run();
208 quit_closure_.Reset();
211 void MockMojoProxyResolver::ClearBlockedClients() {
212 blocked_clients_.clear();
215 void MockMojoProxyResolver::AddConnection(
216 mojo::InterfaceRequest<interfaces::ProxyResolver> req) {
217 if (binding_.is_bound())
218 binding_.Close();
219 binding_.Bind(req.Pass());
222 void MockMojoProxyResolver::GetProxyForUrl(
223 const mojo::String& url,
224 interfaces::ProxyResolverRequestClientPtr client) {
225 ASSERT_FALSE(get_proxy_actions_.empty());
226 GetProxyForUrlAction action = get_proxy_actions_.front();
227 get_proxy_actions_.pop();
229 EXPECT_EQ(action.expected_url.spec(), url.To<std::string>());
230 switch (action.action) {
231 case GetProxyForUrlAction::COMPLETE:
232 client->ReportResult(action.error, action.proxy_servers.Pass());
233 break;
234 case GetProxyForUrlAction::DROP:
235 client.reset();
236 break;
237 case GetProxyForUrlAction::DISCONNECT:
238 binding_.Close();
239 break;
240 case GetProxyForUrlAction::WAIT_FOR_CLIENT_DISCONNECT:
241 ASSERT_FALSE(client.WaitForIncomingResponse());
242 break;
244 WakeWaiter();
247 class Request {
248 public:
249 Request(ProxyResolver* resolver, const GURL& url);
251 int Resolve();
252 void Cancel();
253 int WaitForResult();
255 int error() const { return error_; }
256 const ProxyInfo& results() const { return results_; }
258 private:
259 ProxyResolver* resolver_;
260 const GURL url_;
261 ProxyInfo results_;
262 ProxyResolver::RequestHandle handle_;
263 int error_;
264 TestCompletionCallback callback_;
267 Request::Request(ProxyResolver* resolver, const GURL& url)
268 : resolver_(resolver), url_(url), error_(0) {
271 int Request::Resolve() {
272 BoundNetLog net_log;
273 error_ = resolver_->GetProxyForURL(url_, &results_, callback_.callback(),
274 &handle_, net_log);
275 return error_;
278 void Request::Cancel() {
279 resolver_->CancelRequest(handle_);
282 int Request::WaitForResult() {
283 error_ = callback_.WaitForResult();
284 return error_;
287 class MockMojoProxyResolverFactory : public interfaces::ProxyResolverFactory {
288 public:
289 MockMojoProxyResolverFactory(
290 MockMojoProxyResolver* resolver,
291 mojo::InterfaceRequest<interfaces::ProxyResolverFactory> req);
292 ~MockMojoProxyResolverFactory() override;
294 void AddCreateProxyResolverAction(CreateProxyResolverAction action);
296 void WaitForNextRequest();
298 void ClearBlockedClients();
300 private:
301 // Overridden from interfaces::ProxyResolver:
302 void CreateResolver(
303 const mojo::String& pac_url,
304 mojo::InterfaceRequest<interfaces::ProxyResolver> request,
305 interfaces::HostResolverPtr host_resolver,
306 interfaces::ProxyResolverErrorObserverPtr error_observer,
307 interfaces::ProxyResolverFactoryRequestClientPtr client) override;
309 void WakeWaiter();
311 MockMojoProxyResolver* resolver_;
312 std::queue<CreateProxyResolverAction> create_resolver_actions_;
314 base::Closure quit_closure_;
316 ScopedVector<interfaces::ProxyResolverFactoryRequestClientPtr>
317 blocked_clients_;
318 ScopedVector<mojo::InterfaceRequest<interfaces::ProxyResolver>>
319 blocked_resolver_requests_;
320 mojo::Binding<interfaces::ProxyResolverFactory> binding_;
323 MockMojoProxyResolverFactory::MockMojoProxyResolverFactory(
324 MockMojoProxyResolver* resolver,
325 mojo::InterfaceRequest<interfaces::ProxyResolverFactory> req)
326 : resolver_(resolver), binding_(this, req.Pass()) {
329 MockMojoProxyResolverFactory::~MockMojoProxyResolverFactory() {
330 EXPECT_TRUE(create_resolver_actions_.empty())
331 << "Actions remaining: " << create_resolver_actions_.size();
334 void MockMojoProxyResolverFactory::AddCreateProxyResolverAction(
335 CreateProxyResolverAction action) {
336 create_resolver_actions_.push(action);
339 void MockMojoProxyResolverFactory::WaitForNextRequest() {
340 base::RunLoop run_loop;
341 quit_closure_ = run_loop.QuitClosure();
342 run_loop.Run();
345 void MockMojoProxyResolverFactory::WakeWaiter() {
346 if (!quit_closure_.is_null())
347 quit_closure_.Run();
348 quit_closure_.Reset();
351 void MockMojoProxyResolverFactory::ClearBlockedClients() {
352 blocked_clients_.clear();
355 void MockMojoProxyResolverFactory::CreateResolver(
356 const mojo::String& pac_script,
357 mojo::InterfaceRequest<interfaces::ProxyResolver> request,
358 interfaces::HostResolverPtr host_resolver,
359 interfaces::ProxyResolverErrorObserverPtr error_observer,
360 interfaces::ProxyResolverFactoryRequestClientPtr client) {
361 ASSERT_FALSE(create_resolver_actions_.empty());
362 CreateProxyResolverAction action = create_resolver_actions_.front();
363 create_resolver_actions_.pop();
365 EXPECT_EQ(action.expected_pac_script, pac_script.To<std::string>());
366 switch (action.action) {
367 case CreateProxyResolverAction::COMPLETE:
368 if (action.error == OK)
369 resolver_->AddConnection(request.Pass());
370 client->ReportResult(action.error);
371 break;
372 case CreateProxyResolverAction::DROP_CLIENT:
373 // Save |request| so its pipe isn't closed.
374 blocked_resolver_requests_.push_back(
375 new mojo::InterfaceRequest<interfaces::ProxyResolver>(
376 request.Pass()));
377 break;
378 case CreateProxyResolverAction::DROP_RESOLVER:
379 // Save |client| so its pipe isn't closed.
380 blocked_clients_.push_back(
381 new interfaces::ProxyResolverFactoryRequestClientPtr(client.Pass()));
382 break;
383 case CreateProxyResolverAction::DROP_BOTH:
384 // Both |request| and |client| will be closed.
385 break;
386 case CreateProxyResolverAction::WAIT_FOR_CLIENT_DISCONNECT:
387 ASSERT_FALSE(client.WaitForIncomingResponse());
388 break;
390 WakeWaiter();
393 void DeleteResolverFactoryRequestCallback(
394 scoped_ptr<ProxyResolverFactory::Request>* request,
395 const CompletionCallback& callback,
396 int result) {
397 ASSERT_TRUE(request);
398 EXPECT_TRUE(request->get());
399 request->reset();
400 callback.Run(result);
403 } // namespace
405 class ProxyResolverFactoryMojoTest : public testing::Test,
406 public MojoProxyResolverFactory {
407 public:
408 void SetUp() override {
409 mock_proxy_resolver_factory_.reset(new MockMojoProxyResolverFactory(
410 &mock_proxy_resolver_, mojo::GetProxy(&factory_ptr_)));
411 proxy_resolver_factory_mojo_.reset(new ProxyResolverFactoryMojo(
412 this, nullptr,
413 base::Callback<scoped_ptr<ProxyResolverErrorObserver>()>()));
416 scoped_ptr<Request> MakeRequest(const GURL& url) {
417 return make_scoped_ptr(new Request(proxy_resolver_mojo_.get(), url));
420 scoped_ptr<base::ScopedClosureRunner> CreateResolver(
421 const mojo::String& pac_script,
422 mojo::InterfaceRequest<interfaces::ProxyResolver> req,
423 interfaces::HostResolverPtr host_resolver,
424 interfaces::ProxyResolverErrorObserverPtr error_observer,
425 interfaces::ProxyResolverFactoryRequestClientPtr client) override {
426 factory_ptr_->CreateResolver(pac_script, req.Pass(), host_resolver.Pass(),
427 error_observer.Pass(), client.Pass());
428 return make_scoped_ptr(
429 new base::ScopedClosureRunner(on_delete_callback_.closure()));
432 mojo::Array<interfaces::ProxyServerPtr> ProxyServersFromPacString(
433 const std::string& pac_string) {
434 ProxyInfo proxy_info;
435 proxy_info.UsePacString(pac_string);
437 return mojo::Array<interfaces::ProxyServerPtr>::From(
438 proxy_info.proxy_list().GetAll());
441 void CreateProxyResolver() {
442 mock_proxy_resolver_factory_->AddCreateProxyResolverAction(
443 CreateProxyResolverAction::ReturnResult(kScriptData, OK));
444 TestCompletionCallback callback;
445 scoped_refptr<ProxyResolverScriptData> pac_script(
446 ProxyResolverScriptData::FromUTF8(kScriptData));
447 scoped_ptr<ProxyResolverFactory::Request> request;
448 ASSERT_EQ(
450 callback.GetResult(proxy_resolver_factory_mojo_->CreateProxyResolver(
451 pac_script, &proxy_resolver_mojo_, callback.callback(), &request)));
452 EXPECT_TRUE(request);
453 ASSERT_TRUE(proxy_resolver_mojo_);
456 void DeleteProxyResolverCallback(const CompletionCallback& callback,
457 int result) {
458 proxy_resolver_mojo_.reset();
459 callback.Run(result);
462 scoped_ptr<MockMojoProxyResolverFactory> mock_proxy_resolver_factory_;
463 interfaces::ProxyResolverFactoryPtr factory_ptr_;
464 scoped_ptr<ProxyResolverFactory> proxy_resolver_factory_mojo_;
466 MockMojoProxyResolver mock_proxy_resolver_;
467 scoped_ptr<ProxyResolver> proxy_resolver_mojo_;
468 TestClosure on_delete_callback_;
471 TEST_F(ProxyResolverFactoryMojoTest, CreateProxyResolver) {
472 CreateProxyResolver();
475 TEST_F(ProxyResolverFactoryMojoTest, CreateProxyResolver_Empty) {
476 TestCompletionCallback callback;
477 scoped_refptr<ProxyResolverScriptData> pac_script(
478 ProxyResolverScriptData::FromUTF8(""));
479 scoped_ptr<ProxyResolverFactory::Request> request;
480 EXPECT_EQ(
481 ERR_PAC_SCRIPT_FAILED,
482 callback.GetResult(proxy_resolver_factory_mojo_->CreateProxyResolver(
483 pac_script, &proxy_resolver_mojo_, callback.callback(), &request)));
484 EXPECT_FALSE(request);
487 TEST_F(ProxyResolverFactoryMojoTest, CreateProxyResolver_Url) {
488 TestCompletionCallback callback;
489 scoped_refptr<ProxyResolverScriptData> pac_script(
490 ProxyResolverScriptData::FromURL(GURL(kExampleUrl)));
491 scoped_ptr<ProxyResolverFactory::Request> request;
492 EXPECT_EQ(
493 ERR_PAC_SCRIPT_FAILED,
494 callback.GetResult(proxy_resolver_factory_mojo_->CreateProxyResolver(
495 pac_script, &proxy_resolver_mojo_, callback.callback(), &request)));
496 EXPECT_FALSE(request);
499 TEST_F(ProxyResolverFactoryMojoTest, CreateProxyResolver_Failed) {
500 mock_proxy_resolver_factory_->AddCreateProxyResolverAction(
501 CreateProxyResolverAction::ReturnResult(kScriptData,
502 ERR_PAC_STATUS_NOT_OK));
504 TestCompletionCallback callback;
505 scoped_refptr<ProxyResolverScriptData> pac_script(
506 ProxyResolverScriptData::FromUTF8(kScriptData));
507 scoped_ptr<ProxyResolverFactory::Request> request;
508 EXPECT_EQ(
509 ERR_PAC_STATUS_NOT_OK,
510 callback.GetResult(proxy_resolver_factory_mojo_->CreateProxyResolver(
511 pac_script, &proxy_resolver_mojo_, callback.callback(), &request)));
512 EXPECT_TRUE(request);
513 on_delete_callback_.WaitForResult();
515 // A second attempt succeeds.
516 CreateProxyResolver();
519 TEST_F(ProxyResolverFactoryMojoTest, CreateProxyResolver_BothDisconnected) {
520 mock_proxy_resolver_factory_->AddCreateProxyResolverAction(
521 CreateProxyResolverAction::DropBoth(kScriptData));
523 scoped_refptr<ProxyResolverScriptData> pac_script(
524 ProxyResolverScriptData::FromUTF8(kScriptData));
525 scoped_ptr<ProxyResolverFactory::Request> request;
526 TestCompletionCallback callback;
527 EXPECT_EQ(
528 ERR_PAC_SCRIPT_TERMINATED,
529 callback.GetResult(proxy_resolver_factory_mojo_->CreateProxyResolver(
530 pac_script, &proxy_resolver_mojo_, callback.callback(), &request)));
531 EXPECT_TRUE(request);
534 TEST_F(ProxyResolverFactoryMojoTest, CreateProxyResolver_ClientDisconnected) {
535 mock_proxy_resolver_factory_->AddCreateProxyResolverAction(
536 CreateProxyResolverAction::DropClient(kScriptData));
538 scoped_refptr<ProxyResolverScriptData> pac_script(
539 ProxyResolverScriptData::FromUTF8(kScriptData));
540 scoped_ptr<ProxyResolverFactory::Request> request;
541 TestCompletionCallback callback;
542 EXPECT_EQ(
543 ERR_PAC_SCRIPT_TERMINATED,
544 callback.GetResult(proxy_resolver_factory_mojo_->CreateProxyResolver(
545 pac_script, &proxy_resolver_mojo_, callback.callback(), &request)));
546 EXPECT_TRUE(request);
549 TEST_F(ProxyResolverFactoryMojoTest, CreateProxyResolver_ResolverDisconnected) {
550 mock_proxy_resolver_factory_->AddCreateProxyResolverAction(
551 CreateProxyResolverAction::DropResolver(kScriptData));
553 scoped_refptr<ProxyResolverScriptData> pac_script(
554 ProxyResolverScriptData::FromUTF8(kScriptData));
555 scoped_ptr<ProxyResolverFactory::Request> request;
556 TestCompletionCallback callback;
557 EXPECT_EQ(
558 ERR_PAC_SCRIPT_TERMINATED,
559 callback.GetResult(proxy_resolver_factory_mojo_->CreateProxyResolver(
560 pac_script, &proxy_resolver_mojo_, callback.callback(), &request)));
561 EXPECT_TRUE(request);
562 on_delete_callback_.WaitForResult();
565 TEST_F(ProxyResolverFactoryMojoTest,
566 CreateProxyResolver_ResolverDisconnected_DeleteRequestInCallback) {
567 mock_proxy_resolver_factory_->AddCreateProxyResolverAction(
568 CreateProxyResolverAction::DropResolver(kScriptData));
570 scoped_refptr<ProxyResolverScriptData> pac_script(
571 ProxyResolverScriptData::FromUTF8(kScriptData));
572 scoped_ptr<ProxyResolverFactory::Request> request;
573 TestCompletionCallback callback;
574 EXPECT_EQ(
575 ERR_PAC_SCRIPT_TERMINATED,
576 callback.GetResult(proxy_resolver_factory_mojo_->CreateProxyResolver(
577 pac_script, &proxy_resolver_mojo_,
578 base::Bind(&DeleteResolverFactoryRequestCallback, &request,
579 callback.callback()),
580 &request)));
581 on_delete_callback_.WaitForResult();
584 TEST_F(ProxyResolverFactoryMojoTest, CreateProxyResolver_Cancel) {
585 mock_proxy_resolver_factory_->AddCreateProxyResolverAction(
586 CreateProxyResolverAction::WaitForClientDisconnect(kScriptData));
588 scoped_refptr<ProxyResolverScriptData> pac_script(
589 ProxyResolverScriptData::FromUTF8(kScriptData));
590 scoped_ptr<ProxyResolverFactory::Request> request;
591 TestCompletionCallback callback;
592 EXPECT_EQ(ERR_IO_PENDING, proxy_resolver_factory_mojo_->CreateProxyResolver(
593 pac_script, &proxy_resolver_mojo_,
594 callback.callback(), &request));
595 ASSERT_TRUE(request);
596 request.reset();
598 // The Mojo request is still made.
599 mock_proxy_resolver_factory_->WaitForNextRequest();
600 on_delete_callback_.WaitForResult();
603 TEST_F(ProxyResolverFactoryMojoTest, GetProxyForURL) {
604 mock_proxy_resolver_.AddGetProxyAction(GetProxyForUrlAction::ReturnServers(
605 GURL(kExampleUrl), ProxyServersFromPacString("DIRECT")));
606 CreateProxyResolver();
608 scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl)));
609 EXPECT_EQ(ERR_IO_PENDING, request->Resolve());
610 EXPECT_EQ(OK, request->WaitForResult());
612 EXPECT_EQ("DIRECT", request->results().ToPacString());
615 TEST_F(ProxyResolverFactoryMojoTest, GetProxyForURL_MultipleResults) {
616 static const char kPacString[] =
617 "PROXY foo1:80;DIRECT;SOCKS foo2:1234;"
618 "SOCKS5 foo3:1080;HTTPS foo4:443;QUIC foo6:8888";
619 mock_proxy_resolver_.AddGetProxyAction(GetProxyForUrlAction::ReturnServers(
620 GURL(kExampleUrl), ProxyServersFromPacString(kPacString)));
621 CreateProxyResolver();
623 scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl)));
624 EXPECT_EQ(ERR_IO_PENDING, request->Resolve());
625 EXPECT_EQ(OK, request->WaitForResult());
627 EXPECT_EQ(kPacString, request->results().ToPacString());
630 TEST_F(ProxyResolverFactoryMojoTest, GetProxyForURL_Error) {
631 mock_proxy_resolver_.AddGetProxyAction(
632 GetProxyForUrlAction::ReturnError(GURL(kExampleUrl), ERR_UNEXPECTED));
633 CreateProxyResolver();
635 scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl)));
636 EXPECT_EQ(ERR_IO_PENDING, request->Resolve());
637 EXPECT_EQ(ERR_UNEXPECTED, request->WaitForResult());
639 EXPECT_TRUE(request->results().is_empty());
642 TEST_F(ProxyResolverFactoryMojoTest, GetProxyForURL_Cancel) {
643 mock_proxy_resolver_.AddGetProxyAction(
644 GetProxyForUrlAction::WaitForClientDisconnect(GURL(kExampleUrl)));
645 CreateProxyResolver();
647 scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl)));
648 EXPECT_EQ(ERR_IO_PENDING, request->Resolve());
649 request->Cancel();
651 // The Mojo request is still made.
652 mock_proxy_resolver_.WaitForNextRequest();
655 TEST_F(ProxyResolverFactoryMojoTest, GetProxyForURL_MultipleRequests) {
656 mock_proxy_resolver_.AddGetProxyAction(GetProxyForUrlAction::ReturnServers(
657 GURL(kExampleUrl), ProxyServersFromPacString("DIRECT")));
658 mock_proxy_resolver_.AddGetProxyAction(GetProxyForUrlAction::ReturnServers(
659 GURL("https://www.chromium.org"),
660 ProxyServersFromPacString("HTTPS foo:443")));
661 CreateProxyResolver();
663 scoped_ptr<Request> request1(MakeRequest(GURL(kExampleUrl)));
664 EXPECT_EQ(ERR_IO_PENDING, request1->Resolve());
665 scoped_ptr<Request> request2(MakeRequest(GURL("https://www.chromium.org")));
666 EXPECT_EQ(ERR_IO_PENDING, request2->Resolve());
668 EXPECT_EQ(OK, request1->WaitForResult());
669 EXPECT_EQ(OK, request2->WaitForResult());
671 EXPECT_EQ("DIRECT", request1->results().ToPacString());
672 EXPECT_EQ("HTTPS foo:443", request2->results().ToPacString());
675 TEST_F(ProxyResolverFactoryMojoTest, GetProxyForURL_Disconnect) {
676 mock_proxy_resolver_.AddGetProxyAction(
677 GetProxyForUrlAction::Disconnect(GURL(kExampleUrl)));
678 CreateProxyResolver();
680 scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl)));
681 EXPECT_EQ(ERR_IO_PENDING, request->Resolve());
682 EXPECT_EQ(ERR_PAC_SCRIPT_TERMINATED, request->WaitForResult());
683 EXPECT_TRUE(request->results().is_empty());
687 // Calling GetProxyForURL after a disconnect should fail.
688 scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl)));
689 EXPECT_EQ(ERR_PAC_SCRIPT_TERMINATED, request->Resolve());
693 TEST_F(ProxyResolverFactoryMojoTest, GetProxyForURL_ClientClosed) {
694 mock_proxy_resolver_.AddGetProxyAction(
695 GetProxyForUrlAction::DropRequest(GURL(kExampleUrl)));
696 CreateProxyResolver();
698 scoped_ptr<Request> request1(MakeRequest(GURL(kExampleUrl)));
699 EXPECT_EQ(ERR_IO_PENDING, request1->Resolve());
701 EXPECT_EQ(ERR_PAC_SCRIPT_TERMINATED, request1->WaitForResult());
704 TEST_F(ProxyResolverFactoryMojoTest, GetProxyForURL_DeleteInCallback) {
705 mock_proxy_resolver_.AddGetProxyAction(GetProxyForUrlAction::ReturnServers(
706 GURL(kExampleUrl), ProxyServersFromPacString("DIRECT")));
707 CreateProxyResolver();
709 ProxyInfo results;
710 TestCompletionCallback callback;
711 ProxyResolver::RequestHandle handle;
712 BoundNetLog net_log;
713 EXPECT_EQ(
715 callback.GetResult(proxy_resolver_mojo_->GetProxyForURL(
716 GURL(kExampleUrl), &results,
717 base::Bind(&ProxyResolverFactoryMojoTest::DeleteProxyResolverCallback,
718 base::Unretained(this), callback.callback()),
719 &handle, net_log)));
720 on_delete_callback_.WaitForResult();
723 TEST_F(ProxyResolverFactoryMojoTest,
724 GetProxyForURL_DeleteInCallbackFromDisconnect) {
725 mock_proxy_resolver_.AddGetProxyAction(
726 GetProxyForUrlAction::Disconnect(GURL(kExampleUrl)));
727 CreateProxyResolver();
729 ProxyInfo results;
730 TestCompletionCallback callback;
731 ProxyResolver::RequestHandle handle;
732 BoundNetLog net_log;
733 EXPECT_EQ(
734 ERR_PAC_SCRIPT_TERMINATED,
735 callback.GetResult(proxy_resolver_mojo_->GetProxyForURL(
736 GURL(kExampleUrl), &results,
737 base::Bind(&ProxyResolverFactoryMojoTest::DeleteProxyResolverCallback,
738 base::Unretained(this), callback.callback()),
739 &handle, net_log)));
740 on_delete_callback_.WaitForResult();
743 TEST_F(ProxyResolverFactoryMojoTest, DeleteResolver) {
744 CreateProxyResolver();
745 proxy_resolver_mojo_.reset();
746 on_delete_callback_.WaitForResult();
748 } // namespace net