| OLD | NEW |
| 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 <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 19 #include "base/path_service.h" | 19 #include "base/path_service.h" |
| 20 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
| 21 #include "content/browser/loader/mojo_async_resource_handler.h" | 21 #include "content/browser/loader/mojo_async_resource_handler.h" |
| 22 #include "content/browser/loader/navigation_resource_throttle.h" | 22 #include "content/browser/loader/navigation_resource_throttle.h" |
| 23 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 23 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 24 #include "content/browser/loader/resource_message_filter.h" | 24 #include "content/browser/loader/resource_message_filter.h" |
| 25 #include "content/browser/loader/resource_request_info_impl.h" |
| 25 #include "content/browser/loader/test_url_loader_client.h" | 26 #include "content/browser/loader/test_url_loader_client.h" |
| 26 #include "content/browser/loader_delegate_impl.h" | 27 #include "content/browser/loader_delegate_impl.h" |
| 27 #include "content/common/resource_request.h" | 28 #include "content/common/resource_request.h" |
| 28 #include "content/common/resource_request_completion_status.h" | 29 #include "content/common/resource_request_completion_status.h" |
| 29 #include "content/common/url_loader.mojom.h" | 30 #include "content/common/url_loader.mojom.h" |
| 30 #include "content/public/browser/resource_context.h" | 31 #include "content/public/browser/resource_context.h" |
| 31 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 32 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 32 #include "content/public/common/content_paths.h" | 33 #include "content/public/common/content_paths.h" |
| 33 #include "content/public/common/process_type.h" | 34 #include "content/public/common/process_type.h" |
| 34 #include "content/public/test/test_browser_context.h" | 35 #include "content/public/test/test_browser_context.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 46 #include "net/test/url_request/url_request_failed_job.h" | 47 #include "net/test/url_request/url_request_failed_job.h" |
| 47 #include "net/test/url_request/url_request_mock_http_job.h" | 48 #include "net/test/url_request/url_request_mock_http_job.h" |
| 48 #include "net/url_request/url_request_filter.h" | 49 #include "net/url_request/url_request_filter.h" |
| 49 #include "testing/gtest/include/gtest/gtest.h" | 50 #include "testing/gtest/include/gtest/gtest.h" |
| 50 #include "url/gurl.h" | 51 #include "url/gurl.h" |
| 51 | 52 |
| 52 namespace content { | 53 namespace content { |
| 53 | 54 |
| 54 namespace { | 55 namespace { |
| 55 | 56 |
| 57 constexpr int kChildId = 99; |
| 58 |
| 56 class RejectingResourceDispatcherHostDelegate final | 59 class RejectingResourceDispatcherHostDelegate final |
| 57 : public ResourceDispatcherHostDelegate { | 60 : public ResourceDispatcherHostDelegate { |
| 58 public: | 61 public: |
| 59 RejectingResourceDispatcherHostDelegate() {} | 62 RejectingResourceDispatcherHostDelegate() {} |
| 60 bool ShouldBeginRequest(const std::string& method, | 63 bool ShouldBeginRequest(const std::string& method, |
| 61 const GURL& url, | 64 const GURL& url, |
| 62 ResourceType resource_type, | 65 ResourceType resource_type, |
| 63 ResourceContext* resource_context) override { | 66 ResourceContext* resource_context) override { |
| 64 return false; | 67 return false; |
| 65 } | 68 } |
| 66 | 69 |
| 67 DISALLOW_COPY_AND_ASSIGN(RejectingResourceDispatcherHostDelegate); | 70 DISALLOW_COPY_AND_ASSIGN(RejectingResourceDispatcherHostDelegate); |
| 68 }; | 71 }; |
| 69 | 72 |
| 70 // The test parameter is the number of bytes allocated for the buffer in the | 73 // The test parameter is the number of bytes allocated for the buffer in the |
| 71 // data pipe, for testing the case where the allocated size is smaller than the | 74 // data pipe, for testing the case where the allocated size is smaller than the |
| 72 // size the mime sniffer *implicitly* requires. | 75 // size the mime sniffer *implicitly* requires. |
| 73 class URLLoaderFactoryImplTest : public ::testing::TestWithParam<size_t> { | 76 class URLLoaderFactoryImplTest : public ::testing::TestWithParam<size_t> { |
| 74 public: | 77 public: |
| 75 URLLoaderFactoryImplTest() | 78 URLLoaderFactoryImplTest() |
| 76 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 79 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 77 browser_context_(new TestBrowserContext()), | 80 browser_context_(new TestBrowserContext()), |
| 78 resource_message_filter_(new ResourceMessageFilter( | 81 resource_message_filter_(new ResourceMessageFilter( |
| 79 0, | 82 kChildId, |
| 80 // If browser side navigation is enabled then | 83 // If browser side navigation is enabled then |
| 81 // ResourceDispatcherHostImpl prevents main frame URL requests from | 84 // ResourceDispatcherHostImpl prevents main frame URL requests from |
| 82 // the renderer. Ensure that these checks don't trip us up by | 85 // the renderer. Ensure that these checks don't trip us up by |
| 83 // setting the process type in ResourceMessageFilter as | 86 // setting the process type in ResourceMessageFilter as |
| 84 // PROCESS_TYPE_UNKNOWN. | 87 // PROCESS_TYPE_UNKNOWN. |
| 85 PROCESS_TYPE_UNKNOWN, | 88 PROCESS_TYPE_UNKNOWN, |
| 86 nullptr, | 89 nullptr, |
| 87 nullptr, | 90 nullptr, |
| 88 nullptr, | 91 nullptr, |
| 89 nullptr, | 92 nullptr, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 LoaderDelegateImpl loader_deleate_; | 126 LoaderDelegateImpl loader_deleate_; |
| 124 ResourceDispatcherHostImpl rdh_; | 127 ResourceDispatcherHostImpl rdh_; |
| 125 std::unique_ptr<TestBrowserContext> browser_context_; | 128 std::unique_ptr<TestBrowserContext> browser_context_; |
| 126 scoped_refptr<ResourceMessageFilter> resource_message_filter_; | 129 scoped_refptr<ResourceMessageFilter> resource_message_filter_; |
| 127 mojom::URLLoaderFactoryPtr factory_; | 130 mojom::URLLoaderFactoryPtr factory_; |
| 128 | 131 |
| 129 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryImplTest); | 132 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryImplTest); |
| 130 }; | 133 }; |
| 131 | 134 |
| 132 TEST_P(URLLoaderFactoryImplTest, GetResponse) { | 135 TEST_P(URLLoaderFactoryImplTest, GetResponse) { |
| 136 constexpr int32_t kRoutingId = 81; |
| 137 constexpr int32_t kRequestId = 28; |
| 133 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 138 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
| 134 mojom::URLLoaderPtr loader; | 139 mojom::URLLoaderPtr loader; |
| 135 base::FilePath root; | 140 base::FilePath root; |
| 136 PathService::Get(DIR_TEST_DATA, &root); | 141 PathService::Get(DIR_TEST_DATA, &root); |
| 137 net::URLRequestMockHTTPJob::AddUrlHandlers(root, | 142 net::URLRequestMockHTTPJob::AddUrlHandlers(root, |
| 138 BrowserThread::GetBlockingPool()); | 143 BrowserThread::GetBlockingPool()); |
| 139 ResourceRequest request; | 144 ResourceRequest request; |
| 140 TestURLLoaderClient client; | 145 TestURLLoaderClient client; |
| 141 // 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. |
| 142 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); | 147 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); |
| 143 request.method = "GET"; | 148 request.method = "GET"; |
| 144 request.is_main_frame = true; | 149 request.is_main_frame = true; |
| 145 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, | 150 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), kRoutingId, |
| 151 kRequestId, request, |
| 146 client.CreateInterfacePtrAndBind()); | 152 client.CreateInterfacePtrAndBind()); |
| 147 | 153 |
| 148 ASSERT_FALSE(client.has_received_response()); | 154 ASSERT_FALSE(client.has_received_response()); |
| 149 ASSERT_FALSE(client.response_body().is_valid()); | 155 ASSERT_FALSE(client.response_body().is_valid()); |
| 150 ASSERT_FALSE(client.has_received_completion()); | 156 ASSERT_FALSE(client.has_received_completion()); |
| 151 | 157 |
| 152 client.RunUntilResponseReceived(); | 158 client.RunUntilResponseReceived(); |
| 159 |
| 160 net::URLRequest* url_request = |
| 161 rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId)); |
| 162 ASSERT_TRUE(url_request); |
| 163 ResourceRequestInfoImpl* request_info = |
| 164 ResourceRequestInfoImpl::ForRequest(url_request); |
| 165 ASSERT_TRUE(request_info); |
| 166 EXPECT_EQ(kChildId, request_info->GetChildID()); |
| 167 EXPECT_EQ(kRoutingId, request_info->GetRouteID()); |
| 168 EXPECT_EQ(kRequestId, request_info->GetRequestID()); |
| 169 |
| 153 ASSERT_FALSE(client.has_received_completion()); | 170 ASSERT_FALSE(client.has_received_completion()); |
| 154 ASSERT_FALSE(client.has_received_completion()); | 171 ASSERT_FALSE(client.has_received_completion()); |
| 155 | 172 |
| 156 client.RunUntilResponseBodyArrived(); | 173 client.RunUntilResponseBodyArrived(); |
| 157 ASSERT_TRUE(client.response_body().is_valid()); | 174 ASSERT_TRUE(client.response_body().is_valid()); |
| 158 ASSERT_FALSE(client.has_received_completion()); | 175 ASSERT_FALSE(client.has_received_completion()); |
| 159 | 176 |
| 160 client.RunUntilComplete(); | 177 client.RunUntilComplete(); |
| 161 | 178 |
| 162 EXPECT_EQ(200, client.response_head().headers->response_code()); | 179 EXPECT_EQ(200, client.response_head().headers->response_code()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 187 | 204 |
| 188 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { | 205 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { |
| 189 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 206 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
| 190 mojom::URLLoaderPtr loader; | 207 mojom::URLLoaderPtr loader; |
| 191 ResourceRequest request; | 208 ResourceRequest request; |
| 192 TestURLLoaderClient client; | 209 TestURLLoaderClient client; |
| 193 net::URLRequestFailedJob::AddUrlHandler(); | 210 net::URLRequestFailedJob::AddUrlHandler(); |
| 194 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 211 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 195 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); | 212 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); |
| 196 request.method = "GET"; | 213 request.method = "GET"; |
| 197 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, | 214 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 2, 1, request, |
| 198 client.CreateInterfacePtrAndBind()); | 215 client.CreateInterfacePtrAndBind()); |
| 199 | 216 |
| 200 client.RunUntilComplete(); | 217 client.RunUntilComplete(); |
| 201 ASSERT_FALSE(client.has_received_response()); | 218 ASSERT_FALSE(client.has_received_response()); |
| 202 ASSERT_FALSE(client.response_body().is_valid()); | 219 ASSERT_FALSE(client.response_body().is_valid()); |
| 203 | 220 |
| 204 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); | 221 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); |
| 205 } | 222 } |
| 206 | 223 |
| 207 // This test tests a case where resource loading is cancelled before started. | 224 // This test tests a case where resource loading is cancelled before started. |
| 208 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { | 225 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { |
| 209 mojom::URLLoaderPtr loader; | 226 mojom::URLLoaderPtr loader; |
| 210 ResourceRequest request; | 227 ResourceRequest request; |
| 211 TestURLLoaderClient client; | 228 TestURLLoaderClient client; |
| 212 request.url = GURL(); | 229 request.url = GURL(); |
| 213 request.method = "GET"; | 230 request.method = "GET"; |
| 214 ASSERT_FALSE(request.url.is_valid()); | 231 ASSERT_FALSE(request.url.is_valid()); |
| 215 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, | 232 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 2, 1, request, |
| 216 client.CreateInterfacePtrAndBind()); | 233 client.CreateInterfacePtrAndBind()); |
| 217 | 234 |
| 218 client.RunUntilComplete(); | 235 client.RunUntilComplete(); |
| 219 ASSERT_FALSE(client.has_received_response()); | 236 ASSERT_FALSE(client.has_received_response()); |
| 220 ASSERT_FALSE(client.response_body().is_valid()); | 237 ASSERT_FALSE(client.response_body().is_valid()); |
| 221 | 238 |
| 222 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); | 239 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
| 223 } | 240 } |
| 224 | 241 |
| 225 // This test tests a case where resource loading is cancelled before started. | 242 // This test tests a case where resource loading is cancelled before started. |
| 226 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { | 243 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { |
| 227 mojom::URLLoaderPtr loader; | 244 mojom::URLLoaderPtr loader; |
| 228 RejectingResourceDispatcherHostDelegate rdh_delegate; | 245 RejectingResourceDispatcherHostDelegate rdh_delegate; |
| 229 rdh_.SetDelegate(&rdh_delegate); | 246 rdh_.SetDelegate(&rdh_delegate); |
| 230 ResourceRequest request; | 247 ResourceRequest request; |
| 231 TestURLLoaderClient client; | 248 TestURLLoaderClient client; |
| 232 request.url = GURL("http://localhost/"); | 249 request.url = GURL("http://localhost/"); |
| 233 request.method = "GET"; | 250 request.method = "GET"; |
| 234 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, | 251 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 2, 1, request, |
| 235 client.CreateInterfacePtrAndBind()); | 252 client.CreateInterfacePtrAndBind()); |
| 236 | 253 |
| 237 client.RunUntilComplete(); | 254 client.RunUntilComplete(); |
| 238 rdh_.SetDelegate(nullptr); | 255 rdh_.SetDelegate(nullptr); |
| 239 | 256 |
| 240 ASSERT_FALSE(client.has_received_response()); | 257 ASSERT_FALSE(client.has_received_response()); |
| 241 ASSERT_FALSE(client.response_body().is_valid()); | 258 ASSERT_FALSE(client.response_body().is_valid()); |
| 242 | 259 |
| 243 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); | 260 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
| 244 } | 261 } |
| 245 | 262 |
| 246 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, | 263 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, |
| 247 URLLoaderFactoryImplTest, | 264 URLLoaderFactoryImplTest, |
| 248 ::testing::Values(128, 32 * 1024)); | 265 ::testing::Values(128, 32 * 1024)); |
| 249 | 266 |
| 250 } // namespace | 267 } // namespace |
| 251 | 268 |
| 252 } // namespace content | 269 } // namespace content |
| OLD | NEW |