| 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 "net/url_request/url_request_context_builder_v8.h" | |
| 6 | |
| 7 #include "base/run_loop.h" | |
| 8 #include "base/strings/stringprintf.h" | |
| 9 #include "net/base/host_port_pair.h" | |
| 10 #include "net/proxy/proxy_config.h" | |
| 11 #include "net/proxy/proxy_config_service_fixed.h" | |
| 12 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 13 #include "net/test/embedded_test_server/http_request.h" | |
| 14 #include "net/test/embedded_test_server/http_response.h" | |
| 15 #include "net/test/embedded_test_server/simple_connection_listener.h" | |
| 16 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | |
| 17 #include "net/url_request/url_request.h" | |
| 18 #include "net/url_request/url_request_context.h" | |
| 19 #include "net/url_request/url_request_test_util.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 #include "testing/platform_test.h" | |
| 22 #include "url/gurl.h" | |
| 23 | |
| 24 #ifdef ENABLE_NET_MOJO | |
| 25 #include "net/proxy/test_mojo_proxy_resolver_factory.h" | |
| 26 #endif | |
| 27 | |
| 28 namespace net { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 const char kPacPath[] = "/super.pac"; | |
| 33 | |
| 34 // When kPacPath is requested, returns a PAC script that uses the test server | |
| 35 // itself as the proxy. | |
| 36 std::unique_ptr<test_server::HttpResponse> HandlePacRequest( | |
| 37 const test_server::HttpRequest& request) { | |
| 38 if (request.relative_url != kPacPath) | |
| 39 return nullptr; | |
| 40 std::unique_ptr<test_server::BasicHttpResponse> response = | |
| 41 base::MakeUnique<test_server::BasicHttpResponse>(); | |
| 42 response->set_content(base::StringPrintf( | |
| 43 "function FindProxyForURL(url, host) { return 'PROXY %s;'; }", | |
| 44 HostPortPair::FromURL(request.base_url).ToString().c_str())); | |
| 45 response->set_content_type("text/html"); | |
| 46 return std::move(response); | |
| 47 } | |
| 48 | |
| 49 class URLRequestContextBuilderV8Test : public PlatformTest { | |
| 50 protected: | |
| 51 URLRequestContextBuilderV8Test() { | |
| 52 test_server_.RegisterRequestHandler(base::Bind(&HandlePacRequest)); | |
| 53 test_server_.AddDefaultHandlers( | |
| 54 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | |
| 55 } | |
| 56 | |
| 57 EmbeddedTestServer test_server_; | |
| 58 URLRequestContextBuilderV8 builder_; | |
| 59 }; | |
| 60 | |
| 61 TEST_F(URLRequestContextBuilderV8Test, V8InProcess) { | |
| 62 EXPECT_TRUE(test_server_.Start()); | |
| 63 | |
| 64 builder_.set_proxy_config_service(base::MakeUnique<ProxyConfigServiceFixed>( | |
| 65 ProxyConfig::CreateFromCustomPacURL(test_server_.GetURL(kPacPath)))); | |
| 66 std::unique_ptr<URLRequestContext> context(builder_.Build()); | |
| 67 | |
| 68 TestDelegate delegate; | |
| 69 std::unique_ptr<URLRequest> request(context->CreateRequest( | |
| 70 GURL("http://hats:12345/echoheader?Foo"), DEFAULT_PRIORITY, &delegate, | |
| 71 TRAFFIC_ANNOTATION_FOR_TESTS)); | |
| 72 request->SetExtraRequestHeaderByName("Foo", "Bar", false); | |
| 73 request->Start(); | |
| 74 base::RunLoop().Run(); | |
| 75 EXPECT_EQ("Bar", delegate.data_received()); | |
| 76 } | |
| 77 | |
| 78 // Makes sure that pending PAC requests are correctly shutdown during teardown. | |
| 79 TEST_F(URLRequestContextBuilderV8Test, V8InProcessShutdownWithHungRequest) { | |
| 80 test_server::SimpleConnectionListener connection_listener( | |
| 81 1, test_server::SimpleConnectionListener::FAIL_ON_ADDITIONAL_CONNECTIONS); | |
| 82 test_server_.SetConnectionListener(&connection_listener); | |
| 83 EXPECT_TRUE(test_server_.Start()); | |
| 84 | |
| 85 builder_.set_proxy_config_service(base::MakeUnique<ProxyConfigServiceFixed>( | |
| 86 ProxyConfig::CreateFromCustomPacURL(test_server_.GetURL("/hung")))); | |
| 87 | |
| 88 std::unique_ptr<URLRequestContext> context(builder_.Build()); | |
| 89 TestDelegate delegate; | |
| 90 std::unique_ptr<URLRequest> request(context->CreateRequest( | |
| 91 GURL("http://hats:12345/echoheader?Foo"), DEFAULT_PRIORITY, &delegate, | |
| 92 TRAFFIC_ANNOTATION_FOR_TESTS)); | |
| 93 request->Start(); | |
| 94 connection_listener.WaitForConnections(); | |
| 95 | |
| 96 // Have to shut down the test server before |connection_listener| falls out of | |
| 97 // scope. | |
| 98 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | |
| 99 | |
| 100 // Tearing down the URLRequestContext should not cause an AssertNoURLRequests | |
| 101 // failure. | |
| 102 } | |
| 103 | |
| 104 #ifdef ENABLE_NET_MOJO | |
| 105 TEST_F(URLRequestContextBuilderV8Test, MojoProxyResolver) { | |
| 106 EXPECT_TRUE(test_server_.Start()); | |
| 107 TestMojoProxyResolverFactory::GetInstance()->set_resolver_created(false); | |
| 108 | |
| 109 builder_.set_proxy_config_service(base::MakeUnique<ProxyConfigServiceFixed>( | |
| 110 ProxyConfig::CreateFromCustomPacURL(test_server_.GetURL(kPacPath)))); | |
| 111 builder_.set_mojo_proxy_resolver_factory( | |
| 112 TestMojoProxyResolverFactory::GetInstance()); | |
| 113 | |
| 114 std::unique_ptr<URLRequestContext> context(builder_.Build()); | |
| 115 TestDelegate delegate; | |
| 116 std::unique_ptr<URLRequest> request(context->CreateRequest( | |
| 117 GURL("http://hats:12345/echoheader?Foo"), DEFAULT_PRIORITY, &delegate, | |
| 118 TRAFFIC_ANNOTATION_FOR_TESTS)); | |
| 119 request->SetExtraRequestHeaderByName("Foo", "Bar", false); | |
| 120 request->Start(); | |
| 121 base::RunLoop().Run(); | |
| 122 EXPECT_EQ("Bar", delegate.data_received()); | |
| 123 | |
| 124 // Make sure that the Mojo factory was used. | |
| 125 EXPECT_TRUE(TestMojoProxyResolverFactory::GetInstance()->resolver_created()); | |
| 126 } | |
| 127 #endif // ENABLE_NET_MOJO | |
| 128 | |
| 129 } // namespace | |
| 130 | |
| 131 } // namespace net | |
| OLD | NEW |