[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 5 | #include "net/proxy_resolution/proxy_config.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 6 | #include "base/stl_util.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 7 | #include "net/proxy_resolution/proxy_config_service_common_unittest.h" |
| 8 | #include "net/proxy_resolution/proxy_info.h" |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 11 | namespace net { |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 12 | namespace { |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 13 | |
| 14 | void ExpectProxyServerEquals(const char* expectation, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 15 | const ProxyList& proxy_servers) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 16 | if (expectation == nullptr) { |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 17 | EXPECT_TRUE(proxy_servers.IsEmpty()); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 18 | } else { |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 19 | EXPECT_EQ(expectation, proxy_servers.ToPacString()); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 20 | } |
| 21 | } |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 22 | |
| 23 | TEST(ProxyConfigTest, Equals) { |
| 24 | // Test |ProxyConfig::auto_detect|. |
| 25 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 26 | ProxyConfig config1; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 27 | config1.set_auto_detect(true); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 28 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 29 | ProxyConfig config2; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 30 | config2.set_auto_detect(false); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 31 | |
| 32 | EXPECT_FALSE(config1.Equals(config2)); |
| 33 | EXPECT_FALSE(config2.Equals(config1)); |
| 34 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 35 | config2.set_auto_detect(true); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 36 | |
| 37 | EXPECT_TRUE(config1.Equals(config2)); |
| 38 | EXPECT_TRUE(config2.Equals(config1)); |
| 39 | |
| 40 | // Test |ProxyConfig::pac_url|. |
| 41 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 42 | config2.set_pac_url(GURL("http://wpad/wpad.dat")); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 43 | |
| 44 | EXPECT_FALSE(config1.Equals(config2)); |
| 45 | EXPECT_FALSE(config2.Equals(config1)); |
| 46 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 47 | config1.set_pac_url(GURL("http://wpad/wpad.dat")); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 48 | |
| 49 | EXPECT_TRUE(config1.Equals(config2)); |
| 50 | EXPECT_TRUE(config2.Equals(config1)); |
| 51 | |
| 52 | // Test |ProxyConfig::proxy_rules|. |
| 53 | |
Lily Houghton | e6b617e | 2018-01-19 20:13:07 | [diff] [blame] | 54 | config2.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 55 | config2.proxy_rules().single_proxies.SetSingleProxyServer( |
| 56 | ProxyServer::FromURI("myproxy:80", ProxyServer::SCHEME_HTTP)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 57 | |
| 58 | EXPECT_FALSE(config1.Equals(config2)); |
| 59 | EXPECT_FALSE(config2.Equals(config1)); |
| 60 | |
Lily Houghton | e6b617e | 2018-01-19 20:13:07 | [diff] [blame] | 61 | config1.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 62 | config1.proxy_rules().single_proxies.SetSingleProxyServer( |
| 63 | ProxyServer::FromURI("myproxy:100", ProxyServer::SCHEME_HTTP)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 64 | |
| 65 | EXPECT_FALSE(config1.Equals(config2)); |
| 66 | EXPECT_FALSE(config2.Equals(config1)); |
| 67 | |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 68 | config1.proxy_rules().single_proxies.SetSingleProxyServer( |
| 69 | ProxyServer::FromURI("myproxy", ProxyServer::SCHEME_HTTP)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 70 | |
| 71 | EXPECT_TRUE(config1.Equals(config2)); |
| 72 | EXPECT_TRUE(config2.Equals(config1)); |
| 73 | |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 74 | // Test |ProxyConfig::bypass_rules|. |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 75 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 76 | config2.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 77 | |
| 78 | EXPECT_FALSE(config1.Equals(config2)); |
| 79 | EXPECT_FALSE(config2.Equals(config1)); |
| 80 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 81 | config1.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 82 | |
| 83 | EXPECT_TRUE(config1.Equals(config2)); |
| 84 | EXPECT_TRUE(config2.Equals(config1)); |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 85 | |
| 86 | // Test |ProxyConfig::proxy_rules.reverse_bypass|. |
| 87 | |
| 88 | config2.proxy_rules().reverse_bypass = true; |
| 89 | |
| 90 | EXPECT_FALSE(config1.Equals(config2)); |
| 91 | EXPECT_FALSE(config2.Equals(config1)); |
| 92 | |
| 93 | config1.proxy_rules().reverse_bypass = true; |
| 94 | |
| 95 | EXPECT_TRUE(config1.Equals(config2)); |
| 96 | EXPECT_TRUE(config2.Equals(config1)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST(ProxyConfigTest, ParseProxyRules) { |
| 100 | const struct { |
| 101 | const char* proxy_rules; |
| 102 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 103 | ProxyConfig::ProxyRules::Type type; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 104 | // These will be PAC-stle strings, eg 'PROXY foo.com' |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 105 | const char* single_proxy; |
| 106 | const char* proxy_for_http; |
| 107 | const char* proxy_for_https; |
| 108 | const char* proxy_for_ftp; |
[email protected] | 2b6ed3dc | 2010-08-25 06:03:48 | [diff] [blame] | 109 | const char* fallback_proxy; |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 110 | } tests[] = { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 111 | // One HTTP proxy for all schemes. |
| 112 | { |
| 113 | "myproxy:80", |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 114 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 115 | ProxyConfig::ProxyRules::Type::PROXY_LIST, |
| 116 | "PROXY myproxy:80", |
| 117 | nullptr, |
| 118 | nullptr, |
| 119 | nullptr, |
| 120 | nullptr, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 121 | }, |
| 122 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 123 | // Multiple HTTP proxies for all schemes. |
| 124 | { |
| 125 | "myproxy:80,https://myotherproxy", |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 126 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 127 | ProxyConfig::ProxyRules::Type::PROXY_LIST, |
| 128 | "PROXY myproxy:80;HTTPS myotherproxy:443", |
| 129 | nullptr, |
| 130 | nullptr, |
| 131 | nullptr, |
| 132 | nullptr, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 133 | }, |
| 134 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 135 | // Only specify a proxy server for "http://" urls. |
| 136 | { |
| 137 | "http=myproxy:80", |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 138 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 139 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 140 | nullptr, |
| 141 | "PROXY myproxy:80", |
| 142 | nullptr, |
| 143 | nullptr, |
| 144 | nullptr, |
| 145 | }, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 146 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 147 | // Specify an HTTP proxy for "ftp://" and a SOCKS proxy for "https://" |
| 148 | // urls. |
| 149 | { |
| 150 | "ftp=ftp-proxy ; https=socks4://foopy", |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 151 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 152 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 153 | nullptr, |
| 154 | nullptr, |
| 155 | "SOCKS foopy:1080", |
| 156 | "PROXY ftp-proxy:80", |
| 157 | nullptr, |
| 158 | }, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 159 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 160 | // Give a scheme-specific proxy as well as a non-scheme specific. |
| 161 | // The first entry "foopy" takes precedance marking this list as |
| 162 | // Type::PROXY_LIST. |
| 163 | { |
| 164 | "foopy ; ftp=ftp-proxy", |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 165 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 166 | ProxyConfig::ProxyRules::Type::PROXY_LIST, |
| 167 | "PROXY foopy:80", |
| 168 | nullptr, |
| 169 | nullptr, |
| 170 | nullptr, |
| 171 | nullptr, |
| 172 | }, |
| 173 | |
| 174 | // Give a scheme-specific proxy as well as a non-scheme specific. |
| 175 | // The first entry "ftp=ftp-proxy" takes precedance marking this list as |
| 176 | // Type::PROXY_LIST_PER_SCHEME. |
| 177 | { |
| 178 | "ftp=ftp-proxy ; foopy", |
| 179 | |
| 180 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 181 | nullptr, |
| 182 | nullptr, |
| 183 | nullptr, |
| 184 | "PROXY ftp-proxy:80", |
| 185 | nullptr, |
| 186 | }, |
| 187 | |
| 188 | // Include a list of entries for a single scheme. |
| 189 | { |
| 190 | "ftp=ftp1,ftp2,ftp3", |
| 191 | |
| 192 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 193 | nullptr, |
| 194 | nullptr, |
| 195 | nullptr, |
| 196 | "PROXY ftp1:80;PROXY ftp2:80;PROXY ftp3:80", |
| 197 | nullptr, |
| 198 | }, |
| 199 | |
| 200 | // Include multiple entries for the same scheme -- they accumulate. |
| 201 | { |
| 202 | "http=http1,http2; http=http3", |
| 203 | |
| 204 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 205 | nullptr, |
| 206 | "PROXY http1:80;PROXY http2:80;PROXY http3:80", |
| 207 | nullptr, |
| 208 | nullptr, |
| 209 | nullptr, |
| 210 | }, |
| 211 | |
| 212 | // Include lists of entries for multiple schemes. |
| 213 | { |
| 214 | "ftp=ftp1,ftp2,ftp3 ; http=http1,http2; ", |
| 215 | |
| 216 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 217 | nullptr, |
| 218 | "PROXY http1:80;PROXY http2:80", |
| 219 | nullptr, |
| 220 | "PROXY ftp1:80;PROXY ftp2:80;PROXY ftp3:80", |
| 221 | nullptr, |
| 222 | }, |
| 223 | |
| 224 | // Include non-default proxy schemes. |
| 225 | { |
| 226 | "http=https://secure_proxy; ftp=socks4://socks_proxy; " |
| 227 | "https=socks://foo", |
| 228 | |
| 229 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 230 | nullptr, |
| 231 | "HTTPS secure_proxy:443", |
| 232 | "SOCKS5 foo:1080", |
| 233 | "SOCKS socks_proxy:1080", |
| 234 | nullptr, |
| 235 | }, |
| 236 | |
| 237 | // Only SOCKS proxy present, others being blank. |
| 238 | { |
| 239 | "socks=foopy", |
| 240 | |
| 241 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 242 | nullptr, |
| 243 | nullptr, |
| 244 | nullptr, |
| 245 | nullptr, |
| 246 | "SOCKS foopy:1080", |
| 247 | }, |
| 248 | |
| 249 | // SOCKS proxy present along with other proxies too |
| 250 | { |
| 251 | "http=httpproxy ; https=httpsproxy ; ftp=ftpproxy ; socks=foopy ", |
| 252 | |
| 253 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 254 | nullptr, |
| 255 | "PROXY httpproxy:80", |
| 256 | "PROXY httpsproxy:80", |
| 257 | "PROXY ftpproxy:80", |
| 258 | "SOCKS foopy:1080", |
| 259 | }, |
| 260 | |
| 261 | // SOCKS proxy (with modifier) present along with some proxies |
| 262 | // (FTP being blank) |
| 263 | { |
| 264 | "http=httpproxy ; https=httpsproxy ; socks=socks5://foopy ", |
| 265 | |
| 266 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 267 | nullptr, |
| 268 | "PROXY httpproxy:80", |
| 269 | "PROXY httpsproxy:80", |
| 270 | nullptr, |
| 271 | "SOCKS5 foopy:1080", |
| 272 | }, |
| 273 | |
| 274 | // Include unsupported schemes -- they are discarded. |
| 275 | { |
| 276 | "crazy=foopy ; foo=bar ; https=myhttpsproxy", |
| 277 | |
| 278 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 279 | nullptr, |
| 280 | nullptr, |
| 281 | "PROXY myhttpsproxy:80", |
| 282 | nullptr, |
| 283 | nullptr, |
| 284 | }, |
| 285 | |
| 286 | // direct:// as first option for a scheme. |
| 287 | { |
| 288 | "http=direct://,myhttpproxy; https=direct://", |
| 289 | |
| 290 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 291 | nullptr, |
| 292 | "DIRECT;PROXY myhttpproxy:80", |
| 293 | "DIRECT", |
| 294 | nullptr, |
| 295 | nullptr, |
| 296 | }, |
| 297 | |
| 298 | // direct:// as a second option for a scheme. |
| 299 | { |
| 300 | "http=myhttpproxy,direct://", |
| 301 | |
| 302 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 303 | nullptr, |
| 304 | "PROXY myhttpproxy:80;DIRECT", |
| 305 | nullptr, |
| 306 | nullptr, |
| 307 | nullptr, |
| 308 | }, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 309 | |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 310 | }; |
| 311 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 312 | ProxyConfig config; |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 313 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 314 | for (size_t i = 0; i < base::size(tests); ++i) { |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 315 | config.proxy_rules().ParseFromString(tests[i].proxy_rules); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 316 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 317 | EXPECT_EQ(tests[i].type, config.proxy_rules().type); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 318 | ExpectProxyServerEquals(tests[i].single_proxy, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 319 | config.proxy_rules().single_proxies); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 320 | ExpectProxyServerEquals(tests[i].proxy_for_http, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 321 | config.proxy_rules().proxies_for_http); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 322 | ExpectProxyServerEquals(tests[i].proxy_for_https, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 323 | config.proxy_rules().proxies_for_https); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 324 | ExpectProxyServerEquals(tests[i].proxy_for_ftp, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 325 | config.proxy_rules().proxies_for_ftp); |
[email protected] | 2b6ed3dc | 2010-08-25 06:03:48 | [diff] [blame] | 326 | ExpectProxyServerEquals(tests[i].fallback_proxy, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 327 | config.proxy_rules().fallback_proxies); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 328 | } |
| 329 | } |
[email protected] | ab501a6a | 2009-05-12 15:07:50 | [diff] [blame] | 330 | |
[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 331 | TEST(ProxyConfigTest, ProxyRulesSetBypassFlag) { |
| 332 | // Test whether the did_bypass_proxy() flag is set in proxy info correctly. |
| 333 | ProxyConfig::ProxyRules rules; |
| 334 | ProxyInfo result; |
| 335 | |
| 336 | rules.ParseFromString("http=httpproxy:80"); |
| 337 | rules.bypass_rules.AddRuleFromString(".com"); |
| 338 | |
| 339 | rules.Apply(GURL("http://example.com"), &result); |
| 340 | EXPECT_TRUE(result.is_direct_only()); |
| 341 | EXPECT_TRUE(result.did_bypass_proxy()); |
| 342 | |
| 343 | rules.Apply(GURL("http://example.org"), &result); |
| 344 | EXPECT_FALSE(result.is_direct()); |
| 345 | EXPECT_FALSE(result.did_bypass_proxy()); |
| 346 | |
| 347 | // Try with reversed bypass rules. |
| 348 | rules.reverse_bypass = true; |
| 349 | |
| 350 | rules.Apply(GURL("http://example.org"), &result); |
| 351 | EXPECT_TRUE(result.is_direct_only()); |
| 352 | EXPECT_TRUE(result.did_bypass_proxy()); |
| 353 | |
| 354 | rules.Apply(GURL("http://example.com"), &result); |
| 355 | EXPECT_FALSE(result.is_direct()); |
| 356 | EXPECT_FALSE(result.did_bypass_proxy()); |
| 357 | } |
| 358 | |
ricea | bec9560 | 2014-10-30 05:05:07 | [diff] [blame] | 359 | static const char kWsUrl[] = "ws://example.com/echo"; |
| 360 | static const char kWssUrl[] = "wss://example.com/echo"; |
| 361 | |
| 362 | class ProxyConfigWebSocketTest : public ::testing::Test { |
| 363 | protected: |
| 364 | void ParseFromString(const std::string& rules) { |
| 365 | rules_.ParseFromString(rules); |
| 366 | } |
| 367 | void Apply(const GURL& gurl) { rules_.Apply(gurl, &info_); } |
| 368 | std::string ToPacString() const { return info_.ToPacString(); } |
| 369 | |
| 370 | static GURL WsUrl() { return GURL(kWsUrl); } |
| 371 | static GURL WssUrl() { return GURL(kWssUrl); } |
| 372 | |
| 373 | ProxyConfig::ProxyRules rules_; |
| 374 | ProxyInfo info_; |
| 375 | }; |
| 376 | |
| 377 | // If a single proxy is set for all protocols, WebSocket uses it. |
| 378 | TEST_F(ProxyConfigWebSocketTest, UsesProxy) { |
| 379 | ParseFromString("proxy:3128"); |
| 380 | Apply(WsUrl()); |
| 381 | EXPECT_EQ("PROXY proxy:3128", ToPacString()); |
| 382 | } |
| 383 | |
| 384 | // See RFC6455 Section 4.1. item 3, "_Proxy Usage_". |
| 385 | TEST_F(ProxyConfigWebSocketTest, PrefersSocks) { |
| 386 | ParseFromString( |
| 387 | "http=proxy:3128 ; https=sslproxy:3128 ; socks=socksproxy:1080"); |
| 388 | Apply(WsUrl()); |
| 389 | EXPECT_EQ("SOCKS socksproxy:1080", ToPacString()); |
| 390 | } |
| 391 | |
| 392 | TEST_F(ProxyConfigWebSocketTest, PrefersHttpsToHttp) { |
| 393 | ParseFromString("http=proxy:3128 ; https=sslproxy:3128"); |
| 394 | Apply(WssUrl()); |
| 395 | EXPECT_EQ("PROXY sslproxy:3128", ToPacString()); |
| 396 | } |
| 397 | |
| 398 | TEST_F(ProxyConfigWebSocketTest, PrefersHttpsEvenForWs) { |
| 399 | ParseFromString("http=proxy:3128 ; https=sslproxy:3128"); |
| 400 | Apply(WsUrl()); |
| 401 | EXPECT_EQ("PROXY sslproxy:3128", ToPacString()); |
| 402 | } |
| 403 | |
| 404 | TEST_F(ProxyConfigWebSocketTest, PrefersHttpToDirect) { |
| 405 | ParseFromString("http=proxy:3128"); |
| 406 | Apply(WssUrl()); |
| 407 | EXPECT_EQ("PROXY proxy:3128", ToPacString()); |
| 408 | } |
| 409 | |
| 410 | TEST_F(ProxyConfigWebSocketTest, IgnoresFtpProxy) { |
| 411 | ParseFromString("ftp=ftpproxy:3128"); |
| 412 | Apply(WssUrl()); |
| 413 | EXPECT_EQ("DIRECT", ToPacString()); |
| 414 | } |
| 415 | |
| 416 | TEST_F(ProxyConfigWebSocketTest, ObeysBypassRules) { |
| 417 | ParseFromString("http=proxy:3128 ; https=sslproxy:3128"); |
| 418 | rules_.bypass_rules.AddRuleFromString(".chromium.org"); |
| 419 | Apply(GURL("wss://codereview.chromium.org/feed")); |
| 420 | EXPECT_EQ("DIRECT", ToPacString()); |
| 421 | } |
| 422 | |
| 423 | TEST_F(ProxyConfigWebSocketTest, ObeysLocalBypass) { |
| 424 | ParseFromString("http=proxy:3128 ; https=sslproxy:3128"); |
| 425 | rules_.bypass_rules.AddRuleFromString("<local>"); |
| 426 | Apply(GURL("ws://localhost/feed")); |
| 427 | EXPECT_EQ("DIRECT", ToPacString()); |
| 428 | } |
| 429 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 430 | } // namespace |
| 431 | } // namespace net |