| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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/string_util.h" |
| 8 #include "base/utf_string_conversions.h" |
| 7 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 8 #include "net/base/net_log.h" | 10 #include "net/base/net_log.h" |
| 9 #include "net/base/net_log_unittest.h" | 11 #include "net/base/net_log_unittest.h" |
| 10 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
| 11 #include "net/proxy/init_proxy_resolver.h" | 13 #include "net/proxy/init_proxy_resolver.h" |
| 12 #include "net/proxy/proxy_config.h" | 14 #include "net/proxy/proxy_config.h" |
| 13 #include "net/proxy/proxy_resolver.h" | 15 #include "net/proxy/proxy_resolver.h" |
| 14 #include "net/proxy/proxy_script_fetcher.h" | 16 #include "net/proxy/proxy_script_fetcher.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| 17 namespace net { | 19 namespace net { |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 enum Error { | 22 enum Error { |
| 21 kFailedDownloading = -100, | 23 kFailedDownloading = -100, |
| 22 kFailedParsing = -200, | 24 kFailedParsing = -200, |
| 23 }; | 25 }; |
| 24 | 26 |
| 25 class Rules { | 27 class Rules { |
| 26 public: | 28 public: |
| 27 struct Rule { | 29 struct Rule { |
| 28 Rule(const GURL& url, | 30 Rule(const GURL& url, |
| 29 int fetch_error, | 31 int fetch_error, |
| 30 int set_pac_error) | 32 int set_pac_error) |
| 31 : url(url), | 33 : url(url), |
| 32 fetch_error(fetch_error), | 34 fetch_error(fetch_error), |
| 33 set_pac_error(set_pac_error) { | 35 set_pac_error(set_pac_error) { |
| 34 } | 36 } |
| 35 | 37 |
| 36 std::string bytes() const { | 38 string16 text() const { |
| 37 if (set_pac_error == OK) | 39 if (set_pac_error == OK) |
| 38 return url.spec() + "!valid-script"; | 40 return UTF8ToUTF16(url.spec() + "!valid-script"); |
| 39 if (fetch_error == OK) | 41 if (fetch_error == OK) |
| 40 return url.spec() + "!invalid-script"; | 42 return UTF8ToUTF16(url.spec() + "!invalid-script"); |
| 41 return std::string(); | 43 return string16(); |
| 42 } | 44 } |
| 43 | 45 |
| 44 GURL url; | 46 GURL url; |
| 45 int fetch_error; | 47 int fetch_error; |
| 46 int set_pac_error; | 48 int set_pac_error; |
| 47 }; | 49 }; |
| 48 | 50 |
| 49 Rule AddSuccessRule(const char* url) { | 51 Rule AddSuccessRule(const char* url) { |
| 50 Rule rule(GURL(url), OK /*fetch_error*/, OK /*set_pac_error*/); | 52 Rule rule(GURL(url), OK /*fetch_error*/, OK /*set_pac_error*/); |
| 51 rules_.push_back(rule); | 53 rules_.push_back(rule); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 65 const Rule& GetRuleByUrl(const GURL& url) const { | 67 const Rule& GetRuleByUrl(const GURL& url) const { |
| 66 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end(); | 68 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end(); |
| 67 ++it) { | 69 ++it) { |
| 68 if (it->url == url) | 70 if (it->url == url) |
| 69 return *it; | 71 return *it; |
| 70 } | 72 } |
| 71 LOG(FATAL) << "Rule not found for " << url; | 73 LOG(FATAL) << "Rule not found for " << url; |
| 72 return rules_[0]; | 74 return rules_[0]; |
| 73 } | 75 } |
| 74 | 76 |
| 75 const Rule& GetRuleByBytes(const std::string& bytes) const { | 77 const Rule& GetRuleByText(const string16& text) const { |
| 76 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end(); | 78 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end(); |
| 77 ++it) { | 79 ++it) { |
| 78 if (it->bytes() == bytes) | 80 if (it->text() == text) |
| 79 return *it; | 81 return *it; |
| 80 } | 82 } |
| 81 LOG(FATAL) << "Rule not found for " << bytes; | 83 LOG(FATAL) << "Rule not found for " << text; |
| 82 return rules_[0]; | 84 return rules_[0]; |
| 83 } | 85 } |
| 84 | 86 |
| 85 private: | 87 private: |
| 86 typedef std::vector<Rule> RuleList; | 88 typedef std::vector<Rule> RuleList; |
| 87 RuleList rules_; | 89 RuleList rules_; |
| 88 }; | 90 }; |
| 89 | 91 |
| 90 class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher { | 92 class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher { |
| 91 public: | 93 public: |
| 92 explicit RuleBasedProxyScriptFetcher(const Rules* rules) : rules_(rules) {} | 94 explicit RuleBasedProxyScriptFetcher(const Rules* rules) : rules_(rules) {} |
| 93 | 95 |
| 94 // ProxyScriptFetcher implementation. | 96 // ProxyScriptFetcher implementation. |
| 95 virtual int Fetch(const GURL& url, | 97 virtual int Fetch(const GURL& url, |
| 96 std::string* bytes, | 98 string16* text, |
| 97 CompletionCallback* callback) { | 99 CompletionCallback* callback) { |
| 98 const Rules::Rule& rule = rules_->GetRuleByUrl(url); | 100 const Rules::Rule& rule = rules_->GetRuleByUrl(url); |
| 99 int rv = rule.fetch_error; | 101 int rv = rule.fetch_error; |
| 100 EXPECT_NE(ERR_UNEXPECTED, rv); | 102 EXPECT_NE(ERR_UNEXPECTED, rv); |
| 101 if (rv == OK) | 103 if (rv == OK) |
| 102 *bytes = rule.bytes(); | 104 *text = rule.text(); |
| 103 return rv; | 105 return rv; |
| 104 } | 106 } |
| 105 | 107 |
| 106 virtual void Cancel() {} | 108 virtual void Cancel() {} |
| 107 | 109 |
| 108 private: | 110 private: |
| 109 const Rules* rules_; | 111 const Rules* rules_; |
| 110 }; | 112 }; |
| 111 | 113 |
| 112 class RuleBasedProxyResolver : public ProxyResolver { | 114 class RuleBasedProxyResolver : public ProxyResolver { |
| 113 public: | 115 public: |
| 114 RuleBasedProxyResolver(const Rules* rules, bool expects_pac_bytes) | 116 RuleBasedProxyResolver(const Rules* rules, bool expects_pac_bytes) |
| 115 : ProxyResolver(expects_pac_bytes), rules_(rules) {} | 117 : ProxyResolver(expects_pac_bytes), rules_(rules) {} |
| 116 | 118 |
| 117 // ProxyResolver implementation: | 119 // ProxyResolver implementation: |
| 118 virtual int GetProxyForURL(const GURL& /*url*/, | 120 virtual int GetProxyForURL(const GURL& /*url*/, |
| 119 ProxyInfo* /*results*/, | 121 ProxyInfo* /*results*/, |
| 120 CompletionCallback* /*callback*/, | 122 CompletionCallback* /*callback*/, |
| 121 RequestHandle* /*request_handle*/, | 123 RequestHandle* /*request_handle*/, |
| 122 const BoundNetLog& /*net_log*/) { | 124 const BoundNetLog& /*net_log*/) { |
| 123 NOTREACHED(); | 125 NOTREACHED(); |
| 124 return ERR_UNEXPECTED; | 126 return ERR_UNEXPECTED; |
| 125 } | 127 } |
| 126 | 128 |
| 127 virtual void CancelRequest(RequestHandle request_handle) { | 129 virtual void CancelRequest(RequestHandle request_handle) { |
| 128 NOTREACHED(); | 130 NOTREACHED(); |
| 129 } | 131 } |
| 130 | 132 |
| 131 virtual int SetPacScript(const GURL& pac_url, | 133 virtual int SetPacScript(const GURL& pac_url, |
| 132 const std::string& pac_bytes, | 134 const string16& pac_script, |
| 133 CompletionCallback* callback) { | 135 CompletionCallback* callback) { |
| 134 const Rules::Rule& rule = expects_pac_bytes() ? | 136 const Rules::Rule& rule = expects_pac_bytes() ? |
| 135 rules_->GetRuleByBytes(pac_bytes) : | 137 rules_->GetRuleByText(pac_script) : |
| 136 rules_->GetRuleByUrl(pac_url); | 138 rules_->GetRuleByUrl(pac_url); |
| 137 | 139 |
| 138 int rv = rule.set_pac_error; | 140 int rv = rule.set_pac_error; |
| 139 EXPECT_NE(ERR_UNEXPECTED, rv); | 141 EXPECT_NE(ERR_UNEXPECTED, rv); |
| 140 | 142 |
| 141 if (expects_pac_bytes()) | 143 if (expects_pac_bytes()) |
| 142 EXPECT_EQ(rule.bytes(), pac_bytes); | 144 EXPECT_EQ(rule.text(), pac_script); |
| 143 else | 145 else |
| 144 EXPECT_EQ(rule.url, pac_url); | 146 EXPECT_EQ(rule.url, pac_url); |
| 145 | 147 |
| 146 if (rv == OK) { | 148 if (rv == OK) { |
| 147 pac_bytes_ = pac_bytes; | 149 pac_script_ = pac_script; |
| 148 pac_url_ = pac_url; | 150 pac_url_ = pac_url; |
| 149 } | 151 } |
| 150 return rv; | 152 return rv; |
| 151 } | 153 } |
| 152 | 154 |
| 153 const std::string& pac_bytes() const { return pac_bytes_; } | 155 const string16& pac_script() const { return pac_script_; } |
| 154 const GURL& pac_url() const { return pac_url_; } | 156 const GURL& pac_url() const { return pac_url_; } |
| 155 | 157 |
| 156 private: | 158 private: |
| 157 const Rules* rules_; | 159 const Rules* rules_; |
| 158 std::string pac_bytes_; | 160 string16 pac_script_; |
| 159 GURL pac_url_; | 161 GURL pac_url_; |
| 160 }; | 162 }; |
| 161 | 163 |
| 162 // Succeed using custom PAC script. | 164 // Succeed using custom PAC script. |
| 163 TEST(InitProxyResolverTest, CustomPacSucceeds) { | 165 TEST(InitProxyResolverTest, CustomPacSucceeds) { |
| 164 Rules rules; | 166 Rules rules; |
| 165 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 167 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 166 RuleBasedProxyScriptFetcher fetcher(&rules); | 168 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 167 | 169 |
| 168 ProxyConfig config; | 170 ProxyConfig config; |
| 169 config.set_pac_url(GURL("http://custom/proxy.pac")); | 171 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 170 | 172 |
| 171 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 173 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 172 | 174 |
| 173 TestCompletionCallback callback; | 175 TestCompletionCallback callback; |
| 174 CapturingNetLog log(CapturingNetLog::kUnbounded); | 176 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 175 InitProxyResolver init(&resolver, &fetcher, &log); | 177 InitProxyResolver init(&resolver, &fetcher, &log); |
| 176 EXPECT_EQ(OK, init.Init(config, &callback)); | 178 EXPECT_EQ(OK, init.Init(config, &callback)); |
| 177 EXPECT_EQ(rule.bytes(), resolver.pac_bytes()); | 179 EXPECT_EQ(rule.text(), resolver.pac_script()); |
| 178 | 180 |
| 179 // Check the NetLog was filled correctly. | 181 // Check the NetLog was filled correctly. |
| 180 EXPECT_EQ(6u, log.entries().size()); | 182 EXPECT_EQ(6u, log.entries().size()); |
| 181 EXPECT_TRUE(LogContainsBeginEvent( | 183 EXPECT_TRUE(LogContainsBeginEvent( |
| 182 log.entries(), 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 184 log.entries(), 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 183 EXPECT_TRUE(LogContainsBeginEvent( | 185 EXPECT_TRUE(LogContainsBeginEvent( |
| 184 log.entries(), 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 186 log.entries(), 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 185 EXPECT_TRUE(LogContainsEndEvent( | 187 EXPECT_TRUE(LogContainsEndEvent( |
| 186 log.entries(), 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 188 log.entries(), 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 187 EXPECT_TRUE(LogContainsBeginEvent( | 189 EXPECT_TRUE(LogContainsBeginEvent( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 200 | 202 |
| 201 ProxyConfig config; | 203 ProxyConfig config; |
| 202 config.set_pac_url(GURL("http://custom/proxy.pac")); | 204 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 203 | 205 |
| 204 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 206 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 205 | 207 |
| 206 TestCompletionCallback callback; | 208 TestCompletionCallback callback; |
| 207 CapturingNetLog log(CapturingNetLog::kUnbounded); | 209 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 208 InitProxyResolver init(&resolver, &fetcher, &log); | 210 InitProxyResolver init(&resolver, &fetcher, &log); |
| 209 EXPECT_EQ(kFailedDownloading, init.Init(config, &callback)); | 211 EXPECT_EQ(kFailedDownloading, init.Init(config, &callback)); |
| 210 EXPECT_EQ("", resolver.pac_bytes()); | 212 EXPECT_EQ(string16(), resolver.pac_script()); |
| 211 | 213 |
| 212 // Check the NetLog was filled correctly. | 214 // Check the NetLog was filled correctly. |
| 213 EXPECT_EQ(4u, log.entries().size()); | 215 EXPECT_EQ(4u, log.entries().size()); |
| 214 EXPECT_TRUE(LogContainsBeginEvent( | 216 EXPECT_TRUE(LogContainsBeginEvent( |
| 215 log.entries(), 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 217 log.entries(), 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 216 EXPECT_TRUE(LogContainsBeginEvent( | 218 EXPECT_TRUE(LogContainsBeginEvent( |
| 217 log.entries(), 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 219 log.entries(), 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 218 EXPECT_TRUE(LogContainsEndEvent( | 220 EXPECT_TRUE(LogContainsEndEvent( |
| 219 log.entries(), 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 221 log.entries(), 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 220 EXPECT_TRUE(LogContainsEndEvent( | 222 EXPECT_TRUE(LogContainsEndEvent( |
| 221 log.entries(), 3, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 223 log.entries(), 3, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 222 } | 224 } |
| 223 | 225 |
| 224 // Fail parsing the custom PAC script. | 226 // Fail parsing the custom PAC script. |
| 225 TEST(InitProxyResolverTest, CustomPacFails2) { | 227 TEST(InitProxyResolverTest, CustomPacFails2) { |
| 226 Rules rules; | 228 Rules rules; |
| 227 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 229 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 228 RuleBasedProxyScriptFetcher fetcher(&rules); | 230 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 229 | 231 |
| 230 ProxyConfig config; | 232 ProxyConfig config; |
| 231 config.set_pac_url(GURL("http://custom/proxy.pac")); | 233 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 232 | 234 |
| 233 rules.AddFailParsingRule("http://custom/proxy.pac"); | 235 rules.AddFailParsingRule("http://custom/proxy.pac"); |
| 234 | 236 |
| 235 TestCompletionCallback callback; | 237 TestCompletionCallback callback; |
| 236 InitProxyResolver init(&resolver, &fetcher, NULL); | 238 InitProxyResolver init(&resolver, &fetcher, NULL); |
| 237 EXPECT_EQ(kFailedParsing, init.Init(config, &callback)); | 239 EXPECT_EQ(kFailedParsing, init.Init(config, &callback)); |
| 238 EXPECT_EQ("", resolver.pac_bytes()); | 240 EXPECT_EQ(string16(), resolver.pac_script()); |
| 239 } | 241 } |
| 240 | 242 |
| 241 // Fail downloading the custom PAC script, because the fetcher was NULL. | 243 // Fail downloading the custom PAC script, because the fetcher was NULL. |
| 242 TEST(InitProxyResolverTest, HasNullProxyScriptFetcher) { | 244 TEST(InitProxyResolverTest, HasNullProxyScriptFetcher) { |
| 243 Rules rules; | 245 Rules rules; |
| 244 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 246 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 245 | 247 |
| 246 ProxyConfig config; | 248 ProxyConfig config; |
| 247 config.set_pac_url(GURL("http://custom/proxy.pac")); | 249 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 248 | 250 |
| 249 TestCompletionCallback callback; | 251 TestCompletionCallback callback; |
| 250 InitProxyResolver init(&resolver, NULL, NULL); | 252 InitProxyResolver init(&resolver, NULL, NULL); |
| 251 EXPECT_EQ(ERR_UNEXPECTED, init.Init(config, &callback)); | 253 EXPECT_EQ(ERR_UNEXPECTED, init.Init(config, &callback)); |
| 252 EXPECT_EQ("", resolver.pac_bytes()); | 254 EXPECT_EQ(string16(), resolver.pac_script()); |
| 253 } | 255 } |
| 254 | 256 |
| 255 // Succeeds in choosing autodetect (wpad). | 257 // Succeeds in choosing autodetect (wpad). |
| 256 TEST(InitProxyResolverTest, AutodetectSuccess) { | 258 TEST(InitProxyResolverTest, AutodetectSuccess) { |
| 257 Rules rules; | 259 Rules rules; |
| 258 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 260 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 259 RuleBasedProxyScriptFetcher fetcher(&rules); | 261 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 260 | 262 |
| 261 ProxyConfig config; | 263 ProxyConfig config; |
| 262 config.set_auto_detect(true); | 264 config.set_auto_detect(true); |
| 263 | 265 |
| 264 Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat"); | 266 Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat"); |
| 265 | 267 |
| 266 TestCompletionCallback callback; | 268 TestCompletionCallback callback; |
| 267 InitProxyResolver init(&resolver, &fetcher, NULL); | 269 InitProxyResolver init(&resolver, &fetcher, NULL); |
| 268 EXPECT_EQ(OK, init.Init(config, &callback)); | 270 EXPECT_EQ(OK, init.Init(config, &callback)); |
| 269 EXPECT_EQ(rule.bytes(), resolver.pac_bytes()); | 271 EXPECT_EQ(rule.text(), resolver.pac_script()); |
| 270 } | 272 } |
| 271 | 273 |
| 272 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. | 274 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. |
| 273 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess1) { | 275 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess1) { |
| 274 Rules rules; | 276 Rules rules; |
| 275 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 277 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 276 RuleBasedProxyScriptFetcher fetcher(&rules); | 278 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 277 | 279 |
| 278 ProxyConfig config; | 280 ProxyConfig config; |
| 279 config.set_auto_detect(true); | 281 config.set_auto_detect(true); |
| 280 config.set_pac_url(GURL("http://custom/proxy.pac")); | 282 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 281 | 283 |
| 282 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 284 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 283 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 285 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 284 | 286 |
| 285 TestCompletionCallback callback; | 287 TestCompletionCallback callback; |
| 286 InitProxyResolver init(&resolver, &fetcher, NULL); | 288 InitProxyResolver init(&resolver, &fetcher, NULL); |
| 287 EXPECT_EQ(OK, init.Init(config, &callback)); | 289 EXPECT_EQ(OK, init.Init(config, &callback)); |
| 288 EXPECT_EQ(rule.bytes(), resolver.pac_bytes()); | 290 EXPECT_EQ(rule.text(), resolver.pac_script()); |
| 289 } | 291 } |
| 290 | 292 |
| 291 // Fails at WPAD (parsing), but succeeds in choosing the custom PAC. | 293 // Fails at WPAD (parsing), but succeeds in choosing the custom PAC. |
| 292 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2) { | 294 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2) { |
| 293 Rules rules; | 295 Rules rules; |
| 294 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 296 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 295 RuleBasedProxyScriptFetcher fetcher(&rules); | 297 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 296 | 298 |
| 297 ProxyConfig config; | 299 ProxyConfig config; |
| 298 config.set_auto_detect(true); | 300 config.set_auto_detect(true); |
| 299 config.set_pac_url(GURL("http://custom/proxy.pac")); | 301 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 300 | 302 |
| 301 rules.AddFailParsingRule("http://wpad/wpad.dat"); | 303 rules.AddFailParsingRule("http://wpad/wpad.dat"); |
| 302 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 304 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 303 | 305 |
| 304 TestCompletionCallback callback; | 306 TestCompletionCallback callback; |
| 305 CapturingNetLog log(CapturingNetLog::kUnbounded); | 307 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 306 InitProxyResolver init(&resolver, &fetcher, &log); | 308 InitProxyResolver init(&resolver, &fetcher, &log); |
| 307 EXPECT_EQ(OK, init.Init(config, &callback)); | 309 EXPECT_EQ(OK, init.Init(config, &callback)); |
| 308 EXPECT_EQ(rule.bytes(), resolver.pac_bytes()); | 310 EXPECT_EQ(rule.text(), resolver.pac_script()); |
| 309 | 311 |
| 310 // Check the NetLog was filled correctly. | 312 // Check the NetLog was filled correctly. |
| 311 // (Note that the Fetch and Set states are repeated since both WPAD and custom | 313 // (Note that the Fetch and Set states are repeated since both WPAD and custom |
| 312 // PAC scripts are tried). | 314 // PAC scripts are tried). |
| 313 EXPECT_EQ(11u, log.entries().size()); | 315 EXPECT_EQ(11u, log.entries().size()); |
| 314 EXPECT_TRUE(LogContainsBeginEvent( | 316 EXPECT_TRUE(LogContainsBeginEvent( |
| 315 log.entries(), 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 317 log.entries(), 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 316 EXPECT_TRUE(LogContainsBeginEvent( | 318 EXPECT_TRUE(LogContainsBeginEvent( |
| 317 log.entries(), 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 319 log.entries(), 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 318 EXPECT_TRUE(LogContainsEndEvent( | 320 EXPECT_TRUE(LogContainsEndEvent( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 346 ProxyConfig config; | 348 ProxyConfig config; |
| 347 config.set_auto_detect(true); | 349 config.set_auto_detect(true); |
| 348 config.set_pac_url(GURL("http://custom/proxy.pac")); | 350 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 349 | 351 |
| 350 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 352 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 351 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 353 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 352 | 354 |
| 353 TestCompletionCallback callback; | 355 TestCompletionCallback callback; |
| 354 InitProxyResolver init(&resolver, &fetcher, NULL); | 356 InitProxyResolver init(&resolver, &fetcher, NULL); |
| 355 EXPECT_EQ(kFailedDownloading, init.Init(config, &callback)); | 357 EXPECT_EQ(kFailedDownloading, init.Init(config, &callback)); |
| 356 EXPECT_EQ("", resolver.pac_bytes()); | 358 EXPECT_EQ(string16(), resolver.pac_script()); |
| 357 } | 359 } |
| 358 | 360 |
| 359 // Fails at WPAD (downloading), and fails at custom PAC (parsing). | 361 // Fails at WPAD (downloading), and fails at custom PAC (parsing). |
| 360 TEST(InitProxyResolverTest, AutodetectFailCustomFails2) { | 362 TEST(InitProxyResolverTest, AutodetectFailCustomFails2) { |
| 361 Rules rules; | 363 Rules rules; |
| 362 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 364 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 363 RuleBasedProxyScriptFetcher fetcher(&rules); | 365 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 364 | 366 |
| 365 ProxyConfig config; | 367 ProxyConfig config; |
| 366 config.set_auto_detect(true); | 368 config.set_auto_detect(true); |
| 367 config.set_pac_url(GURL("http://custom/proxy.pac")); | 369 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 368 | 370 |
| 369 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 371 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 370 rules.AddFailParsingRule("http://custom/proxy.pac"); | 372 rules.AddFailParsingRule("http://custom/proxy.pac"); |
| 371 | 373 |
| 372 TestCompletionCallback callback; | 374 TestCompletionCallback callback; |
| 373 InitProxyResolver init(&resolver, &fetcher, NULL); | 375 InitProxyResolver init(&resolver, &fetcher, NULL); |
| 374 EXPECT_EQ(kFailedParsing, init.Init(config, &callback)); | 376 EXPECT_EQ(kFailedParsing, init.Init(config, &callback)); |
| 375 EXPECT_EQ("", resolver.pac_bytes()); | 377 EXPECT_EQ(string16(), resolver.pac_script()); |
| 376 } | 378 } |
| 377 | 379 |
| 378 // Fails at WPAD (parsing), but succeeds in choosing the custom PAC. | 380 // Fails at WPAD (parsing), but succeeds in choosing the custom PAC. |
| 379 // This is the same as AutodetectFailCustomSuccess2, but using a ProxyResolver | 381 // This is the same as AutodetectFailCustomSuccess2, but using a ProxyResolver |
| 380 // that doesn't |expects_pac_bytes|. | 382 // that doesn't |expects_pac_bytes|. |
| 381 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2_NoFetch) { | 383 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2_NoFetch) { |
| 382 Rules rules; | 384 Rules rules; |
| 383 RuleBasedProxyResolver resolver(&rules, false /*expects_pac_bytes*/); | 385 RuleBasedProxyResolver resolver(&rules, false /*expects_pac_bytes*/); |
| 384 RuleBasedProxyScriptFetcher fetcher(&rules); | 386 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 385 | 387 |
| 386 ProxyConfig config; | 388 ProxyConfig config; |
| 387 config.set_auto_detect(true); | 389 config.set_auto_detect(true); |
| 388 config.set_pac_url(GURL("http://custom/proxy.pac")); | 390 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 389 | 391 |
| 390 rules.AddFailParsingRule(""); // Autodetect. | 392 rules.AddFailParsingRule(""); // Autodetect. |
| 391 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 393 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 392 | 394 |
| 393 TestCompletionCallback callback; | 395 TestCompletionCallback callback; |
| 394 InitProxyResolver init(&resolver, &fetcher, NULL); | 396 InitProxyResolver init(&resolver, &fetcher, NULL); |
| 395 EXPECT_EQ(OK, init.Init(config, &callback)); | 397 EXPECT_EQ(OK, init.Init(config, &callback)); |
| 396 EXPECT_EQ(rule.url, resolver.pac_url()); | 398 EXPECT_EQ(rule.url, resolver.pac_url()); |
| 397 } | 399 } |
| 398 | 400 |
| 399 } // namespace | 401 } // namespace |
| 400 } // namespace net | 402 } // namespace net |
| OLD | NEW |