[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "base/command_line.h" |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 6 | #include "base/files/file_path.h" |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 7 | #include "base/location.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 8 | #include "base/macros.h" |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 11 | #include "build/build_config.h" |
[email protected] | 9ea0cd3 | 2013-07-12 01:50:36 | [diff] [blame] | 12 | #include "chrome/browser/chrome_notification_types.h" |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser.h" |
tfarina | 535e6f5 | 2016-03-31 13:46:49 | [diff] [blame] | 15 | #include "chrome/browser/ui/login/login_handler.h" |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 16 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 17 | #include "chrome/common/chrome_paths.h" |
| 18 | #include "chrome/common/chrome_switches.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 19 | #include "chrome/test/base/in_process_browser_test.h" |
| 20 | #include "chrome/test/base/ui_test_utils.h" |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 21 | #include "content/public/browser/browser_thread.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 22 | #include "content/public/browser/notification_details.h" |
| 23 | #include "content/public/browser/notification_source.h" |
| 24 | #include "content/public/browser/web_contents.h" |
| 25 | #include "content/public/browser/web_contents_observer.h" |
yzshen | a90291c | 2017-04-26 16:22:52 | [diff] [blame] | 26 | #include "content/public/common/content_switches.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 27 | #include "content/public/test/browser_test_utils.h" |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 28 | #include "net/base/load_flags.h" |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 29 | #include "net/test/embedded_test_server/embedded_test_server.h" |
mmenke | ab0c11d | 2017-05-30 17:15:17 | [diff] [blame^] | 30 | #include "net/test/embedded_test_server/simple_connection_listener.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 31 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 32 | #include "net/test/test_data_directory.h" |
rhalavati | 8aa3fbb7 | 2017-05-22 23:27:18 | [diff] [blame] | 33 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 34 | #include "net/url_request/url_fetcher.h" |
| 35 | #include "net/url_request/url_fetcher_delegate.h" |
| 36 | #include "url/gurl.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 37 | |
| 38 | namespace { |
| 39 | |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 40 | // PAC script that sends all requests to an invalid proxy server. |
| 41 | const base::FilePath::CharType kPACScript[] = FILE_PATH_LITERAL( |
| 42 | "bad_server.pac"); |
| 43 | |
| 44 | // Verify kPACScript is installed as the PAC script. |
| 45 | void VerifyProxyScript(Browser* browser) { |
| 46 | ui_test_utils::NavigateToURL(browser, GURL("http://google.com")); |
| 47 | |
| 48 | // Verify we get the ERR_PROXY_CONNECTION_FAILED screen. |
| 49 | bool result = false; |
| 50 | EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 51 | browser->tab_strip_model()->GetActiveWebContents(), |
| 52 | "var textContent = document.body.textContent;" |
| 53 | "var hasError = textContent.indexOf('ERR_PROXY_CONNECTION_FAILED') >= 0;" |
| 54 | "domAutomationController.send(hasError);", |
| 55 | &result)); |
| 56 | EXPECT_TRUE(result); |
| 57 | } |
| 58 | |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 59 | // This class observes chrome::NOTIFICATION_AUTH_NEEDED and supplies |
| 60 | // the credential which is required by the test proxy server. |
| 61 | // "foo:bar" is the required username and password for our test proxy server. |
| 62 | class LoginPromptObserver : public content::NotificationObserver { |
| 63 | public: |
| 64 | LoginPromptObserver() : auth_handled_(false) {} |
| 65 | |
dcheng | fce29ad | 2014-10-23 03:47:47 | [diff] [blame] | 66 | void Observe(int type, |
| 67 | const content::NotificationSource& source, |
| 68 | const content::NotificationDetails& details) override { |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 69 | if (type == chrome::NOTIFICATION_AUTH_NEEDED) { |
| 70 | LoginNotificationDetails* login_details = |
| 71 | content::Details<LoginNotificationDetails>(details).ptr(); |
| 72 | // |login_details->handler()| is the associated LoginHandler object. |
| 73 | // SetAuth() will close the login dialog. |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 74 | login_details->handler()->SetAuth(base::ASCIIToUTF16("foo"), |
| 75 | base::ASCIIToUTF16("bar")); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 76 | auth_handled_ = true; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | bool auth_handled() const { return auth_handled_; } |
| 81 | |
| 82 | private: |
| 83 | bool auth_handled_; |
[email protected] | 5b7115b3 | 2012-12-05 21:38:09 | [diff] [blame] | 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(LoginPromptObserver); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | class ProxyBrowserTest : public InProcessBrowserTest { |
| 89 | public: |
| 90 | ProxyBrowserTest() |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 91 | : proxy_server_(net::SpawnedTestServer::TYPE_BASIC_AUTH_PROXY, |
| 92 | net::SpawnedTestServer::kLocalhost, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 93 | base::FilePath()) { |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 94 | } |
| 95 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 96 | void SetUp() override { |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 97 | ASSERT_TRUE(proxy_server_.Start()); |
| 98 | InProcessBrowserTest::SetUp(); |
| 99 | } |
| 100 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 101 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 102 | command_line->AppendSwitchASCII(switches::kProxyServer, |
| 103 | proxy_server_.host_port_pair().ToString()); |
| 104 | } |
| 105 | |
| 106 | protected: |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 107 | net::SpawnedTestServer proxy_server_; |
[email protected] | 5b7115b3 | 2012-12-05 21:38:09 | [diff] [blame] | 108 | |
| 109 | private: |
[email protected] | 5b7115b3 | 2012-12-05 21:38:09 | [diff] [blame] | 110 | DISALLOW_COPY_AND_ASSIGN(ProxyBrowserTest); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | #if defined(OS_CHROMEOS) |
| 114 | // We bypass manually installed proxy for localhost on chromeos. |
| 115 | #define MAYBE_BasicAuthWSConnect DISABLED_BasicAuthWSConnect |
| 116 | #else |
| 117 | #define MAYBE_BasicAuthWSConnect BasicAuthWSConnect |
| 118 | #endif |
| 119 | // Test that the browser can establish a WebSocket connection via a proxy |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 120 | // that requires basic authentication. This test also checks the headers |
| 121 | // arrive at WebSocket server. |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 122 | IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, MAYBE_BasicAuthWSConnect) { |
| 123 | // Launch WebSocket server. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 124 | net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS, |
| 125 | net::SpawnedTestServer::kLocalhost, |
| 126 | net::GetWebSocketTestDataDirectory()); |
[email protected] | e0e6f9f | 2012-10-24 05:35:44 | [diff] [blame] | 127 | ASSERT_TRUE(ws_server.Start()); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 128 | |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 129 | content::WebContents* tab = |
| 130 | browser()->tab_strip_model()->GetActiveWebContents(); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 131 | content::NavigationController* controller = &tab->GetController(); |
| 132 | content::NotificationRegistrar registrar; |
| 133 | // The proxy server will request basic authentication. |
| 134 | // |observer| supplies the credential. |
| 135 | LoginPromptObserver observer; |
| 136 | registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED, |
| 137 | content::Source<content::NavigationController>(controller)); |
| 138 | |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 139 | content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); |
| 140 | watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 141 | |
| 142 | // Visit a page that tries to establish WebSocket connection. The title |
| 143 | // of the page will be 'PASS' on success. |
[email protected] | e0e6f9f | 2012-10-24 05:35:44 | [diff] [blame] | 144 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 145 | replacements.SetSchemeStr("http"); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 146 | ui_test_utils::NavigateToURL(browser(), |
| 147 | ws_server.GetURL("proxied_request_check.html") |
| 148 | .ReplaceComponents(replacements)); |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 149 | |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 150 | const base::string16 result = watcher.WaitAndGetTitle(); |
brettw | 00a56b7 | 2015-06-10 03:47:26 | [diff] [blame] | 151 | EXPECT_TRUE(base::EqualsASCII(result, "PASS")); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 152 | EXPECT_TRUE(observer.auth_handled()); |
| 153 | } |
| 154 | |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 155 | // Fetch PAC script via an http:// URL. |
| 156 | class HttpProxyScriptBrowserTest : public InProcessBrowserTest { |
| 157 | public: |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 158 | HttpProxyScriptBrowserTest() { |
| 159 | http_server_.ServeFilesFromSourceDirectory("chrome/test/data"); |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 160 | } |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 161 | ~HttpProxyScriptBrowserTest() override {} |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 162 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 163 | void SetUp() override { |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 164 | ASSERT_TRUE(http_server_.Start()); |
| 165 | InProcessBrowserTest::SetUp(); |
| 166 | } |
| 167 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 168 | void SetUpCommandLine(base::CommandLine* command_line) override { |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 169 | base::FilePath pac_script_path(FILE_PATH_LITERAL("/")); |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 170 | command_line->AppendSwitchASCII(switches::kProxyPacUrl, http_server_.GetURL( |
| 171 | pac_script_path.Append(kPACScript).MaybeAsASCII()).spec()); |
| 172 | } |
| 173 | |
| 174 | private: |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 175 | net::EmbeddedTestServer http_server_; |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 176 | |
| 177 | DISALLOW_COPY_AND_ASSIGN(HttpProxyScriptBrowserTest); |
| 178 | }; |
| 179 | |
| 180 | IN_PROC_BROWSER_TEST_F(HttpProxyScriptBrowserTest, Verify) { |
| 181 | VerifyProxyScript(browser()); |
| 182 | } |
| 183 | |
| 184 | // Fetch PAC script via a file:// URL. |
| 185 | class FileProxyScriptBrowserTest : public InProcessBrowserTest { |
| 186 | public: |
| 187 | FileProxyScriptBrowserTest() {} |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 188 | ~FileProxyScriptBrowserTest() override {} |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 189 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 190 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 191 | command_line->AppendSwitchASCII(switches::kProxyPacUrl, |
| 192 | ui_test_utils::GetTestUrl( |
| 193 | base::FilePath(base::FilePath::kCurrentDirectory), |
| 194 | base::FilePath(kPACScript)).spec()); |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | DISALLOW_COPY_AND_ASSIGN(FileProxyScriptBrowserTest); |
| 199 | }; |
| 200 | |
| 201 | IN_PROC_BROWSER_TEST_F(FileProxyScriptBrowserTest, Verify) { |
| 202 | VerifyProxyScript(browser()); |
| 203 | } |
| 204 | |
| 205 | // Fetch PAC script via an ftp:// URL. |
| 206 | class FtpProxyScriptBrowserTest : public InProcessBrowserTest { |
| 207 | public: |
| 208 | FtpProxyScriptBrowserTest() |
| 209 | : ftp_server_(net::SpawnedTestServer::TYPE_FTP, |
| 210 | net::SpawnedTestServer::kLocalhost, |
| 211 | base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))) { |
| 212 | } |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 213 | ~FtpProxyScriptBrowserTest() override {} |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 214 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 215 | void SetUp() override { |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 216 | ASSERT_TRUE(ftp_server_.Start()); |
| 217 | InProcessBrowserTest::SetUp(); |
| 218 | } |
| 219 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 220 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 221 | base::FilePath pac_script_path(kPACScript); |
| 222 | command_line->AppendSwitchASCII( |
| 223 | switches::kProxyPacUrl, |
| 224 | ftp_server_.GetURL(pac_script_path.MaybeAsASCII()).spec()); |
| 225 | } |
| 226 | |
| 227 | private: |
| 228 | net::SpawnedTestServer ftp_server_; |
| 229 | |
| 230 | DISALLOW_COPY_AND_ASSIGN(FtpProxyScriptBrowserTest); |
| 231 | }; |
| 232 | |
| 233 | IN_PROC_BROWSER_TEST_F(FtpProxyScriptBrowserTest, Verify) { |
| 234 | VerifyProxyScript(browser()); |
| 235 | } |
| 236 | |
| 237 | // Fetch PAC script via a data: URL. |
| 238 | class DataProxyScriptBrowserTest : public InProcessBrowserTest { |
| 239 | public: |
| 240 | DataProxyScriptBrowserTest() {} |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 241 | ~DataProxyScriptBrowserTest() override {} |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 242 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 243 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 244 | std::string contents; |
| 245 | // Read in kPACScript contents. |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 246 | ASSERT_TRUE(base::ReadFileToString(ui_test_utils::GetTestFilePath( |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 247 | base::FilePath(base::FilePath::kCurrentDirectory), |
| 248 | base::FilePath(kPACScript)), |
| 249 | &contents)); |
| 250 | command_line->AppendSwitchASCII(switches::kProxyPacUrl, |
| 251 | std::string("data:,") + contents); |
| 252 | } |
| 253 | |
| 254 | private: |
| 255 | DISALLOW_COPY_AND_ASSIGN(DataProxyScriptBrowserTest); |
| 256 | }; |
| 257 | |
| 258 | IN_PROC_BROWSER_TEST_F(DataProxyScriptBrowserTest, Verify) { |
| 259 | VerifyProxyScript(browser()); |
| 260 | } |
| 261 | |
scottmg | e8de13f1 | 2017-01-19 01:42:01 | [diff] [blame] | 262 | // Fetch PAC script via a data: URL. |
amistry | 467cdc7 | 2015-03-13 01:58:47 | [diff] [blame] | 263 | class OutOfProcessProxyResolverBrowserTest : public InProcessBrowserTest { |
| 264 | public: |
| 265 | OutOfProcessProxyResolverBrowserTest() {} |
| 266 | ~OutOfProcessProxyResolverBrowserTest() override {} |
| 267 | |
| 268 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 269 | std::string contents; |
| 270 | // Read in kPACScript contents. |
| 271 | ASSERT_TRUE(base::ReadFileToString(ui_test_utils::GetTestFilePath( |
| 272 | base::FilePath(base::FilePath::kCurrentDirectory), |
| 273 | base::FilePath(kPACScript)), |
| 274 | &contents)); |
| 275 | command_line->AppendSwitchASCII( |
| 276 | switches::kProxyPacUrl, "data:," + contents); |
amistry | 467cdc7 | 2015-03-13 01:58:47 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | private: |
| 280 | DISALLOW_COPY_AND_ASSIGN(OutOfProcessProxyResolverBrowserTest); |
| 281 | }; |
| 282 | |
| 283 | IN_PROC_BROWSER_TEST_F(OutOfProcessProxyResolverBrowserTest, Verify) { |
| 284 | VerifyProxyScript(browser()); |
| 285 | } |
| 286 | |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 287 | // Fetch PAC script via a hanging http:// URL. |
| 288 | class HangingPacRequestProxyScriptBrowserTest : public InProcessBrowserTest { |
| 289 | public: |
| 290 | HangingPacRequestProxyScriptBrowserTest() {} |
| 291 | ~HangingPacRequestProxyScriptBrowserTest() override {} |
| 292 | |
| 293 | void SetUp() override { |
| 294 | // Must start listening (And get a port for the proxy) before calling |
| 295 | // SetUp(). |
| 296 | ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); |
| 297 | InProcessBrowserTest::SetUp(); |
| 298 | } |
| 299 | |
mmenke | ab0c11d | 2017-05-30 17:15:17 | [diff] [blame^] | 300 | void TearDown() override { |
| 301 | // Need to stop this before |connection_listener_| is destroyed. |
| 302 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 303 | InProcessBrowserTest::TearDown(); |
| 304 | } |
| 305 | |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 306 | void SetUpOnMainThread() override { |
| 307 | // This must be created after the main message loop has been set up. |
mmenke | ab0c11d | 2017-05-30 17:15:17 | [diff] [blame^] | 308 | // Waits for one connection. Additional connections are fine. |
| 309 | connection_listener_ = |
| 310 | base::MakeUnique<net::test_server::SimpleConnectionListener>( |
| 311 | 1, net::test_server::SimpleConnectionListener:: |
| 312 | ALLOW_ADDITIONAL_CONNECTIONS); |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 313 | embedded_test_server()->SetConnectionListener(connection_listener_.get()); |
| 314 | embedded_test_server()->StartAcceptingConnections(); |
| 315 | |
| 316 | InProcessBrowserTest::SetUpOnMainThread(); |
| 317 | } |
| 318 | |
| 319 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 320 | command_line->AppendSwitchASCII( |
| 321 | switches::kProxyPacUrl, embedded_test_server()->GetURL("/hung").spec()); |
| 322 | } |
| 323 | |
| 324 | protected: |
mmenke | ab0c11d | 2017-05-30 17:15:17 | [diff] [blame^] | 325 | std::unique_ptr<net::test_server::SimpleConnectionListener> |
| 326 | connection_listener_; |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 327 | |
| 328 | private: |
| 329 | DISALLOW_COPY_AND_ASSIGN(HangingPacRequestProxyScriptBrowserTest); |
| 330 | }; |
| 331 | |
| 332 | // URLFetcherDelegate that expects a request to hang. |
| 333 | class HangingURLFetcherDelegate : public net::URLFetcherDelegate { |
| 334 | public: |
| 335 | HangingURLFetcherDelegate() {} |
| 336 | ~HangingURLFetcherDelegate() override {} |
| 337 | |
| 338 | void OnURLFetchComplete(const net::URLFetcher* source) override { |
| 339 | ADD_FAILURE() << "This request should never complete."; |
| 340 | } |
| 341 | |
| 342 | private: |
| 343 | DISALLOW_COPY_AND_ASSIGN(HangingURLFetcherDelegate); |
| 344 | }; |
| 345 | |
| 346 | // Check that the URLRequest for a PAC that is still alive during shutdown is |
| 347 | // safely cleaned up. This test relies on AssertNoURLRequests being called on |
| 348 | // the main URLRequestContext. |
| 349 | IN_PROC_BROWSER_TEST_F(HangingPacRequestProxyScriptBrowserTest, Shutdown) { |
| 350 | // Request that should hang while trying to request the PAC script. |
| 351 | // Enough requests are created on startup that this probably isn't needed, but |
| 352 | // best to be safe. |
| 353 | HangingURLFetcherDelegate hanging_request_delegate; |
| 354 | std::unique_ptr<net::URLFetcher> hanging_fetcher = net::URLFetcher::Create( |
rhalavati | 8aa3fbb7 | 2017-05-22 23:27:18 | [diff] [blame] | 355 | GURL("http://blah/"), net::URLFetcher::GET, &hanging_request_delegate, |
| 356 | TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 357 | hanging_fetcher->SetRequestContext(browser()->profile()->GetRequestContext()); |
| 358 | hanging_fetcher->Start(); |
| 359 | |
mmenke | ab0c11d | 2017-05-30 17:15:17 | [diff] [blame^] | 360 | connection_listener_->WaitForConnections(); |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 361 | } |
| 362 | |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 363 | } // namespace |