OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "chrome/browser/io_thread.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/run_loop.h" |
| 15 #include "base/strings/stringprintf.h" |
| 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "net/base/filename_util.h" |
| 20 #include "net/base/host_port_pair.h" |
| 21 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 22 #include "net/test/embedded_test_server/simple_connection_listener.h" |
| 23 #include "net/url_request/url_fetcher.h" |
| 24 #include "net/url_request/url_fetcher_delegate.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "url/gurl.h" |
| 27 |
| 28 namespace { |
| 29 |
| 30 // URLFetcherDelegate that expects a request to hang. |
| 31 class HangingURLFetcherDelegate : public net::URLFetcherDelegate { |
| 32 public: |
| 33 HangingURLFetcherDelegate() {} |
| 34 ~HangingURLFetcherDelegate() override {} |
| 35 |
| 36 void OnURLFetchComplete(const net::URLFetcher* source) override { |
| 37 ADD_FAILURE() << "This request should never complete."; |
| 38 } |
| 39 |
| 40 private: |
| 41 DISALLOW_COPY_AND_ASSIGN(HangingURLFetcherDelegate); |
| 42 }; |
| 43 |
| 44 // URLFetcherDelegate that can wait for a request to succeed. |
| 45 class TestURLFetcherDelegate : public net::URLFetcherDelegate { |
| 46 public: |
| 47 TestURLFetcherDelegate() {} |
| 48 ~TestURLFetcherDelegate() override {} |
| 49 |
| 50 void OnURLFetchComplete(const net::URLFetcher* source) override { |
| 51 run_loop_.Quit(); |
| 52 } |
| 53 |
| 54 void WaitForCompletion() { run_loop_.Run(); } |
| 55 |
| 56 private: |
| 57 base::RunLoop run_loop_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherDelegate); |
| 60 }; |
| 61 |
| 62 class IOThreadPacTest : public InProcessBrowserTest { |
| 63 public: |
| 64 IOThreadPacTest() {} |
| 65 ~IOThreadPacTest() override {} |
| 66 |
| 67 void SetUp() override { |
| 68 // Must start listening (And get a port for the proxy) before calling |
| 69 // SetUp(). |
| 70 ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); |
| 71 InProcessBrowserTest::SetUp(); |
| 72 } |
| 73 |
| 74 void SetUpOnMainThread() override { |
| 75 embedded_test_server()->StartAcceptingConnections(); |
| 76 |
| 77 InProcessBrowserTest::SetUpOnMainThread(); |
| 78 } |
| 79 |
| 80 void TearDown() override { |
| 81 // Need to stop this before |connection_listener_| is destroyed. |
| 82 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 83 InProcessBrowserTest::TearDown(); |
| 84 } |
| 85 }; |
| 86 |
| 87 class IOThreadPacTestWithHangingRequest : public IOThreadPacTest { |
| 88 public: |
| 89 IOThreadPacTestWithHangingRequest() {} |
| 90 ~IOThreadPacTestWithHangingRequest() override {} |
| 91 |
| 92 void SetUpOnMainThread() override { |
| 93 // This must be created after the main message loop has been set up. |
| 94 // Waits for one connection. Additional connections are fine. |
| 95 connection_listener_ = |
| 96 base::MakeUnique<net::test_server::SimpleConnectionListener>( |
| 97 1, net::test_server::SimpleConnectionListener:: |
| 98 ALLOW_ADDITIONAL_CONNECTIONS); |
| 99 |
| 100 embedded_test_server()->SetConnectionListener(connection_listener_.get()); |
| 101 |
| 102 IOThreadPacTest::SetUpOnMainThread(); |
| 103 } |
| 104 |
| 105 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 106 command_line->AppendSwitchASCII( |
| 107 switches::kProxyPacUrl, embedded_test_server()->GetURL("/hung").spec()); |
| 108 } |
| 109 |
| 110 protected: |
| 111 std::unique_ptr<net::test_server::SimpleConnectionListener> |
| 112 connection_listener_; |
| 113 }; |
| 114 |
| 115 // Make sure that the SystemURLRequestContext is shut down correctly when |
| 116 // there's an in-progress PAC script fetch. |
| 117 IN_PROC_BROWSER_TEST_F(IOThreadPacTestWithHangingRequest, Shutdown) { |
| 118 // Request that should hang while trying to request the PAC script. |
| 119 // Enough requests are created on startup that this probably isn't needed, but |
| 120 // best to be safe. |
| 121 HangingURLFetcherDelegate hanging_fetcher_delegate; |
| 122 std::unique_ptr<net::URLFetcher> hanging_fetcher = net::URLFetcher::Create( |
| 123 GURL("http://blah/"), net::URLFetcher::GET, &hanging_fetcher_delegate); |
| 124 hanging_fetcher->SetRequestContext( |
| 125 g_browser_process->io_thread()->system_url_request_context_getter()); |
| 126 hanging_fetcher->Start(); |
| 127 |
| 128 connection_listener_->WaitForConnections(); |
| 129 } |
| 130 |
| 131 class IOThreadPacTestWithFileURL : public IOThreadPacTest { |
| 132 public: |
| 133 IOThreadPacTestWithFileURL() { EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
| 134 |
| 135 ~IOThreadPacTestWithFileURL() override {} |
| 136 |
| 137 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 138 base::FilePath pac_file_path; |
| 139 ASSERT_TRUE( |
| 140 base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &pac_file_path)); |
| 141 |
| 142 std::string pac_script = base::StringPrintf( |
| 143 "function FindProxyForURL(url, host){ return 'PROXY %s;'; }", |
| 144 net::HostPortPair::FromURL(embedded_test_server()->base_url()) |
| 145 .ToString() |
| 146 .c_str()); |
| 147 ASSERT_EQ( |
| 148 static_cast<int>(pac_script.size()), |
| 149 base::WriteFile(pac_file_path, pac_script.c_str(), pac_script.size())); |
| 150 |
| 151 command_line->AppendSwitchASCII( |
| 152 switches::kProxyPacUrl, net::FilePathToFileURL(pac_file_path).spec()); |
| 153 } |
| 154 |
| 155 protected: |
| 156 base::ScopedTempDir temp_dir_; |
| 157 }; |
| 158 |
| 159 // Make sure the system URLRequestContext can hadle fetching PAC scripts from |
| 160 // file URLs. |
| 161 IN_PROC_BROWSER_TEST_F(IOThreadPacTestWithFileURL, FilePac) { |
| 162 TestURLFetcherDelegate fetcher_delegate; |
| 163 std::unique_ptr<net::URLFetcher> fetcher = |
| 164 net::URLFetcher::Create(GURL("http://foo:12345/echoheader?Foo"), |
| 165 net::URLFetcher::GET, &fetcher_delegate); |
| 166 fetcher->AddExtraRequestHeader("Foo: Bar"); |
| 167 fetcher->SetRequestContext( |
| 168 g_browser_process->io_thread()->system_url_request_context_getter()); |
| 169 fetcher->Start(); |
| 170 fetcher_delegate.WaitForCompletion(); |
| 171 EXPECT_EQ(200, fetcher->GetResponseCode()); |
| 172 std::string response; |
| 173 ASSERT_TRUE(fetcher->GetResponseAsString(&response)); |
| 174 EXPECT_EQ("Bar", response); |
| 175 } |
| 176 |
| 177 } // namespace |
OLD | NEW |