Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: content/browser/loader/url_loader_factory_impl_unittest.cc

Issue 2481093003: Introduce ResourceRequesterInfo to abstract the requester of resource request (Closed)
Patch Set: incorporated mmemke's comment Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "content/browser/loader/url_loader_factory_impl.h" 5 #include "content/browser/loader/url_loader_factory_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
(...skipping 15 matching lines...) Expand all
26 #include "content/browser/loader/resource_message_filter.h" 26 #include "content/browser/loader/resource_message_filter.h"
27 #include "content/browser/loader/resource_request_info_impl.h" 27 #include "content/browser/loader/resource_request_info_impl.h"
28 #include "content/browser/loader/test_url_loader_client.h" 28 #include "content/browser/loader/test_url_loader_client.h"
29 #include "content/browser/loader_delegate_impl.h" 29 #include "content/browser/loader_delegate_impl.h"
30 #include "content/common/resource_request.h" 30 #include "content/common/resource_request.h"
31 #include "content/common/resource_request_completion_status.h" 31 #include "content/common/resource_request_completion_status.h"
32 #include "content/common/url_loader.mojom.h" 32 #include "content/common/url_loader.mojom.h"
33 #include "content/public/browser/resource_context.h" 33 #include "content/public/browser/resource_context.h"
34 #include "content/public/browser/resource_dispatcher_host_delegate.h" 34 #include "content/public/browser/resource_dispatcher_host_delegate.h"
35 #include "content/public/common/content_paths.h" 35 #include "content/public/common/content_paths.h"
36 #include "content/public/common/process_type.h"
37 #include "content/public/test/test_browser_context.h" 36 #include "content/public/test/test_browser_context.h"
38 #include "content/public/test/test_browser_thread_bundle.h" 37 #include "content/public/test/test_browser_thread_bundle.h"
39 #include "mojo/public/c/system/data_pipe.h" 38 #include "mojo/public/c/system/data_pipe.h"
40 #include "mojo/public/c/system/types.h" 39 #include "mojo/public/c/system/types.h"
41 #include "mojo/public/cpp/bindings/binding.h" 40 #include "mojo/public/cpp/bindings/binding.h"
42 #include "mojo/public/cpp/system/data_pipe.h" 41 #include "mojo/public/cpp/system/data_pipe.h"
43 #include "net/base/io_buffer.h" 42 #include "net/base/io_buffer.h"
44 #include "net/base/net_errors.h" 43 #include "net/base/net_errors.h"
45 #include "net/http/http_response_headers.h" 44 #include "net/http/http_response_headers.h"
46 #include "net/http/http_response_info.h" 45 #include "net/http/http_response_info.h"
(...skipping 29 matching lines...) Expand all
76 // The test parameter is the number of bytes allocated for the buffer in the 75 // The test parameter is the number of bytes allocated for the buffer in the
77 // data pipe, for testing the case where the allocated size is smaller than the 76 // data pipe, for testing the case where the allocated size is smaller than the
78 // size the mime sniffer *implicitly* requires. 77 // size the mime sniffer *implicitly* requires.
79 class URLLoaderFactoryImplTest : public ::testing::TestWithParam<size_t> { 78 class URLLoaderFactoryImplTest : public ::testing::TestWithParam<size_t> {
80 public: 79 public:
81 URLLoaderFactoryImplTest() 80 URLLoaderFactoryImplTest()
82 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), 81 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
83 browser_context_(new TestBrowserContext()), 82 browser_context_(new TestBrowserContext()),
84 resource_message_filter_(new ResourceMessageFilter( 83 resource_message_filter_(new ResourceMessageFilter(
85 kChildId, 84 kChildId,
86 // If browser side navigation is enabled then
87 // ResourceDispatcherHostImpl prevents main frame URL requests from
88 // the renderer. Ensure that these checks don't trip us up by
89 // setting the process type in ResourceMessageFilter as
90 // PROCESS_TYPE_UNKNOWN.
91 PROCESS_TYPE_UNKNOWN,
92 nullptr, 85 nullptr,
93 nullptr, 86 nullptr,
94 nullptr, 87 nullptr,
95 nullptr, 88 nullptr,
96 base::Bind(&URLLoaderFactoryImplTest::GetContexts, 89 base::Bind(&URLLoaderFactoryImplTest::GetContexts,
97 base::Unretained(this)))) { 90 base::Unretained(this)))) {
98 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); 91 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam());
99 rdh_.SetLoaderDelegate(&loader_deleate_); 92 rdh_.SetLoaderDelegate(&loader_deleate_);
100 93
101 URLLoaderFactoryImpl::Create(resource_message_filter_, 94 URLLoaderFactoryImpl::Create(ResourceRequesterInfo::CreateForRenderer(
95 resource_message_filter_.get()),
102 mojo::GetProxy(&factory_)); 96 mojo::GetProxy(&factory_));
103 97
104 // Calling this function creates a request context. 98 // Calling this function creates a request context.
105 browser_context_->GetResourceContext()->GetRequestContext(); 99 browser_context_->GetResourceContext()->GetRequestContext();
106 base::RunLoop().RunUntilIdle(); 100 base::RunLoop().RunUntilIdle();
107 } 101 }
108 102
109 ~URLLoaderFactoryImplTest() override { 103 ~URLLoaderFactoryImplTest() override {
110 rdh_.SetDelegate(nullptr); 104 rdh_.SetDelegate(nullptr);
111 net::URLRequestFilter::GetInstance()->ClearHandlers(); 105 net::URLRequestFilter::GetInstance()->ClearHandlers();
(...skipping 29 matching lines...) Expand all
141 mojom::URLLoaderAssociatedPtr loader; 135 mojom::URLLoaderAssociatedPtr loader;
142 base::FilePath root; 136 base::FilePath root;
143 PathService::Get(DIR_TEST_DATA, &root); 137 PathService::Get(DIR_TEST_DATA, &root);
144 net::URLRequestMockHTTPJob::AddUrlHandlers(root, 138 net::URLRequestMockHTTPJob::AddUrlHandlers(root,
145 BrowserThread::GetBlockingPool()); 139 BrowserThread::GetBlockingPool());
146 ResourceRequest request; 140 ResourceRequest request;
147 TestURLLoaderClient client; 141 TestURLLoaderClient client;
148 // Assume the file contents is small enough to be stored in the data pipe. 142 // Assume the file contents is small enough to be stored in the data pipe.
149 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); 143 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html");
150 request.method = "GET"; 144 request.method = "GET";
151 request.is_main_frame = true; 145 // |resource_type| can't be a frame type. It is because when PlzNavigate is
146 // enabled, the url scheme of frame type requests from the renderer process
147 // must be blob scheme.
148 request.resource_type = RESOURCE_TYPE_XHR;
149 // Need to set |request_initiator| for non main frame type request.
150 request.request_initiator = url::Origin(GURL("http://localhost/"));
152 factory_->CreateLoaderAndStart( 151 factory_->CreateLoaderAndStart(
153 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, 152 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId,
154 kRequestId, request, 153 kRequestId, request,
155 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); 154 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group()));
156 155
157 ASSERT_FALSE(client.has_received_response()); 156 ASSERT_FALSE(client.has_received_response());
158 ASSERT_FALSE(client.response_body().is_valid()); 157 ASSERT_FALSE(client.response_body().is_valid());
159 ASSERT_FALSE(client.has_received_completion()); 158 ASSERT_FALSE(client.has_received_completion());
160 159
161 client.RunUntilResponseReceived(); 160 client.RunUntilResponseReceived();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 205
207 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { 206 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) {
208 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); 207 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true);
209 mojom::URLLoaderAssociatedPtr loader; 208 mojom::URLLoaderAssociatedPtr loader;
210 ResourceRequest request; 209 ResourceRequest request;
211 TestURLLoaderClient client; 210 TestURLLoaderClient client;
212 net::URLRequestFailedJob::AddUrlHandler(); 211 net::URLRequestFailedJob::AddUrlHandler();
213 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( 212 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
214 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); 213 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT);
215 request.method = "GET"; 214 request.method = "GET";
215 // |resource_type| can't be a frame type. It is because when PlzNavigate is
216 // enabled, the url scheme of frame type requests from the renderer process
217 // must be blob scheme.
218 request.resource_type = RESOURCE_TYPE_XHR;
219 // Need to set |request_initiator| for non main frame type request.
220 request.request_initiator = url::Origin(GURL("http://localhost/"));
216 factory_->CreateLoaderAndStart( 221 factory_->CreateLoaderAndStart(
217 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, 222 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request,
218 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); 223 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group()));
219 224
220 client.RunUntilComplete(); 225 client.RunUntilComplete();
221 ASSERT_FALSE(client.has_received_response()); 226 ASSERT_FALSE(client.has_received_response());
222 ASSERT_FALSE(client.response_body().is_valid()); 227 ASSERT_FALSE(client.response_body().is_valid());
223 228
224 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); 229 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code);
225 } 230 }
226 231
227 // This test tests a case where resource loading is cancelled before started. 232 // This test tests a case where resource loading is cancelled before started.
228 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { 233 TEST_P(URLLoaderFactoryImplTest, InvalidURL) {
229 mojom::URLLoaderAssociatedPtr loader; 234 mojom::URLLoaderAssociatedPtr loader;
230 ResourceRequest request; 235 ResourceRequest request;
231 TestURLLoaderClient client; 236 TestURLLoaderClient client;
232 request.url = GURL(); 237 request.url = GURL();
233 request.method = "GET"; 238 request.method = "GET";
239 // |resource_type| can't be a frame type. It is because when PlzNavigate is
240 // enabled, the url scheme of frame type requests from the renderer process
241 // must be blob scheme.
242 request.resource_type = RESOURCE_TYPE_XHR;
243 // Need to set |request_initiator| for non main frame type request.
244 request.request_initiator = url::Origin(GURL("http://localhost/"));
234 ASSERT_FALSE(request.url.is_valid()); 245 ASSERT_FALSE(request.url.is_valid());
235 factory_->CreateLoaderAndStart( 246 factory_->CreateLoaderAndStart(
236 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, 247 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request,
237 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); 248 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group()));
238 249
239 client.RunUntilComplete(); 250 client.RunUntilComplete();
240 ASSERT_FALSE(client.has_received_response()); 251 ASSERT_FALSE(client.has_received_response());
241 ASSERT_FALSE(client.response_body().is_valid()); 252 ASSERT_FALSE(client.response_body().is_valid());
242 253
243 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); 254 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code);
244 } 255 }
245 256
246 // This test tests a case where resource loading is cancelled before started. 257 // This test tests a case where resource loading is cancelled before started.
247 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { 258 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) {
248 mojom::URLLoaderAssociatedPtr loader; 259 mojom::URLLoaderAssociatedPtr loader;
249 RejectingResourceDispatcherHostDelegate rdh_delegate; 260 RejectingResourceDispatcherHostDelegate rdh_delegate;
250 rdh_.SetDelegate(&rdh_delegate); 261 rdh_.SetDelegate(&rdh_delegate);
251 ResourceRequest request; 262 ResourceRequest request;
252 TestURLLoaderClient client; 263 TestURLLoaderClient client;
253 request.url = GURL("http://localhost/"); 264 request.url = GURL("http://localhost/");
254 request.method = "GET"; 265 request.method = "GET";
266 // |resource_type| can't be a frame type. It is because when PlzNavigate is
267 // enabled, the url scheme of frame type requests from the renderer process
268 // must be blob scheme.
269 request.resource_type = RESOURCE_TYPE_XHR;
270 // Need to set |request_initiator| for non main frame type request.
271 request.request_initiator = url::Origin(GURL("http://localhost/"));
255 factory_->CreateLoaderAndStart( 272 factory_->CreateLoaderAndStart(
256 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, 273 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request,
257 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); 274 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group()));
258 275
259 client.RunUntilComplete(); 276 client.RunUntilComplete();
260 rdh_.SetDelegate(nullptr); 277 rdh_.SetDelegate(nullptr);
261 278
262 ASSERT_FALSE(client.has_received_response()); 279 ASSERT_FALSE(client.has_received_response());
263 ASSERT_FALSE(client.response_body().is_valid()); 280 ASSERT_FALSE(client.response_body().is_valid());
264 281
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); 405 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code);
389 } 406 }
390 407
391 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, 408 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest,
392 URLLoaderFactoryImplTest, 409 URLLoaderFactoryImplTest,
393 ::testing::Values(128, 32 * 1024)); 410 ::testing::Values(128, 32 * 1024));
394 411
395 } // namespace 412 } // namespace
396 413
397 } // namespace content 414 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698