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"
17 void Fail(int error
) {
18 FAIL() << "Unexpected callback called";
21 class TestProxyResolver
: public MockAsyncProxyResolver
{
23 int SetPacScript(const scoped_refptr
<ProxyResolverScriptData
>& script_data
,
24 const CompletionCallback
& callback
) override
{
25 int result
= MockAsyncProxyResolver::SetPacScript(script_data
, callback
);
27 return error_to_return_
;
32 void set_error_to_return(Error error
) {
34 error_to_return_
= error
;
38 bool synchronous_
= false;
39 Error error_to_return_
= OK
;
42 class TestLegacyProxyResolverFactory
: public LegacyProxyResolverFactory
{
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_
; }
54 TestProxyResolver resolver_
;
56 DISALLOW_COPY_AND_ASSIGN(TestLegacyProxyResolverFactory
);
61 class LegacyProxyResolverFactoryTest
: public testing::Test
{
63 ProxyResolverFactory
& factory() { return factory_
; }
64 TestProxyResolver
& mock_resolver() { return factory_
.resolver(); }
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());
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());
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());
114 mock_resolver().pending_set_pac_script_request()->script_data()->url());
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());
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());
147 mock_resolver().pending_set_pac_script_request()->script_data()->url());
148 EXPECT_FALSE(resolver
);