OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/proxy/proxy_resolver_factory.h" | |
6 | |
7 #include "net/base/net_errors.h" | |
8 #include "net/base/test_completion_callback.h" | |
9 #include "net/proxy/mock_proxy_resolver.h" | |
10 #include "net/proxy/proxy_resolver.h" | |
11 #include "net/proxy/proxy_resolver_script_data.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace net { | |
15 namespace { | |
16 | |
17 void Fail(int error) { | |
18 FAIL() << "Unexpected callback called"; | |
19 } | |
20 | |
21 class TestProxyResolver : public MockAsyncProxyResolver { | |
22 public: | |
23 int SetPacScript(const scoped_refptr<ProxyResolverScriptData>& script_data, | |
24 const CompletionCallback& callback) override { | |
25 int result = MockAsyncProxyResolver::SetPacScript(script_data, callback); | |
26 if (synchronous_) | |
27 return error_to_return_; | |
28 | |
29 return result; | |
30 } | |
31 | |
32 void set_error_to_return(Error error) { | |
33 synchronous_ = true; | |
34 error_to_return_ = error; | |
35 } | |
36 | |
37 private: | |
38 bool synchronous_ = false; | |
39 Error error_to_return_ = OK; | |
40 }; | |
41 | |
42 class TestLegacyProxyResolverFactory : public LegacyProxyResolverFactory { | |
43 public: | |
44 using ProxyResolverFactory::CreateProxyResolver; | |
45 | |
46 explicit TestLegacyProxyResolverFactory(ProxyResolver* resolver) | |
47 : LegacyProxyResolverFactory(false), resolver_(resolver) {} | |
48 | |
49 // LegacyProxyResolverFactory override. | |
50 scoped_ptr<ProxyResolver> CreateProxyResolver() override { | |
51 return make_scoped_ptr(new ForwardingProxyResolver(resolver_)); | |
52 } | |
53 | |
54 private: | |
55 ProxyResolver* resolver_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(TestLegacyProxyResolverFactory); | |
58 }; | |
59 | |
60 } // namespace | |
61 | |
62 class LegacyProxyResolverFactoryTest : public testing::Test { | |
63 public: | |
64 void SetUp() override { | |
65 factory_.reset(new TestLegacyProxyResolverFactory(&resolver_)); | |
66 } | |
67 | |
68 ProxyResolverFactory& factory() { return *factory_; } | |
69 TestProxyResolver& mock_resolver() { return resolver_; } | |
70 | |
71 private: | |
72 TestProxyResolver resolver_; | |
73 scoped_ptr<TestLegacyProxyResolverFactory> factory_; | |
74 }; | |
75 | |
76 TEST_F(LegacyProxyResolverFactoryTest, Async_Success) { | |
77 const GURL url("http://proxy"); | |
78 TestCompletionCallback callback; | |
79 scoped_ptr<ProxyResolver> resolver; | |
80 scoped_ptr<ProxyResolverFactory::Request> request; | |
81 EXPECT_EQ(ERR_IO_PENDING, factory().CreateProxyResolver( | |
82 ProxyResolverScriptData::FromURL(url), | |
83 &resolver, callback.callback(), &request)); | |
84 ASSERT_TRUE(mock_resolver().has_pending_set_pac_script_request()); | |
85 EXPECT_EQ( | |
86 url, | |
87 mock_resolver().pending_set_pac_script_request()->script_data()->url()); | |
88 mock_resolver().pending_set_pac_script_request()->CompleteNow(OK); | |
89 EXPECT_EQ(OK, callback.WaitForResult()); | |
90 EXPECT_TRUE(resolver); | |
91 } | |
92 | |
93 TEST_F(LegacyProxyResolverFactoryTest, Async_Error) { | |
94 const GURL url("http://proxy"); | |
95 TestCompletionCallback callback; | |
96 scoped_ptr<ProxyResolver> resolver; | |
97 scoped_ptr<ProxyResolverFactory::Request> request; | |
98 EXPECT_EQ(ERR_IO_PENDING, factory().CreateProxyResolver( | |
99 ProxyResolverScriptData::FromURL(url), | |
100 &resolver, callback.callback(), &request)); | |
101 ASSERT_TRUE(mock_resolver().has_pending_set_pac_script_request()); | |
102 EXPECT_EQ( | |
103 url, | |
104 mock_resolver().pending_set_pac_script_request()->script_data()->url()); | |
105 mock_resolver().pending_set_pac_script_request()->CompleteNow(ERR_FAILED); | |
106 EXPECT_EQ(ERR_FAILED, callback.WaitForResult()); | |
107 EXPECT_FALSE(resolver); | |
108 } | |
109 | |
110 TEST_F(LegacyProxyResolverFactoryTest, Async_Cancel) { | |
111 const GURL url("http://proxy"); | |
112 scoped_ptr<ProxyResolver> resolver; | |
113 scoped_ptr<ProxyResolverFactory::Request> request; | |
114 EXPECT_EQ(ERR_IO_PENDING, factory().CreateProxyResolver( | |
115 ProxyResolverScriptData::FromURL(url), | |
116 &resolver, base::Bind(&Fail), &request)); | |
117 ASSERT_TRUE(mock_resolver().has_pending_set_pac_script_request()); | |
118 EXPECT_EQ( | |
119 url, | |
120 mock_resolver().pending_set_pac_script_request()->script_data()->url()); | |
121 request.reset(); | |
122 EXPECT_FALSE(resolver); | |
123 } | |
124 | |
125 TEST_F(LegacyProxyResolverFactoryTest, Async_DeleteFactory) { | |
126 const GURL url("http://proxy"); | |
127 scoped_ptr<ProxyResolver> resolver; | |
128 scoped_ptr<ProxyResolverFactory::Request> request; | |
129 { | |
130 TestProxyResolver test_resolver; | |
131 TestLegacyProxyResolverFactory factory(&test_resolver); | |
132 EXPECT_EQ(ERR_IO_PENDING, factory.CreateProxyResolver( | |
133 ProxyResolverScriptData::FromURL(url), | |
134 &resolver, base::Bind(&Fail), &request)); | |
135 ASSERT_TRUE(test_resolver.has_pending_set_pac_script_request()); | |
136 EXPECT_EQ( | |
137 url, | |
138 test_resolver.pending_set_pac_script_request()->script_data()->url()); | |
139 } | |
140 EXPECT_FALSE(resolver); | |
141 } | |
142 | |
143 TEST_F(LegacyProxyResolverFactoryTest, Sync_Success) { | |
144 const GURL url("http://proxy"); | |
145 TestCompletionCallback callback; | |
146 scoped_ptr<ProxyResolver> resolver; | |
147 scoped_ptr<ProxyResolverFactory::Request> request; | |
148 mock_resolver().set_error_to_return(OK); | |
149 EXPECT_EQ(OK, factory().CreateProxyResolver( | |
150 ProxyResolverScriptData::FromURL(url), &resolver, | |
151 callback.callback(), &request)); | |
152 ASSERT_TRUE(mock_resolver().has_pending_set_pac_script_request()); | |
153 EXPECT_EQ( | |
154 url, | |
155 mock_resolver().pending_set_pac_script_request()->script_data()->url()); | |
156 EXPECT_TRUE(resolver); | |
157 } | |
158 | |
159 TEST_F(LegacyProxyResolverFactoryTest, Sync_Error) { | |
160 const GURL url("http://proxy"); | |
161 TestCompletionCallback callback; | |
162 scoped_ptr<ProxyResolver> resolver; | |
163 scoped_ptr<ProxyResolverFactory::Request> request; | |
164 mock_resolver().set_error_to_return(ERR_FAILED); | |
165 EXPECT_EQ(ERR_FAILED, factory().CreateProxyResolver( | |
166 ProxyResolverScriptData::FromURL(url), &resolver, | |
167 callback.callback(), &request)); | |
168 ASSERT_TRUE(mock_resolver().has_pending_set_pac_script_request()); | |
169 EXPECT_EQ( | |
170 url, | |
171 mock_resolver().pending_set_pac_script_request()->script_data()->url()); | |
172 EXPECT_FALSE(resolver); | |
173 } | |
174 | |
175 } // namespace net | |
OLD | NEW |