Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: net/proxy/proxy_service_unittest.cc

Issue 160619: Remove dependency on SingleThreadedProxyResolver from resolve_proxy_msg_helper_unittest (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Address willchan's comments + sync Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/proxy/mock_proxy_resolver.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 <vector> 5 #include <vector>
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "googleurl/src/gurl.h" 9 #include "googleurl/src/gurl.h"
10 #include "net/base/net_errors.h" 10 #include "net/base/net_errors.h"
11 #include "net/base/test_completion_callback.h" 11 #include "net/base/test_completion_callback.h"
12 #include "net/proxy/mock_proxy_resolver.h"
12 #include "net/proxy/proxy_config_service.h" 13 #include "net/proxy/proxy_config_service.h"
13 #include "net/proxy/proxy_resolver.h" 14 #include "net/proxy/proxy_resolver.h"
14 #include "net/proxy/proxy_script_fetcher.h" 15 #include "net/proxy/proxy_script_fetcher.h"
15 #include "net/proxy/proxy_service.h" 16 #include "net/proxy/proxy_service.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 // TODO(eroman): Write a test which exercises 19 // TODO(eroman): Write a test which exercises
19 // ProxyService::SuspendAllPendingRequests(). 20 // ProxyService::SuspendAllPendingRequests().
20 namespace net { 21 namespace net {
21 namespace { 22 namespace {
22 23
23 class MockProxyConfigService: public ProxyConfigService { 24 class MockProxyConfigService: public ProxyConfigService {
24 public: 25 public:
25 MockProxyConfigService() {} // Direct connect. 26 MockProxyConfigService() {} // Direct connect.
26 explicit MockProxyConfigService(const ProxyConfig& pc) : config(pc) {} 27 explicit MockProxyConfigService(const ProxyConfig& pc) : config(pc) {}
27 explicit MockProxyConfigService(const std::string& pac_url) { 28 explicit MockProxyConfigService(const std::string& pac_url) {
28 config.pac_url = GURL(pac_url); 29 config.pac_url = GURL(pac_url);
29 } 30 }
30 31
31 virtual int GetProxyConfig(ProxyConfig* results) { 32 virtual int GetProxyConfig(ProxyConfig* results) {
32 *results = config; 33 *results = config;
33 return OK; 34 return OK;
34 } 35 }
35 36
36 ProxyConfig config; 37 ProxyConfig config;
37 }; 38 };
38 39
39 // Asynchronous mock proxy resolver. All requests complete asynchronously,
40 // user must call Request::CompleteNow() on a pending request to signal it.
41 class MockAsyncProxyResolverBase : public ProxyResolver {
42 public:
43 class Request : public base::RefCounted<Request> {
44 public:
45 Request(MockAsyncProxyResolverBase* resolver,
46 const GURL& url,
47 ProxyInfo* results,
48 CompletionCallback* callback)
49 : resolver_(resolver),
50 url_(url),
51 results_(results),
52 callback_(callback),
53 origin_loop_(MessageLoop::current()) {
54 }
55
56 const GURL& url() const { return url_; }
57 ProxyInfo* results() const { return results_; }
58 CompletionCallback* callback() const { return callback_; }
59
60 void CompleteNow(int rv) {
61 CompletionCallback* callback = callback_;
62
63 // May delete |this|.
64 resolver_->RemovePendingRequest(this);
65
66 callback->Run(rv);
67 }
68
69 private:
70 MockAsyncProxyResolverBase* resolver_;
71 const GURL url_;
72 ProxyInfo* results_;
73 CompletionCallback* callback_;
74 MessageLoop* origin_loop_;
75 };
76
77 class SetPacScriptRequest {
78 public:
79 SetPacScriptRequest(MockAsyncProxyResolverBase* resolver,
80 const GURL& pac_url,
81 const std::string& pac_bytes,
82 CompletionCallback* callback)
83 : resolver_(resolver),
84 pac_url_(pac_url),
85 pac_bytes_(pac_bytes),
86 callback_(callback),
87 origin_loop_(MessageLoop::current()) {
88 }
89
90 const GURL& pac_url() const { return pac_url_; }
91 const std::string& pac_bytes() const { return pac_bytes_; }
92
93 void CompleteNow(int rv) {
94 CompletionCallback* callback = callback_;
95
96 // Will delete |this|.
97 resolver_->RemovePendingSetPacScriptRequest(this);
98
99 callback->Run(rv);
100 }
101
102 private:
103 MockAsyncProxyResolverBase* resolver_;
104 const GURL pac_url_;
105 const std::string pac_bytes_;
106 CompletionCallback* callback_;
107 MessageLoop* origin_loop_;
108 };
109
110 typedef std::vector<scoped_refptr<Request> > RequestsList;
111
112 // ProxyResolver implementation:
113 virtual int GetProxyForURL(const GURL& url,
114 ProxyInfo* results,
115 CompletionCallback* callback,
116 RequestHandle* request_handle) {
117 scoped_refptr<Request> request = new Request(this, url, results, callback);
118 pending_requests_.push_back(request);
119
120 if (request_handle)
121 *request_handle = reinterpret_cast<RequestHandle>(request.get());
122
123 // Test code completes the request by calling request->CompleteNow().
124 return ERR_IO_PENDING;
125 }
126
127 virtual void CancelRequest(RequestHandle request_handle) {
128 scoped_refptr<Request> request = reinterpret_cast<Request*>(request_handle);
129 cancelled_requests_.push_back(request);
130 RemovePendingRequest(request);
131 }
132
133 virtual int SetPacScript(const GURL& pac_url,
134 const std::string& pac_bytes,
135 CompletionCallback* callback) {
136 EXPECT_EQ(NULL, pending_set_pac_script_request_.get());
137 pending_set_pac_script_request_.reset(
138 new SetPacScriptRequest(this, pac_url, pac_bytes, callback));
139 // Finished when user calls SetPacScriptRequest::CompleteNow().
140 return ERR_IO_PENDING;
141 }
142
143 const RequestsList& pending_requests() const {
144 return pending_requests_;
145 }
146
147 const RequestsList& cancelled_requests() const {
148 return cancelled_requests_;
149 }
150
151 SetPacScriptRequest* pending_set_pac_script_request() const {
152 return pending_set_pac_script_request_.get();
153 }
154
155 void RemovePendingRequest(Request* request) {
156 RequestsList::iterator it = std::find(
157 pending_requests_.begin(), pending_requests_.end(), request);
158 DCHECK(it != pending_requests_.end());
159 pending_requests_.erase(it);
160 }
161
162 void RemovePendingSetPacScriptRequest(SetPacScriptRequest* request) {
163 EXPECT_EQ(request, pending_set_pac_script_request());
164 pending_set_pac_script_request_.reset();
165 }
166
167 protected:
168 explicit MockAsyncProxyResolverBase(bool expects_pac_bytes)
169 : ProxyResolver(expects_pac_bytes) {}
170
171 private:
172 RequestsList pending_requests_;
173 RequestsList cancelled_requests_;
174 scoped_ptr<SetPacScriptRequest> pending_set_pac_script_request_;
175 };
176
177 class MockAsyncProxyResolver : public MockAsyncProxyResolverBase {
178 public:
179 MockAsyncProxyResolver()
180 : MockAsyncProxyResolverBase(false /*expects_pac_bytes*/) {}
181 };
182
183 class MockAsyncProxyResolverExpectsBytes : public MockAsyncProxyResolverBase {
184 public:
185 MockAsyncProxyResolverExpectsBytes()
186 : MockAsyncProxyResolverBase(true /*expects_pac_bytes*/) {}
187 };
188
189 } // namespace 40 } // namespace
190 41
191 // A mock ProxyScriptFetcher. No result will be returned to the fetch client 42 // A mock ProxyScriptFetcher. No result will be returned to the fetch client
192 // until we call NotifyFetchCompletion() to set the results. 43 // until we call NotifyFetchCompletion() to set the results.
193 class MockProxyScriptFetcher : public ProxyScriptFetcher { 44 class MockProxyScriptFetcher : public ProxyScriptFetcher {
194 public: 45 public:
195 MockProxyScriptFetcher() 46 MockProxyScriptFetcher()
196 : pending_request_callback_(NULL), pending_request_bytes_(NULL) { 47 : pending_request_callback_(NULL), pending_request_bytes_(NULL) {
197 } 48 }
198 49
(...skipping 1421 matching lines...) Expand 10 before | Expand all | Expand 10 after
1620 ProxyInfo info2; 1471 ProxyInfo info2;
1621 TestCompletionCallback callback2; 1472 TestCompletionCallback callback2;
1622 rv = service.ResolveProxy( 1473 rv = service.ResolveProxy(
1623 GURL("http://www.google.com"), &info2, &callback2, NULL); 1474 GURL("http://www.google.com"), &info2, &callback2, NULL);
1624 EXPECT_EQ(OK, rv); 1475 EXPECT_EQ(OK, rv);
1625 1476
1626 EXPECT_TRUE(info2.is_direct()); 1477 EXPECT_TRUE(info2.is_direct());
1627 } 1478 }
1628 1479
1629 } // namespace net 1480 } // namespace net
OLDNEW
« no previous file with comments | « net/proxy/mock_proxy_resolver.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698