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

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

Issue 160510: Better match IE's proxy settings.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix merge conflict 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/init_proxy_resolver.cc ('k') | net/proxy/proxy_resolver.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2009 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 <vector>
6
7 #include "net/base/net_errors.h"
8 #include "net/base/test_completion_callback.h"
9 #include "net/proxy/init_proxy_resolver.h"
10 #include "net/proxy/proxy_config.h"
11 #include "net/proxy/proxy_resolver.h"
12 #include "net/proxy/proxy_script_fetcher.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace net {
16 namespace {
17
18 enum Error {
19 kFailedDownloading = -100,
20 kFailedParsing = -200,
21 };
22
23 class Rules {
24 public:
25 struct Rule {
26 Rule(const GURL& url,
27 int fetch_error,
28 int set_pac_error)
29 : url(url),
30 fetch_error(fetch_error),
31 set_pac_error(set_pac_error) {
32 }
33
34 std::string bytes() const {
35 if (set_pac_error == OK)
36 return url.spec() + "!valid-script";
37 if (fetch_error == OK)
38 return url.spec() + "!invalid-script";
39 return std::string();
40 }
41
42 GURL url;
43 int fetch_error;
44 int set_pac_error;
45 };
46
47 Rule AddSuccessRule(const char* url) {
48 Rule rule(GURL(url), OK /*fetch_error*/, OK /*set_pac_error*/);
49 rules_.push_back(rule);
50 return rule;
51 }
52
53 void AddFailDownloadRule(const char* url) {
54 rules_.push_back(Rule(GURL(url), kFailedDownloading /*fetch_error*/,
55 ERR_UNEXPECTED /*set_pac_error*/));
56 }
57
58 void AddFailParsingRule(const char* url) {
59 rules_.push_back(Rule(GURL(url), OK /*fetch_error*/,
60 kFailedParsing /*set_pac_error*/));
61 }
62
63 const Rule& GetRuleByUrl(const GURL& url) const {
64 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end();
65 ++it) {
66 if (it->url == url)
67 return *it;
68 }
69 CHECK(false) << "Rule not found for " << url;
70 return rules_[0];
71 }
72
73 const Rule& GetRuleByBytes(const std::string& bytes) const {
74 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end();
75 ++it) {
76 if (it->bytes() == bytes)
77 return *it;
78 }
79 CHECK(false) << "Rule not found for " << bytes;
80 return rules_[0];
81 }
82
83 private:
84 typedef std::vector<Rule> RuleList;
85 RuleList rules_;
86 };
87
88 class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher {
89 public:
90 explicit RuleBasedProxyScriptFetcher(const Rules* rules) : rules_(rules) {}
91
92 // ProxyScriptFetcher implementation.
93 virtual int Fetch(const GURL& url,
94 std::string* bytes,
95 CompletionCallback* callback) {
96 const Rules::Rule& rule = rules_->GetRuleByUrl(url);
97 int rv = rule.fetch_error;
98 EXPECT_NE(ERR_UNEXPECTED, rv);
99 if (rv == OK)
100 *bytes = rule.bytes();
101 return rv;
102 }
103
104 virtual void Cancel() {}
105
106 private:
107 const Rules* rules_;
108 };
109
110 class RuleBasedProxyResolver : public ProxyResolver {
111 public:
112 RuleBasedProxyResolver(const Rules* rules, bool expects_pac_bytes)
113 : ProxyResolver(expects_pac_bytes), rules_(rules) {}
114
115 // ProxyResolver implementation:
116 virtual int GetProxyForURL(const GURL& /*url*/,
117 ProxyInfo* /*results*/,
118 CompletionCallback* /*callback*/,
119 RequestHandle* /*request_handle*/) {
120 NOTREACHED();
121 return ERR_UNEXPECTED;
122 }
123
124 virtual void CancelRequest(RequestHandle request_handle) {
125 NOTREACHED();
126 }
127
128 virtual int SetPacScript(const GURL& pac_url,
129 const std::string& pac_bytes,
130 CompletionCallback* callback) {
131 const Rules::Rule& rule = expects_pac_bytes() ?
132 rules_->GetRuleByBytes(pac_bytes) :
133 rules_->GetRuleByUrl(pac_url);
134
135 int rv = rule.set_pac_error;
136 EXPECT_NE(ERR_UNEXPECTED, rv);
137
138 if (expects_pac_bytes())
139 EXPECT_EQ(rule.bytes(), pac_bytes);
140 else
141 EXPECT_EQ(rule.url, pac_url);
142
143 if (rv == OK) {
144 pac_bytes_ = pac_bytes;
145 pac_url_ = pac_url;
146 }
147 return rv;
148 }
149
150 const std::string& pac_bytes() const { return pac_bytes_; }
151 const GURL& pac_url() const { return pac_url_; }
152
153 private:
154 const Rules* rules_;
155 std::string pac_bytes_;
156 GURL pac_url_;
157 };
158
159 // Succeed using custom PAC script.
160 TEST(InitProxyResolverTest, CustomPacSucceeds) {
161 Rules rules;
162 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
163 RuleBasedProxyScriptFetcher fetcher(&rules);
164
165 ProxyConfig config;
166 config.pac_url = GURL("http://custom/proxy.pac");
167
168 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
169
170 TestCompletionCallback callback;
171 InitProxyResolver init(&resolver, &fetcher);
172 EXPECT_EQ(OK, init.Init(config, &callback));
173 EXPECT_EQ(rule.bytes(), resolver.pac_bytes());
174 }
175
176 // Fail downloading the custom PAC script.
177 TEST(InitProxyResolverTest, CustomPacFails1) {
178 Rules rules;
179 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
180 RuleBasedProxyScriptFetcher fetcher(&rules);
181
182 ProxyConfig config;
183 config.pac_url = GURL("http://custom/proxy.pac");
184
185 rules.AddFailDownloadRule("http://custom/proxy.pac");
186
187 TestCompletionCallback callback;
188 InitProxyResolver init(&resolver, &fetcher);
189 EXPECT_EQ(kFailedDownloading, init.Init(config, &callback));
190 EXPECT_EQ("", resolver.pac_bytes());
191 }
192
193 // Fail parsing the custom PAC script.
194 TEST(InitProxyResolverTest, CustomPacFails2) {
195 Rules rules;
196 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
197 RuleBasedProxyScriptFetcher fetcher(&rules);
198
199 ProxyConfig config;
200 config.pac_url = GURL("http://custom/proxy.pac");
201
202 rules.AddFailParsingRule("http://custom/proxy.pac");
203
204 TestCompletionCallback callback;
205 InitProxyResolver init(&resolver, &fetcher);
206 EXPECT_EQ(kFailedParsing, init.Init(config, &callback));
207 EXPECT_EQ("", resolver.pac_bytes());
208 }
209
210 // Succeeds in choosing autodetect (wpad).
211 TEST(InitProxyResolverTest, AutodetectSuccess) {
212 Rules rules;
213 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
214 RuleBasedProxyScriptFetcher fetcher(&rules);
215
216 ProxyConfig config;
217 config.auto_detect = true;
218
219 Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat");
220
221 TestCompletionCallback callback;
222 InitProxyResolver init(&resolver, &fetcher);
223 EXPECT_EQ(OK, init.Init(config, &callback));
224 EXPECT_EQ(rule.bytes(), resolver.pac_bytes());
225 }
226
227 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC.
228 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess1) {
229 Rules rules;
230 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
231 RuleBasedProxyScriptFetcher fetcher(&rules);
232
233 ProxyConfig config;
234 config.auto_detect = true;
235 config.pac_url = GURL("http://custom/proxy.pac");
236
237 rules.AddFailDownloadRule("http://wpad/wpad.dat");
238 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
239
240 TestCompletionCallback callback;
241 InitProxyResolver init(&resolver, &fetcher);
242 EXPECT_EQ(OK, init.Init(config, &callback));
243 EXPECT_EQ(rule.bytes(), resolver.pac_bytes());
244 }
245
246 // Fails at WPAD (parsing), but succeeds in choosing the custom PAC.
247 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2) {
248 Rules rules;
249 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
250 RuleBasedProxyScriptFetcher fetcher(&rules);
251
252 ProxyConfig config;
253 config.auto_detect = true;
254 config.pac_url = GURL("http://custom/proxy.pac");
255
256 rules.AddFailParsingRule("http://wpad/wpad.dat");
257 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
258
259 TestCompletionCallback callback;
260 InitProxyResolver init(&resolver, &fetcher);
261 EXPECT_EQ(OK, init.Init(config, &callback));
262 EXPECT_EQ(rule.bytes(), resolver.pac_bytes());
263 }
264
265 // Fails at WPAD (downloading), and fails at custom PAC (downloading).
266 TEST(InitProxyResolverTest, AutodetectFailCustomFails1) {
267 Rules rules;
268 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
269 RuleBasedProxyScriptFetcher fetcher(&rules);
270
271 ProxyConfig config;
272 config.auto_detect = true;
273 config.pac_url = GURL("http://custom/proxy.pac");
274
275 rules.AddFailDownloadRule("http://wpad/wpad.dat");
276 rules.AddFailDownloadRule("http://custom/proxy.pac");
277
278 TestCompletionCallback callback;
279 InitProxyResolver init(&resolver, &fetcher);
280 EXPECT_EQ(kFailedDownloading, init.Init(config, &callback));
281 EXPECT_EQ("", resolver.pac_bytes());
282 }
283
284 // Fails at WPAD (downloading), and fails at custom PAC (parsing).
285 TEST(InitProxyResolverTest, AutodetectFailCustomFails2) {
286 Rules rules;
287 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
288 RuleBasedProxyScriptFetcher fetcher(&rules);
289
290 ProxyConfig config;
291 config.auto_detect = true;
292 config.pac_url = GURL("http://custom/proxy.pac");
293
294 rules.AddFailDownloadRule("http://wpad/wpad.dat");
295 rules.AddFailParsingRule("http://custom/proxy.pac");
296
297 TestCompletionCallback callback;
298 InitProxyResolver init(&resolver, &fetcher);
299 EXPECT_EQ(kFailedParsing, init.Init(config, &callback));
300 EXPECT_EQ("", resolver.pac_bytes());
301 }
302
303 // Fails at WPAD (parsing), but succeeds in choosing the custom PAC.
304 // This is the same as AutodetectFailCustomSuccess2, but using a ProxyResolver
305 // that doesn't |expects_pac_bytes|.
306 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2_NoFetch) {
307 Rules rules;
308 RuleBasedProxyResolver resolver(&rules, false /*expects_pac_bytes*/);
309 RuleBasedProxyScriptFetcher fetcher(&rules);
310
311 ProxyConfig config;
312 config.auto_detect = true;
313 config.pac_url = GURL("http://custom/proxy.pac");
314
315 rules.AddFailParsingRule(""); // Autodetect.
316 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
317
318 TestCompletionCallback callback;
319 InitProxyResolver init(&resolver, &fetcher);
320 EXPECT_EQ(OK, init.Init(config, &callback));
321 EXPECT_EQ(rule.url, resolver.pac_url());
322 }
323
324 } // namespace
325 } // namespace net
OLDNEW
« no previous file with comments | « net/proxy/init_proxy_resolver.cc ('k') | net/proxy/proxy_resolver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698