OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/proxy/mojo_proxy_resolver_impl.h" | 5 #include "net/proxy/mojo_proxy_resolver_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 namespace net { | 21 namespace net { |
22 namespace { | 22 namespace { |
23 | 23 |
24 class TestRequestClient : public interfaces::ProxyResolverRequestClient, | 24 class TestRequestClient : public interfaces::ProxyResolverRequestClient, |
25 public mojo::ErrorHandler { | 25 public mojo::ErrorHandler { |
26 public: | 26 public: |
27 explicit TestRequestClient( | 27 explicit TestRequestClient( |
28 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request); | 28 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request); |
29 | 29 |
30 void WaitForResult(); | 30 void WaitForResult(); |
31 void WaitForLoadStateChange(); | |
31 void WaitForConnectionError(); | 32 void WaitForConnectionError(); |
32 | 33 |
33 Error error() { return error_; } | 34 Error error() { return error_; } |
34 const mojo::Array<interfaces::ProxyServerPtr>& results() { return results_; } | 35 const mojo::Array<interfaces::ProxyServerPtr>& results() { return results_; } |
36 LoadState load_state() { return load_state_; } | |
35 | 37 |
36 private: | 38 private: |
37 // interfaces::ProxyResolverRequestClient override. | 39 // interfaces::ProxyResolverRequestClient override. |
38 void ReportResult(int32_t error, | 40 void ReportResult(int32_t error, |
39 mojo::Array<interfaces::ProxyServerPtr> results) override; | 41 mojo::Array<interfaces::ProxyServerPtr> results) override; |
42 void LoadStateChanged(int32_t load_state) override; | |
40 | 43 |
41 // mojo::ErrorHandler override. | 44 // mojo::ErrorHandler override. |
42 void OnConnectionError() override; | 45 void OnConnectionError() override; |
43 | 46 |
44 bool done_ = false; | 47 bool done_ = false; |
45 bool encountered_connection_error_ = false; | 48 bool encountered_connection_error_ = false; |
46 Error error_ = ERR_FAILED; | 49 Error error_ = ERR_FAILED; |
50 LoadState load_state_ = LOAD_STATE_IDLE; | |
47 mojo::Array<interfaces::ProxyServerPtr> results_; | 51 mojo::Array<interfaces::ProxyServerPtr> results_; |
48 base::Closure run_loop_quit_closure_; | 52 base::Closure run_loop_quit_closure_; |
49 base::Closure connection_error_callback_; | 53 base::Closure connection_error_callback_; |
50 | 54 |
51 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; | 55 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; |
52 }; | 56 }; |
53 | 57 |
54 TestRequestClient::TestRequestClient( | 58 TestRequestClient::TestRequestClient( |
55 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request) | 59 mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request) |
56 : binding_(this, request.Pass()) { | 60 : binding_(this, request.Pass()) { |
57 binding_.set_error_handler(this); | 61 binding_.set_error_handler(this); |
58 } | 62 } |
59 | 63 |
60 void TestRequestClient::WaitForResult() { | 64 void TestRequestClient::WaitForResult() { |
61 if (done_) | 65 if (done_) |
62 return; | 66 return; |
63 | 67 |
64 base::RunLoop run_loop; | 68 base::RunLoop run_loop; |
65 run_loop_quit_closure_ = run_loop.QuitClosure(); | 69 run_loop_quit_closure_ = run_loop.QuitClosure(); |
66 run_loop.Run(); | 70 run_loop.Run(); |
67 ASSERT_TRUE(done_); | 71 ASSERT_TRUE(done_); |
68 } | 72 } |
69 | 73 |
74 void TestRequestClient::WaitForLoadStateChange() { | |
Anand Mistry (off Chromium)
2015/03/10 03:36:09
Switch to net::EventWaiter. You are, after all, wa
Sam McNally
2015/03/10 06:47:35
Done.
| |
75 base::RunLoop run_loop; | |
76 run_loop_quit_closure_ = run_loop.QuitClosure(); | |
77 run_loop.Run(); | |
78 } | |
79 | |
70 void TestRequestClient::WaitForConnectionError() { | 80 void TestRequestClient::WaitForConnectionError() { |
71 if (encountered_connection_error_) | 81 if (encountered_connection_error_) |
72 return; | 82 return; |
73 | 83 |
74 base::RunLoop run_loop; | 84 base::RunLoop run_loop; |
75 connection_error_callback_ = run_loop.QuitClosure(); | 85 connection_error_callback_ = run_loop.QuitClosure(); |
76 run_loop.Run(); | 86 run_loop.Run(); |
77 ASSERT_TRUE(encountered_connection_error_); | 87 ASSERT_TRUE(encountered_connection_error_); |
78 } | 88 } |
79 | 89 |
80 void TestRequestClient::ReportResult( | 90 void TestRequestClient::ReportResult( |
81 int32_t error, | 91 int32_t error, |
82 mojo::Array<interfaces::ProxyServerPtr> results) { | 92 mojo::Array<interfaces::ProxyServerPtr> results) { |
83 if (!run_loop_quit_closure_.is_null()) { | 93 if (!run_loop_quit_closure_.is_null()) { |
84 run_loop_quit_closure_.Run(); | 94 run_loop_quit_closure_.Run(); |
85 } | 95 } |
86 ASSERT_FALSE(done_); | 96 ASSERT_FALSE(done_); |
87 error_ = static_cast<Error>(error); | 97 error_ = static_cast<Error>(error); |
88 results_ = results.Pass(); | 98 results_ = results.Pass(); |
89 done_ = true; | 99 done_ = true; |
90 } | 100 } |
91 | 101 |
102 void TestRequestClient::LoadStateChanged(int32_t load_state) { | |
103 load_state_ = static_cast<LoadState>(load_state); | |
104 if (!run_loop_quit_closure_.is_null()) | |
105 run_loop_quit_closure_.Run(); | |
106 } | |
107 | |
92 void TestRequestClient::OnConnectionError() { | 108 void TestRequestClient::OnConnectionError() { |
93 if (!connection_error_callback_.is_null()) | 109 if (!connection_error_callback_.is_null()) |
94 connection_error_callback_.Run(); | 110 connection_error_callback_.Run(); |
95 encountered_connection_error_ = true; | 111 encountered_connection_error_ = true; |
96 } | 112 } |
97 | 113 |
98 class SetPacScriptClient { | 114 class SetPacScriptClient { |
99 public: | 115 public: |
100 base::Callback<void(int32_t)> CreateCallback(); | 116 base::Callback<void(int32_t)> CreateCallback(); |
101 Error error() { return error_; } | 117 Error error() { return error_; } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 } | 220 } |
205 | 221 |
206 } // namespace | 222 } // namespace |
207 | 223 |
208 class MojoProxyResolverImplTest : public testing::Test { | 224 class MojoProxyResolverImplTest : public testing::Test { |
209 protected: | 225 protected: |
210 void SetUp() override { | 226 void SetUp() override { |
211 scoped_ptr<CallbackMockProxyResolver> mock_resolver( | 227 scoped_ptr<CallbackMockProxyResolver> mock_resolver( |
212 new CallbackMockProxyResolver); | 228 new CallbackMockProxyResolver); |
213 mock_proxy_resolver_ = mock_resolver.get(); | 229 mock_proxy_resolver_ = mock_resolver.get(); |
214 resolver_.reset(new MojoProxyResolverImpl(mock_resolver.Pass())); | 230 resolver_impl_.reset(new MojoProxyResolverImpl(mock_resolver.Pass())); |
231 resolver_ = resolver_impl_.get(); | |
215 } | 232 } |
216 | 233 |
217 CallbackMockProxyResolver* mock_proxy_resolver_; | 234 CallbackMockProxyResolver* mock_proxy_resolver_; |
218 | 235 |
219 scoped_ptr<interfaces::ProxyResolver> resolver_; | 236 scoped_ptr<MojoProxyResolverImpl> resolver_impl_; |
237 interfaces::ProxyResolver* resolver_; | |
220 }; | 238 }; |
221 | 239 |
222 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { | 240 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { |
223 interfaces::ProxyResolverRequestClientPtr client_ptr; | 241 interfaces::ProxyResolverRequestClientPtr client_ptr; |
224 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 242 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
225 | 243 |
226 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 244 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); |
227 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 245 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
228 scoped_refptr<MockAsyncProxyResolverBase::Request> request = | 246 scoped_refptr<MockAsyncProxyResolverBase::Request> request = |
229 mock_proxy_resolver_->pending_requests()[0]; | 247 mock_proxy_resolver_->pending_requests()[0]; |
230 EXPECT_EQ(GURL("http://example.com"), request->url()); | 248 EXPECT_EQ(GURL("http://example.com"), request->url()); |
249 | |
250 resolver_impl_->LoadStateChanged(request.get(), | |
251 LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | |
252 client.WaitForLoadStateChange(); | |
253 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT, client.load_state()); | |
254 | |
231 request->results()->UsePacString( | 255 request->results()->UsePacString( |
232 "PROXY proxy.example.com:1; " | 256 "PROXY proxy.example.com:1; " |
233 "SOCKS4 socks4.example.com:2; " | 257 "SOCKS4 socks4.example.com:2; " |
234 "SOCKS5 socks5.example.com:3; " | 258 "SOCKS5 socks5.example.com:3; " |
235 "HTTPS https.example.com:4; " | 259 "HTTPS https.example.com:4; " |
236 "QUIC quic.example.com:65000; " | 260 "QUIC quic.example.com:65000; " |
237 "DIRECT"); | 261 "DIRECT"); |
238 request->CompleteNow(OK); | 262 request->CompleteNow(OK); |
239 client.WaitForResult(); | 263 client.WaitForResult(); |
240 | 264 |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
402 | 426 |
403 TEST_F(MojoProxyResolverImplTest, DestroyService) { | 427 TEST_F(MojoProxyResolverImplTest, DestroyService) { |
404 interfaces::ProxyResolverRequestClientPtr client_ptr; | 428 interfaces::ProxyResolverRequestClientPtr client_ptr; |
405 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 429 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
406 | 430 |
407 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); | 431 resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass()); |
408 resolver_->SetPacScript("pac script", base::Bind(&Fail)); | 432 resolver_->SetPacScript("pac script", base::Bind(&Fail)); |
409 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 433 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); |
410 scoped_refptr<MockAsyncProxyResolverBase::Request> request = | 434 scoped_refptr<MockAsyncProxyResolverBase::Request> request = |
411 mock_proxy_resolver_->pending_requests()[0]; | 435 mock_proxy_resolver_->pending_requests()[0]; |
412 resolver_.reset(); | 436 resolver_impl_.reset(); |
413 client.WaitForConnectionError(); | 437 client.WaitForConnectionError(); |
414 } | 438 } |
415 | 439 |
416 } // namespace net | 440 } // namespace net |
OLD | NEW |