| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/loader/url_loader_factory_impl.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 #include <utility> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/files/file_path.h" |
| 14 #include "base/location.h" |
| 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/path_service.h" |
| 19 #include "base/run_loop.h" |
| 20 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 21 #include "content/browser/loader/resource_message_filter.h" |
| 22 #include "content/browser/loader/url_loader_factory_impl.h" |
| 23 #include "content/common/resource_request.h" |
| 24 #include "content/common/resource_request_completion_status.h" |
| 25 #include "content/common/url_loader.mojom.h" |
| 26 #include "content/common/url_loader_factory.mojom.h" |
| 27 #include "content/public/browser/resource_context.h" |
| 28 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 29 #include "content/public/common/content_paths.h" |
| 30 #include "content/public/test/test_browser_context.h" |
| 31 #include "content/public/test/test_browser_thread_bundle.h" |
| 32 #include "mojo/public/c/system/data_pipe.h" |
| 33 #include "mojo/public/c/system/types.h" |
| 34 #include "mojo/public/cpp/system/data_pipe.h" |
| 35 #include "net/base/io_buffer.h" |
| 36 #include "net/base/net_errors.h" |
| 37 #include "net/http/http_response_headers.h" |
| 38 #include "net/http/http_response_info.h" |
| 39 #include "net/http/http_status_code.h" |
| 40 #include "net/http/http_util.h" |
| 41 #include "net/test/url_request/url_request_failed_job.h" |
| 42 #include "net/test/url_request/url_request_mock_http_job.h" |
| 43 #include "net/url_request/url_request_filter.h" |
| 44 #include "testing/gtest/include/gtest/gtest.h" |
| 45 #include "url/gurl.h" |
| 46 |
| 47 namespace content { |
| 48 |
| 49 namespace { |
| 50 |
| 51 class FakeURLLoaderClient final : public mojom::URLLoaderClient { |
| 52 public: |
| 53 FakeURLLoaderClient() : binding_(this) {} |
| 54 void OnReceiveResponse(const ResourceResponseHead& response_head) override { |
| 55 has_received_response_ = true; |
| 56 response_head_ = response_head; |
| 57 if (!quit_closure_.Equals(base::Closure())) |
| 58 quit_closure_.Run(); |
| 59 } |
| 60 void OnStartLoadingResponseBody( |
| 61 mojo::ScopedDataPipeConsumerHandle body) override { |
| 62 response_body_ = std::move(body); |
| 63 if (!quit_closure_.Equals(base::Closure())) |
| 64 quit_closure_.Run(); |
| 65 } |
| 66 void OnComplete(const ResourceRequestCompletionStatus& status) override { |
| 67 has_received_completion_ = true; |
| 68 completion_status_ = status; |
| 69 if (!quit_closure_.Equals(base::Closure())) |
| 70 quit_closure_.Run(); |
| 71 } |
| 72 |
| 73 bool has_received_response() const { return has_received_response_; } |
| 74 bool has_received_completion() const { return has_received_completion_; } |
| 75 const ResourceResponseHead& response_head() const { return response_head_; } |
| 76 mojo::DataPipeConsumerHandle response_body() { return response_body_.get(); } |
| 77 const ResourceRequestCompletionStatus& completion_status() const { |
| 78 return completion_status_; |
| 79 } |
| 80 |
| 81 mojom::URLLoaderClientPtr CreateInterfacePtrAndBind() { |
| 82 return binding_.CreateInterfacePtrAndBind(); |
| 83 } |
| 84 |
| 85 void set_quit_closure(const base::Closure& quit_closure) { |
| 86 quit_closure_ = quit_closure; |
| 87 } |
| 88 |
| 89 private: |
| 90 mojo::Binding<mojom::URLLoaderClient> binding_; |
| 91 ResourceResponseHead response_head_; |
| 92 mojo::ScopedDataPipeConsumerHandle response_body_; |
| 93 ResourceRequestCompletionStatus completion_status_; |
| 94 bool has_received_response_ = false; |
| 95 bool has_received_completion_ = false; |
| 96 base::Closure quit_closure_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(FakeURLLoaderClient); |
| 99 }; |
| 100 |
| 101 class RejectingResourceDispatcherHostDelegate final |
| 102 : public ResourceDispatcherHostDelegate { |
| 103 public: |
| 104 RejectingResourceDispatcherHostDelegate() {} |
| 105 bool ShouldBeginRequest(const std::string& method, |
| 106 const GURL& url, |
| 107 ResourceType resource_type, |
| 108 ResourceContext* resource_context) override { |
| 109 return false; |
| 110 } |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(RejectingResourceDispatcherHostDelegate); |
| 113 }; |
| 114 |
| 115 class TestURLRequestJobForBrokenBody final : public net::URLRequestJob { |
| 116 public: |
| 117 TestURLRequestJobForBrokenBody(net::URLRequest* request, |
| 118 net::NetworkDelegate* network_delegate) |
| 119 : URLRequestJob(request, network_delegate), weak_factory_(this) {} |
| 120 |
| 121 void Start() override { |
| 122 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 123 FROM_HERE, base::Bind(&TestURLRequestJobForBrokenBody::StartAsync, |
| 124 weak_factory_.GetWeakPtr())); |
| 125 } |
| 126 |
| 127 int ReadRawData(net::IOBuffer* buf, int buf_size) override { |
| 128 DCHECK_GT(buf_size, 0); |
| 129 if (++counter_ > 1) |
| 130 return net::ERR_FAILED; |
| 131 buf->data()[0] = 'a'; |
| 132 return 1; |
| 133 } |
| 134 |
| 135 int GetResponseCode() const override { return 200; } |
| 136 |
| 137 void GetResponseInfo(net::HttpResponseInfo* info) override { |
| 138 std::string raw_headers; |
| 139 raw_headers.append( |
| 140 "HTTP/1.1 200 OK\n" |
| 141 "Content-type: text/plain\n"); |
| 142 info->headers = |
| 143 new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( |
| 144 raw_headers.c_str(), static_cast<int>(raw_headers.length()))); |
| 145 } |
| 146 |
| 147 private: |
| 148 void StartAsync() { NotifyHeadersComplete(); } |
| 149 |
| 150 int counter_ = 0; |
| 151 base::WeakPtrFactory<TestURLRequestJobForBrokenBody> weak_factory_; |
| 152 |
| 153 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJobForBrokenBody); |
| 154 }; |
| 155 |
| 156 class TestInterceptorForBrokenBody final : public net::URLRequestInterceptor { |
| 157 public: |
| 158 explicit TestInterceptorForBrokenBody(const GURL& url) : url_(url) {} |
| 159 |
| 160 net::URLRequestJob* MaybeInterceptRequest( |
| 161 net::URLRequest* request, |
| 162 net::NetworkDelegate* network_delegate) const override { |
| 163 if (request->url() != url_) |
| 164 return nullptr; |
| 165 |
| 166 return new TestURLRequestJobForBrokenBody(request, network_delegate); |
| 167 } |
| 168 |
| 169 private: |
| 170 const GURL url_; |
| 171 |
| 172 DISALLOW_COPY_AND_ASSIGN(TestInterceptorForBrokenBody); |
| 173 }; |
| 174 |
| 175 class URLLoaderFactoryImplTest : public ::testing::Test { |
| 176 public: |
| 177 URLLoaderFactoryImplTest() |
| 178 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 179 browser_context_(new TestBrowserContext()), |
| 180 resource_message_filter_(new ResourceMessageFilter( |
| 181 0, |
| 182 0, |
| 183 nullptr, |
| 184 nullptr, |
| 185 nullptr, |
| 186 nullptr, |
| 187 nullptr, |
| 188 base::Bind(&URLLoaderFactoryImplTest::GetContexts, |
| 189 base::Unretained(this)))) { |
| 190 factory_impl_retainer_.reset(new URLLoaderFactoryImpl( |
| 191 resource_message_filter_, mojo::GetProxy(&factory_))); |
| 192 |
| 193 // Calling this function creates a request context. |
| 194 browser_context_->GetResourceContext()->GetRequestContext(); |
| 195 base::RunLoop().RunUntilIdle(); |
| 196 } |
| 197 |
| 198 ~URLLoaderFactoryImplTest() override { |
| 199 rdh_.SetDelegate(nullptr); |
| 200 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| 201 |
| 202 rdh_.CancelRequestsForProcess(resource_message_filter_->child_id()); |
| 203 base::RunLoop().RunUntilIdle(); |
| 204 } |
| 205 |
| 206 void GetContexts(ResourceType resource_type, |
| 207 int origin_pid, |
| 208 ResourceContext** resource_context, |
| 209 net::URLRequestContext** request_context) { |
| 210 *resource_context = browser_context_->GetResourceContext(); |
| 211 *request_context = |
| 212 browser_context_->GetResourceContext()->GetRequestContext(); |
| 213 } |
| 214 |
| 215 void RunUntilNextNotification(FakeURLLoaderClient* client) { |
| 216 base::RunLoop run_loop; |
| 217 client->set_quit_closure(run_loop.QuitClosure()); |
| 218 run_loop.Run(); |
| 219 } |
| 220 |
| 221 TestBrowserThreadBundle thread_bundle_; |
| 222 ResourceDispatcherHostImpl rdh_; |
| 223 std::unique_ptr<TestBrowserContext> browser_context_; |
| 224 scoped_refptr<ResourceMessageFilter> resource_message_filter_; |
| 225 mojom::URLLoaderFactoryPtr factory_; |
| 226 std::unique_ptr<URLLoaderFactoryImpl> factory_impl_retainer_; |
| 227 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryImplTest); |
| 228 }; |
| 229 |
| 230 TEST_F(URLLoaderFactoryImplTest, GetResponse) { |
| 231 mojom::URLLoaderPtr loader; |
| 232 factory_->CreateURLLoader(mojo::GetProxy(&loader)); |
| 233 base::FilePath root; |
| 234 PathService::Get(DIR_TEST_DATA, &root); |
| 235 net::URLRequestMockHTTPJob::AddUrlHandlers(root, |
| 236 BrowserThread::GetBlockingPool()); |
| 237 ResourceRequest request; |
| 238 FakeURLLoaderClient client; |
| 239 // Assume the file contents is small enough to be stored in the data pipe. |
| 240 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); |
| 241 request.method = "GET"; |
| 242 loader->Start(1, request, client.CreateInterfacePtrAndBind()); |
| 243 |
| 244 ASSERT_FALSE(client.has_received_response()); |
| 245 ASSERT_FALSE(client.response_body().is_valid()); |
| 246 ASSERT_FALSE(client.has_received_completion()); |
| 247 |
| 248 RunUntilNextNotification(&client); |
| 249 ASSERT_TRUE(client.has_received_response()); |
| 250 ASSERT_FALSE(client.has_received_completion()); |
| 251 ASSERT_FALSE(client.has_received_completion()); |
| 252 |
| 253 RunUntilNextNotification(&client); |
| 254 ASSERT_TRUE(client.has_received_response()); |
| 255 ASSERT_TRUE(client.response_body().is_valid()); |
| 256 ASSERT_FALSE(client.has_received_completion()); |
| 257 |
| 258 RunUntilNextNotification(&client); |
| 259 ASSERT_TRUE(client.has_received_completion()); |
| 260 |
| 261 EXPECT_EQ(200, client.response_head().headers->response_code()); |
| 262 std::string content_type; |
| 263 client.response_head().headers->GetNormalizedHeader("content-type", |
| 264 &content_type); |
| 265 EXPECT_EQ("text/html", content_type); |
| 266 EXPECT_EQ(0, client.completion_status().error_code); |
| 267 |
| 268 std::string contents; |
| 269 while (true) { |
| 270 char buffer[16]; |
| 271 uint32_t read = sizeof(buffer); |
| 272 MojoResult r = mojo::ReadDataRaw(client.response_body(), buffer, &read, |
| 273 MOJO_READ_DATA_FLAG_NONE); |
| 274 if (r == MOJO_RESULT_FAILED_PRECONDITION) |
| 275 break; |
| 276 if (r == MOJO_RESULT_SHOULD_WAIT) |
| 277 continue; |
| 278 ASSERT_EQ(MOJO_RESULT_OK, r); |
| 279 contents += std::string(buffer, read); |
| 280 } |
| 281 EXPECT_EQ( |
| 282 "<!doctype html>\n" |
| 283 "<p>hello</p>\n", |
| 284 contents); |
| 285 } |
| 286 |
| 287 TEST_F(URLLoaderFactoryImplTest, GetFailedResponse) { |
| 288 mojom::URLLoaderPtr loader; |
| 289 factory_->CreateURLLoader(mojo::GetProxy(&loader)); |
| 290 |
| 291 ResourceRequest request; |
| 292 FakeURLLoaderClient client; |
| 293 net::URLRequestFailedJob::AddUrlHandler(); |
| 294 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 295 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); |
| 296 request.method = "GET"; |
| 297 loader->Start(1, request, client.CreateInterfacePtrAndBind()); |
| 298 |
| 299 RunUntilNextNotification(&client); |
| 300 ASSERT_FALSE(client.has_received_response()); |
| 301 ASSERT_FALSE(client.response_body().is_valid()); |
| 302 ASSERT_TRUE(client.has_received_completion()); |
| 303 |
| 304 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); |
| 305 } |
| 306 |
| 307 // This test tests the case where resource loading is cancelled before started. |
| 308 TEST_F(URLLoaderFactoryImplTest, InvalidURL) { |
| 309 mojom::URLLoaderPtr loader; |
| 310 factory_->CreateURLLoader(mojo::GetProxy(&loader)); |
| 311 |
| 312 ResourceRequest request; |
| 313 FakeURLLoaderClient client; |
| 314 request.url = GURL(); |
| 315 request.method = "GET"; |
| 316 ASSERT_FALSE(request.url.is_valid()); |
| 317 loader->Start(1, request, client.CreateInterfacePtrAndBind()); |
| 318 |
| 319 RunUntilNextNotification(&client); |
| 320 ASSERT_FALSE(client.has_received_response()); |
| 321 ASSERT_FALSE(client.response_body().is_valid()); |
| 322 ASSERT_TRUE(client.has_received_completion()); |
| 323 |
| 324 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
| 325 } |
| 326 |
| 327 // This test tests the case where resource loading is cancelled before started. |
| 328 TEST_F(URLLoaderFactoryImplTest, ShouldNotRequestURL) { |
| 329 mojom::URLLoaderPtr loader; |
| 330 factory_->CreateURLLoader(mojo::GetProxy(&loader)); |
| 331 |
| 332 RejectingResourceDispatcherHostDelegate rdh_delegate; |
| 333 rdh_.SetDelegate(&rdh_delegate); |
| 334 ResourceRequest request; |
| 335 FakeURLLoaderClient client; |
| 336 request.url = GURL("http://localhost/"); |
| 337 request.method = "GET"; |
| 338 loader->Start(1, request, client.CreateInterfacePtrAndBind()); |
| 339 |
| 340 RunUntilNextNotification(&client); |
| 341 rdh_.SetDelegate(nullptr); |
| 342 |
| 343 ASSERT_FALSE(client.has_received_response()); |
| 344 ASSERT_FALSE(client.response_body().is_valid()); |
| 345 ASSERT_TRUE(client.has_received_completion()); |
| 346 |
| 347 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
| 348 } |
| 349 |
| 350 TEST_F(URLLoaderFactoryImplTest, FailedAfterResponseStarted) { |
| 351 mojom::URLLoaderPtr loader; |
| 352 factory_->CreateURLLoader(mojo::GetProxy(&loader)); |
| 353 |
| 354 ResourceRequest request; |
| 355 FakeURLLoaderClient client; |
| 356 net::URLRequestFailedJob::AddUrlHandler(); |
| 357 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 358 net::URLRequestFailedJob::READ_ASYNC, net::ERR_TIMED_OUT); |
| 359 request.method = "GET"; |
| 360 loader->Start(1, request, client.CreateInterfacePtrAndBind()); |
| 361 |
| 362 ASSERT_FALSE(client.has_received_response()); |
| 363 ASSERT_FALSE(client.response_body().is_valid()); |
| 364 ASSERT_FALSE(client.has_received_completion()); |
| 365 |
| 366 RunUntilNextNotification(&client); |
| 367 ASSERT_FALSE(client.has_received_response()); |
| 368 ASSERT_TRUE(client.response_body().is_valid()); |
| 369 ASSERT_FALSE(client.has_received_completion()); |
| 370 |
| 371 RunUntilNextNotification(&client); |
| 372 ASSERT_FALSE(client.has_received_response()); |
| 373 ASSERT_TRUE(client.response_body().is_valid()); |
| 374 ASSERT_TRUE(client.has_received_completion()); |
| 375 |
| 376 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); |
| 377 } |
| 378 |
| 379 TEST_F(URLLoaderFactoryImplTest, BrokenBody) { |
| 380 mojom::URLLoaderPtr loader; |
| 381 factory_->CreateURLLoader(mojo::GetProxy(&loader)); |
| 382 |
| 383 ResourceRequest request; |
| 384 FakeURLLoaderClient client; |
| 385 request.url = GURL("http://www.example.com/test"); |
| 386 request.method = "GET"; |
| 387 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( |
| 388 request.url, |
| 389 base::WrapUnique(new TestInterceptorForBrokenBody(request.url))); |
| 390 loader->Start(1, request, client.CreateInterfacePtrAndBind()); |
| 391 |
| 392 ASSERT_FALSE(client.has_received_response()); |
| 393 ASSERT_FALSE(client.response_body().is_valid()); |
| 394 ASSERT_FALSE(client.has_received_completion()); |
| 395 |
| 396 RunUntilNextNotification(&client); |
| 397 ASSERT_FALSE(client.has_received_response()); |
| 398 ASSERT_TRUE(client.response_body().is_valid()); |
| 399 ASSERT_TRUE(client.has_received_completion()); |
| 400 |
| 401 EXPECT_EQ(net::ERR_FAILED, client.completion_status().error_code); |
| 402 } |
| 403 |
| 404 } // namespace |
| 405 |
| 406 } // namespace content |
| OLD | NEW |