Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / proxy / proxy_resolver_factory_unittest.cc
blob6e9708b5299018d82c73cebe3d63510a090216fc
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.h"
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"
14 namespace net {
15 namespace {
17 void Fail(int error) {
18 FAIL() << "Unexpected callback called";
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_;
29 return result;
32 void set_error_to_return(Error error) {
33 synchronous_ = true;
34 error_to_return_ = error;
37 private:
38 bool synchronous_ = false;
39 Error error_to_return_ = OK;
42 class TestLegacyProxyResolverFactory : public LegacyProxyResolverFactory {
43 public:
44 TestLegacyProxyResolverFactory() : LegacyProxyResolverFactory(false) {}
46 // LegacyProxyResolverFactory override.
47 scoped_ptr<ProxyResolver> CreateProxyResolver() override {
48 return make_scoped_ptr(new ForwardingProxyResolver(&resolver_));
51 TestProxyResolver& resolver() { return resolver_; }
53 private:
54 TestProxyResolver resolver_;
56 DISALLOW_COPY_AND_ASSIGN(TestLegacyProxyResolverFactory);
59 } // namespace
61 class LegacyProxyResolverFactoryTest : public testing::Test {
62 public:
63 ProxyResolverFactory& factory() { return factory_; }
64 TestProxyResolver& mock_resolver() { return factory_.resolver(); }
66 private:
67 TestLegacyProxyResolverFactory factory_;
70 TEST_F(LegacyProxyResolverFactoryTest, Async_Success) {
71 const GURL url("http://proxy");
72 TestCompletionCallback callback;
73 scoped_ptr<ProxyResolver> resolver;
74 scoped_ptr<ProxyResolverFactory::Request> request;
75 EXPECT_EQ(ERR_IO_PENDING, factory().CreateProxyResolver(
76 ProxyResolverScriptData::FromURL(url),
77 &resolver, callback.callback(), &request));
78 ASSERT_TRUE(mock_resolver().has_pending_set_pac_script_request());
79 EXPECT_EQ(
80 url,
81 mock_resolver().pending_set_pac_script_request()->script_data()->url());
82 mock_resolver().pending_set_pac_script_request()->CompleteNow(OK);
83 EXPECT_EQ(OK, callback.WaitForResult());
84 EXPECT_TRUE(resolver);
87 TEST_F(LegacyProxyResolverFactoryTest, Async_Error) {
88 const GURL url("http://proxy");
89 TestCompletionCallback callback;
90 scoped_ptr<ProxyResolver> resolver;
91 scoped_ptr<ProxyResolverFactory::Request> request;
92 EXPECT_EQ(ERR_IO_PENDING, factory().CreateProxyResolver(
93 ProxyResolverScriptData::FromURL(url),
94 &resolver, callback.callback(), &request));
95 ASSERT_TRUE(mock_resolver().has_pending_set_pac_script_request());
96 EXPECT_EQ(
97 url,
98 mock_resolver().pending_set_pac_script_request()->script_data()->url());
99 mock_resolver().pending_set_pac_script_request()->CompleteNow(ERR_FAILED);
100 EXPECT_EQ(ERR_FAILED, callback.WaitForResult());
101 EXPECT_FALSE(resolver);
104 TEST_F(LegacyProxyResolverFactoryTest, Async_Cancel) {
105 const GURL url("http://proxy");
106 scoped_ptr<ProxyResolver> resolver;
107 scoped_ptr<ProxyResolverFactory::Request> request;
108 EXPECT_EQ(ERR_IO_PENDING, factory().CreateProxyResolver(
109 ProxyResolverScriptData::FromURL(url),
110 &resolver, base::Bind(&Fail), &request));
111 ASSERT_TRUE(mock_resolver().has_pending_set_pac_script_request());
112 EXPECT_EQ(
113 url,
114 mock_resolver().pending_set_pac_script_request()->script_data()->url());
115 request.reset();
116 EXPECT_FALSE(resolver);
119 TEST_F(LegacyProxyResolverFactoryTest, Sync_Success) {
120 const GURL url("http://proxy");
121 TestCompletionCallback callback;
122 scoped_ptr<ProxyResolver> resolver;
123 scoped_ptr<ProxyResolverFactory::Request> request;
124 mock_resolver().set_error_to_return(OK);
125 EXPECT_EQ(OK, factory().CreateProxyResolver(
126 ProxyResolverScriptData::FromURL(url), &resolver,
127 callback.callback(), &request));
128 ASSERT_TRUE(mock_resolver().has_pending_set_pac_script_request());
129 EXPECT_EQ(
130 url,
131 mock_resolver().pending_set_pac_script_request()->script_data()->url());
132 EXPECT_TRUE(resolver);
135 TEST_F(LegacyProxyResolverFactoryTest, Sync_Error) {
136 const GURL url("http://proxy");
137 TestCompletionCallback callback;
138 scoped_ptr<ProxyResolver> resolver;
139 scoped_ptr<ProxyResolverFactory::Request> request;
140 mock_resolver().set_error_to_return(ERR_FAILED);
141 EXPECT_EQ(ERR_FAILED, factory().CreateProxyResolver(
142 ProxyResolverScriptData::FromURL(url), &resolver,
143 callback.callback(), &request));
144 ASSERT_TRUE(mock_resolver().has_pending_set_pac_script_request());
145 EXPECT_EQ(
146 url,
147 mock_resolver().pending_set_pac_script_request()->script_data()->url());
148 EXPECT_FALSE(resolver);
151 } // namespace net