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

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: fix unittests 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 19 matching lines...) Expand all
66 bool ShouldBeginRequest(const std::string& method, 65 bool ShouldBeginRequest(const std::string& method,
67 const GURL& url, 66 const GURL& url,
68 ResourceType resource_type, 67 ResourceType resource_type,
69 ResourceContext* resource_context) override { 68 ResourceContext* resource_context) override {
70 return false; 69 return false;
71 } 70 }
72 71
73 DISALLOW_COPY_AND_ASSIGN(RejectingResourceDispatcherHostDelegate); 72 DISALLOW_COPY_AND_ASSIGN(RejectingResourceDispatcherHostDelegate);
74 }; 73 };
75 74
75 } // namespace
76
76 // The test parameter is the number of bytes allocated for the buffer in the 77 // 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 78 // data pipe, for testing the case where the allocated size is smaller than the
78 // size the mime sniffer *implicitly* requires. 79 // size the mime sniffer *implicitly* requires.
79 class URLLoaderFactoryImplTest : public ::testing::TestWithParam<size_t> { 80 class URLLoaderFactoryImplTest : public ::testing::TestWithParam<size_t> {
80 public: 81 public:
81 URLLoaderFactoryImplTest() 82 URLLoaderFactoryImplTest()
82 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), 83 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
83 browser_context_(new TestBrowserContext()), 84 browser_context_(new TestBrowserContext()),
84 resource_message_filter_(new ResourceMessageFilter( 85 resource_message_filter_(new ResourceMessageFilter(
85 kChildId, 86 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, 87 nullptr,
93 nullptr, 88 nullptr,
94 nullptr, 89 nullptr,
95 nullptr, 90 nullptr,
96 base::Bind(&URLLoaderFactoryImplTest::GetContexts, 91 base::Bind(&URLLoaderFactoryImplTest::GetContexts,
97 base::Unretained(this)))) { 92 base::Unretained(this)))) {
93 resource_message_filter_->OnFilterAdded(nullptr);
98 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); 94 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam());
99 rdh_.SetLoaderDelegate(&loader_deleate_); 95 rdh_.SetLoaderDelegate(&loader_deleate_);
100 96
101 URLLoaderFactoryImpl::Create(resource_message_filter_, 97 URLLoaderFactoryImpl::Create(
102 mojo::GetProxy(&factory_)); 98 resource_message_filter_->requester_info()->clone(),
99 mojo::GetProxy(&factory_));
103 100
104 // Calling this function creates a request context. 101 // Calling this function creates a request context.
105 browser_context_->GetResourceContext()->GetRequestContext(); 102 browser_context_->GetResourceContext()->GetRequestContext();
106 base::RunLoop().RunUntilIdle(); 103 base::RunLoop().RunUntilIdle();
107 } 104 }
108 105
109 ~URLLoaderFactoryImplTest() override { 106 ~URLLoaderFactoryImplTest() override {
110 rdh_.SetDelegate(nullptr); 107 rdh_.SetDelegate(nullptr);
111 net::URLRequestFilter::GetInstance()->ClearHandlers(); 108 net::URLRequestFilter::GetInstance()->ClearHandlers();
112 109
(...skipping 29 matching lines...) Expand all
142 mojom::URLLoaderAssociatedPtr loader; 139 mojom::URLLoaderAssociatedPtr loader;
143 base::FilePath root; 140 base::FilePath root;
144 PathService::Get(DIR_TEST_DATA, &root); 141 PathService::Get(DIR_TEST_DATA, &root);
145 net::URLRequestMockHTTPJob::AddUrlHandlers(root, 142 net::URLRequestMockHTTPJob::AddUrlHandlers(root,
146 BrowserThread::GetBlockingPool()); 143 BrowserThread::GetBlockingPool());
147 ResourceRequest request; 144 ResourceRequest request;
148 TestURLLoaderClient client; 145 TestURLLoaderClient client;
149 // Assume the file contents is small enough to be stored in the data pipe. 146 // Assume the file contents is small enough to be stored in the data pipe.
150 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); 147 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html");
151 request.method = "GET"; 148 request.method = "GET";
152 request.is_main_frame = true; 149 // |resource_type| can't be a frame type. It is because when PlzNavigate is
150 // enabled, the url scheme of frame type requests from the renderer process
151 // must be blob scheme.
152 request.resource_type = RESOURCE_TYPE_XHR;
153 // Need to set |request_initiator| for non main frame type request.
154 request.request_initiator = url::Origin(GURL("http://localhost/"));
153 factory_->CreateLoaderAndStart( 155 factory_->CreateLoaderAndStart(
154 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, 156 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId,
155 kRequestId, request, 157 kRequestId, request,
156 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); 158 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group()));
157 159
158 ASSERT_FALSE(client.has_received_response()); 160 ASSERT_FALSE(client.has_received_response());
159 ASSERT_FALSE(client.response_body().is_valid()); 161 ASSERT_FALSE(client.response_body().is_valid());
160 ASSERT_FALSE(client.has_received_completion()); 162 ASSERT_FALSE(client.has_received_completion());
161 163
162 client.RunUntilResponseReceived(); 164 client.RunUntilResponseReceived();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 209
208 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { 210 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) {
209 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); 211 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true);
210 mojom::URLLoaderAssociatedPtr loader; 212 mojom::URLLoaderAssociatedPtr loader;
211 ResourceRequest request; 213 ResourceRequest request;
212 TestURLLoaderClient client; 214 TestURLLoaderClient client;
213 net::URLRequestFailedJob::AddUrlHandler(); 215 net::URLRequestFailedJob::AddUrlHandler();
214 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( 216 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
215 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); 217 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT);
216 request.method = "GET"; 218 request.method = "GET";
219 // |resource_type| can't be a frame type. It is because when PlzNavigate is
220 // enabled, the url scheme of frame type requests from the renderer process
221 // must be blob scheme.
222 request.resource_type = RESOURCE_TYPE_XHR;
223 // Need to set |request_initiator| for non main frame type request.
224 request.request_initiator = url::Origin(GURL("http://localhost/"));
217 factory_->CreateLoaderAndStart( 225 factory_->CreateLoaderAndStart(
218 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, 226 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request,
219 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); 227 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group()));
220 228
221 client.RunUntilComplete(); 229 client.RunUntilComplete();
222 ASSERT_FALSE(client.has_received_response()); 230 ASSERT_FALSE(client.has_received_response());
223 ASSERT_FALSE(client.response_body().is_valid()); 231 ASSERT_FALSE(client.response_body().is_valid());
224 232
225 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); 233 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code);
226 } 234 }
227 235
228 // This test tests a case where resource loading is cancelled before started. 236 // This test tests a case where resource loading is cancelled before started.
229 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { 237 TEST_P(URLLoaderFactoryImplTest, InvalidURL) {
230 mojom::URLLoaderAssociatedPtr loader; 238 mojom::URLLoaderAssociatedPtr loader;
231 ResourceRequest request; 239 ResourceRequest request;
232 TestURLLoaderClient client; 240 TestURLLoaderClient client;
233 request.url = GURL(); 241 request.url = GURL();
234 request.method = "GET"; 242 request.method = "GET";
243 // |resource_type| can't be a frame type. It is because when PlzNavigate is
244 // enabled, the url scheme of frame type requests from the renderer process
245 // must be blob scheme.
246 request.resource_type = RESOURCE_TYPE_XHR;
247 // Need to set |request_initiator| for non main frame type request.
248 request.request_initiator = url::Origin(GURL("http://localhost/"));
235 ASSERT_FALSE(request.url.is_valid()); 249 ASSERT_FALSE(request.url.is_valid());
236 factory_->CreateLoaderAndStart( 250 factory_->CreateLoaderAndStart(
237 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, 251 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request,
238 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); 252 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group()));
239 253
240 client.RunUntilComplete(); 254 client.RunUntilComplete();
241 ASSERT_FALSE(client.has_received_response()); 255 ASSERT_FALSE(client.has_received_response());
242 ASSERT_FALSE(client.response_body().is_valid()); 256 ASSERT_FALSE(client.response_body().is_valid());
243 257
244 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); 258 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code);
245 } 259 }
246 260
247 // This test tests a case where resource loading is cancelled before started. 261 // This test tests a case where resource loading is cancelled before started.
248 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { 262 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) {
249 mojom::URLLoaderAssociatedPtr loader; 263 mojom::URLLoaderAssociatedPtr loader;
250 RejectingResourceDispatcherHostDelegate rdh_delegate; 264 RejectingResourceDispatcherHostDelegate rdh_delegate;
251 rdh_.SetDelegate(&rdh_delegate); 265 rdh_.SetDelegate(&rdh_delegate);
252 ResourceRequest request; 266 ResourceRequest request;
253 TestURLLoaderClient client; 267 TestURLLoaderClient client;
254 request.url = GURL("http://localhost/"); 268 request.url = GURL("http://localhost/");
255 request.method = "GET"; 269 request.method = "GET";
270 // |resource_type| can't be a frame type. It is because when PlzNavigate is
271 // enabled, the url scheme of frame type requests from the renderer process
272 // must be blob scheme.
273 request.resource_type = RESOURCE_TYPE_XHR;
274 // Need to set |request_initiator| for non main frame type request.
275 request.request_initiator = url::Origin(GURL("http://localhost/"));
256 factory_->CreateLoaderAndStart( 276 factory_->CreateLoaderAndStart(
257 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, 277 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request,
258 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); 278 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group()));
259 279
260 client.RunUntilComplete(); 280 client.RunUntilComplete();
261 rdh_.SetDelegate(nullptr); 281 rdh_.SetDelegate(nullptr);
262 282
263 ASSERT_FALSE(client.has_received_response()); 283 ASSERT_FALSE(client.has_received_response());
264 ASSERT_FALSE(client.response_body().is_valid()); 284 ASSERT_FALSE(client.response_body().is_valid());
265 285
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 437
418 loader = nullptr; 438 loader = nullptr;
419 base::RunLoop().RunUntilIdle(); 439 base::RunLoop().RunUntilIdle();
420 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); 440 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId)));
421 } 441 }
422 442
423 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, 443 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest,
424 URLLoaderFactoryImplTest, 444 URLLoaderFactoryImplTest,
425 ::testing::Values(128, 32 * 1024)); 445 ::testing::Values(128, 32 * 1024));
426 446
427 } // namespace
428
429 } // namespace content 447 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698