| 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/net/system_network_context_manager.h" | |
| 6 | |
| 7 #include "base/test/scoped_feature_list.h" | |
| 8 #include "chrome/test/base/in_process_browser_test.h" | |
| 9 #include "content/public/common/content_features.h" | |
| 10 #include "content/public/common/content_switches.h" | |
| 11 #include "content/public/common/network_service.mojom.h" | |
| 12 #include "content/public/common/resource_response.h" | |
| 13 #include "content/public/common/resource_response_info.h" | |
| 14 #include "content/public/common/url_loader.mojom.h" | |
| 15 #include "content/public/common/url_loader_factory.mojom.h" | |
| 16 #include "content/public/test/test_url_loader_client.h" | |
| 17 #include "mojo/common/data_pipe_utils.h" | |
| 18 #include "net/base/net_errors.h" | |
| 19 #include "net/http/http_response_headers.h" | |
| 20 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 21 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 enum class NetworkServiceState { | |
| 26 kDisabled, | |
| 27 kEnabled, | |
| 28 }; | |
| 29 | |
| 30 class SystemNetworkContextManagerTest | |
| 31 : public InProcessBrowserTest, | |
| 32 public testing::WithParamInterface<NetworkServiceState> { | |
| 33 public: | |
| 34 SystemNetworkContextManagerTest() { | |
| 35 EXPECT_TRUE(embedded_test_server()->Start()); | |
| 36 } | |
| 37 | |
| 38 ~SystemNetworkContextManagerTest() override {} | |
| 39 | |
| 40 void SetUpInProcessBrowserTestFixture() override { | |
| 41 feature_list_.InitAndEnableFeature(features::kNetworkService); | |
| 42 } | |
| 43 | |
| 44 void SetUpOnMainThread() override { | |
| 45 SystemNetworkContextManager::Context()->CreateURLLoaderFactory( | |
| 46 MakeRequest(&loader_factory_), 0); | |
| 47 } | |
| 48 | |
| 49 content::mojom::URLLoaderFactory* loader_factory() { | |
| 50 return loader_factory_.get(); | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 content::mojom::URLLoaderFactoryPtr loader_factory_; | |
| 55 base::test::ScopedFeatureList feature_list_; | |
| 56 }; | |
| 57 | |
| 58 IN_PROC_BROWSER_TEST_P(SystemNetworkContextManagerTest, BasicRequest) { | |
| 59 content::mojom::URLLoaderAssociatedPtr loader; | |
| 60 content::ResourceRequest request; | |
| 61 content::TestURLLoaderClient client; | |
| 62 request.url = embedded_test_server()->GetURL("/echo"); | |
| 63 request.method = "GET"; | |
| 64 request.request_initiator = url::Origin(); | |
| 65 loader_factory()->CreateLoaderAndStart( | |
| 66 mojo::MakeRequest(&loader), 2, 1, content::mojom::kURLLoadOptionNone, | |
| 67 request, client.CreateInterfacePtr(), | |
| 68 net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS)); | |
| 69 client.RunUntilResponseReceived(); | |
| 70 ASSERT_TRUE(client.response_head().headers); | |
| 71 EXPECT_EQ(200, client.response_head().headers->response_code()); | |
| 72 | |
| 73 client.RunUntilResponseBodyArrived(); | |
| 74 // TODO(mmenke): Is blocking the UI Thread while reading the response really | |
| 75 // the best way to test requests in a browser test? | |
| 76 std::string response_body; | |
| 77 EXPECT_TRUE(mojo::common::BlockingCopyToString(client.response_body_release(), | |
| 78 &response_body)); | |
| 79 EXPECT_EQ("Echo", response_body); | |
| 80 | |
| 81 client.RunUntilComplete(); | |
| 82 EXPECT_EQ(net::OK, client.completion_status().error_code); | |
| 83 } | |
| 84 | |
| 85 INSTANTIATE_TEST_CASE_P( | |
| 86 /* no prefix */, | |
| 87 SystemNetworkContextManagerTest, | |
| 88 ::testing::Values(NetworkServiceState::kDisabled, | |
| 89 NetworkServiceState::kEnabled)); | |
| 90 | |
| 91 } // namespace | |
| OLD | NEW |