OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file_util.h" | 5 #include "base/files/file_util.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
10 #include "content/browser/loader/test_url_loader_client.h" | 10 #include "content/browser/loader/test_url_loader_client.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 } | 63 } |
64 | 64 |
65 } // namespace | 65 } // namespace |
66 | 66 |
67 class URLLoaderImplTest : public testing::Test { | 67 class URLLoaderImplTest : public testing::Test { |
68 public: | 68 public: |
69 URLLoaderImplTest() : context_(NetworkContext::CreateForTesting()) {} | 69 URLLoaderImplTest() : context_(NetworkContext::CreateForTesting()) {} |
70 ~URLLoaderImplTest() override {} | 70 ~URLLoaderImplTest() override {} |
71 | 71 |
72 void SetUp() override { | 72 void SetUp() override { |
73 test_server_.ServeFilesFromSourceDirectory( | 73 test_server_.AddDefaultHandlers( |
74 base::FilePath(FILE_PATH_LITERAL("content/test/data"))); | 74 base::FilePath(FILE_PATH_LITERAL("content/test/data"))); |
75 ASSERT_TRUE(test_server_.Start()); | 75 ASSERT_TRUE(test_server_.Start()); |
76 } | 76 } |
77 | 77 |
78 void Load(const GURL& url, | 78 void Load(const GURL& url, |
79 TestURLLoaderClient* client, | 79 TestURLLoaderClient* client, |
80 uint32_t options = 0) { | 80 uint32_t options = 0) { |
81 mojom::URLLoaderAssociatedPtr loader; | 81 mojom::URLLoaderAssociatedPtr loader; |
82 | 82 |
83 ResourceRequest request = | 83 ResourceRequest request = |
84 CreateResourceRequest("GET", RESOURCE_TYPE_MAIN_FRAME, url); | 84 CreateResourceRequest("GET", RESOURCE_TYPE_MAIN_FRAME, url); |
85 | 85 |
86 URLLoaderImpl loader_impl(context(), mojo::MakeIsolatedRequest(&loader), | 86 URLLoaderImpl loader_impl(context(), mojo::MakeIsolatedRequest(&loader), |
87 options, request, client->CreateInterfacePtr(), | 87 options, request, client->CreateInterfacePtr(), |
88 TRAFFIC_ANNOTATION_FOR_TESTS); | 88 TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke
2017/06/22 17:58:50
The fact that we create these on the stack seems w
| |
89 | 89 |
90 client->RunUntilComplete(); | 90 client->RunUntilComplete(); |
91 } | 91 } |
92 | 92 |
93 void LoadAndCompareFile(const std::string& path) { | 93 void LoadAndCompareFile(const std::string& path) { |
94 TestURLLoaderClient client; | 94 TestURLLoaderClient client; |
95 GURL url = test_server()->GetURL(std::string("/") + path); | 95 GURL url = test_server()->GetURL(std::string("/") + path); |
96 Load(url, &client); | 96 Load(url, &client); |
97 | 97 |
98 base::FilePath file; | 98 base::FilePath file; |
99 PathService::Get(content::DIR_TEST_DATA, &file); | 99 PathService::Get(content::DIR_TEST_DATA, &file); |
100 file = file.AppendASCII(path); | 100 file = file.AppendASCII(path); |
101 | 101 |
102 std::string file_contents; | 102 std::string file_contents; |
103 if (!base::ReadFileToString(file, &file_contents)) { | 103 if (!base::ReadFileToString(file, &file_contents)) { |
104 ADD_FAILURE() << "File not found: " << file.value(); | 104 ADD_FAILURE() << "File not found: " << file.value(); |
105 return; | 105 return; |
106 } | 106 } |
107 | 107 |
108 std::string data = | 108 std::string data = |
109 ReadData(client.response_body().value(), file_contents.size()); | 109 ReadData(client.response_body().value(), file_contents.size()); |
110 CHECK_EQ(data, file_contents); | 110 CHECK_EQ(data, file_contents); |
111 } | 111 } |
112 | 112 |
113 net::EmbeddedTestServer* test_server() { return &test_server_; } | 113 net::EmbeddedTestServer* test_server() { return &test_server_; } |
114 NetworkContext* context() { return context_.get(); } | 114 NetworkContext* context() { return context_.get(); } |
115 void DestroyContext() { context_.reset(); } | |
115 | 116 |
116 private: | 117 private: |
117 base::MessageLoopForIO message_loop_; | 118 base::MessageLoopForIO message_loop_; |
118 net::EmbeddedTestServer test_server_; | 119 net::EmbeddedTestServer test_server_; |
119 std::unique_ptr<NetworkContext> context_; | 120 std::unique_ptr<NetworkContext> context_; |
120 }; | 121 }; |
121 | 122 |
122 TEST_F(URLLoaderImplTest, Basic) { | 123 TEST_F(URLLoaderImplTest, Basic) { |
123 LoadAndCompareFile("simple_page.html"); | 124 LoadAndCompareFile("simple_page.html"); |
124 } | 125 } |
(...skipping 23 matching lines...) Expand all Loading... | |
148 https_server.ServeFilesFromSourceDirectory( | 149 https_server.ServeFilesFromSourceDirectory( |
149 base::FilePath(FILE_PATH_LITERAL("content/test/data"))); | 150 base::FilePath(FILE_PATH_LITERAL("content/test/data"))); |
150 ASSERT_TRUE(https_server.Start()); | 151 ASSERT_TRUE(https_server.Start()); |
151 | 152 |
152 TestURLLoaderClient client; | 153 TestURLLoaderClient client; |
153 GURL url = https_server.GetURL("/simple_page.html"); | 154 GURL url = https_server.GetURL("/simple_page.html"); |
154 Load(url, &client, 0); | 155 Load(url, &client, 0); |
155 ASSERT_FALSE(!!client.ssl_info()); | 156 ASSERT_FALSE(!!client.ssl_info()); |
156 } | 157 } |
157 | 158 |
159 TEST_F(URLLoaderImplTest, DestroyContextWithLiveRequest) { | |
160 TestURLLoaderClient client; | |
161 GURL url = test_server()->GetURL("/hung-after-headers"); | |
162 ResourceRequest request = | |
163 CreateResourceRequest("GET", RESOURCE_TYPE_MAIN_FRAME, url); | |
164 | |
165 mojom::URLLoaderAssociatedPtr loader; | |
166 // The loader is implicitly owned by the client and the NetworkContext, so | |
167 // don't hold on to a pointer to it. | |
168 base::WeakPtr<URLLoaderImpl> loader_impl = | |
169 (new URLLoaderImpl(context(), mojo::MakeIsolatedRequest(&loader), 0, | |
170 request, client.CreateInterfacePtr(), | |
171 TRAFFIC_ANNOTATION_FOR_TESTS)) | |
172 ->GetWeakPtrForTests(); | |
173 | |
174 client.RunUntilResponseReceived(); | |
175 EXPECT_TRUE(client.has_received_response()); | |
176 EXPECT_FALSE(client.has_received_completion()); | |
177 | |
178 // Request hasn't completed, so the loader should not have been destroyed. | |
179 EXPECT_TRUE(loader_impl); | |
180 | |
181 // Destroying the context should result in destroying the loader and the | |
182 // client receiving a connection error. | |
183 DestroyContext(); | |
184 EXPECT_FALSE(loader_impl); | |
185 | |
186 client.RunUntilConnectionError(); | |
187 EXPECT_FALSE(client.has_received_completion()); | |
188 EXPECT_EQ(0u, client.download_data_length()); | |
189 } | |
190 | |
158 } // namespace content | 191 } // namespace content |
OLD | NEW |