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_holder.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/files/file_util.h" |
| 15 #include "base/location.h" |
| 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/message_loop/message_loop.h" |
| 19 #include "base/path_service.h" |
| 20 #include "base/run_loop.h" |
| 21 #include "content/browser/loader/mojo_async_resource_handler.h" |
| 22 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 23 #include "content/browser/loader/resource_message_filter.h" |
| 24 #include "content/browser/loader/test_url_loader_client.h" |
| 25 #include "content/browser/loader_delegate_impl.h" |
| 26 #include "content/common/resource_request.h" |
| 27 #include "content/common/resource_request_completion_status.h" |
| 28 #include "content/common/url_loader.mojom.h" |
| 29 #include "content/common/url_loader_factory.mojom.h" |
| 30 #include "content/public/browser/resource_context.h" |
| 31 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 32 #include "content/public/common/content_paths.h" |
| 33 #include "content/public/test/test_browser_context.h" |
| 34 #include "content/public/test/test_browser_thread_bundle.h" |
| 35 #include "mojo/public/c/system/data_pipe.h" |
| 36 #include "mojo/public/c/system/types.h" |
| 37 #include "mojo/public/cpp/bindings/binding.h" |
| 38 #include "mojo/public/cpp/system/data_pipe.h" |
| 39 #include "net/base/io_buffer.h" |
| 40 #include "net/base/net_errors.h" |
| 41 #include "net/http/http_response_headers.h" |
| 42 #include "net/http/http_response_info.h" |
| 43 #include "net/http/http_status_code.h" |
| 44 #include "net/http/http_util.h" |
| 45 #include "net/test/url_request/url_request_failed_job.h" |
| 46 #include "net/test/url_request/url_request_mock_http_job.h" |
| 47 #include "net/url_request/url_request_filter.h" |
| 48 #include "testing/gtest/include/gtest/gtest.h" |
| 49 #include "url/gurl.h" |
| 50 |
| 51 namespace content { |
| 52 |
| 53 namespace { |
| 54 |
| 55 class RejectingResourceDispatcherHostDelegate final |
| 56 : public ResourceDispatcherHostDelegate { |
| 57 public: |
| 58 RejectingResourceDispatcherHostDelegate() {} |
| 59 bool ShouldBeginRequest(const std::string& method, |
| 60 const GURL& url, |
| 61 ResourceType resource_type, |
| 62 ResourceContext* resource_context) override { |
| 63 return false; |
| 64 } |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(RejectingResourceDispatcherHostDelegate); |
| 67 }; |
| 68 |
| 69 // The test parameter is the number of bytes allocated for the buffer in the |
| 70 // data pipe, for testing the case where the allocated size is smaller than the |
| 71 // size the mime sniffer *implicitly* requires. |
| 72 class URLLoaderFactoryImplTest : public ::testing::TestWithParam<size_t> { |
| 73 public: |
| 74 URLLoaderFactoryImplTest() |
| 75 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 76 browser_context_(new TestBrowserContext()), |
| 77 resource_message_filter_(new ResourceMessageFilter( |
| 78 0, |
| 79 0, |
| 80 nullptr, |
| 81 nullptr, |
| 82 nullptr, |
| 83 nullptr, |
| 84 nullptr, |
| 85 base::Bind(&URLLoaderFactoryImplTest::GetContexts, |
| 86 base::Unretained(this)))) { |
| 87 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); |
| 88 rdh_.SetLoaderDelegate(&loader_deleate_); |
| 89 |
| 90 factory_impl_holder_.reset(new URLLoaderFactoryHolder( |
| 91 resource_message_filter_, mojo::GetProxy(&factory_))); |
| 92 |
| 93 // Calling this function creates a request context. |
| 94 browser_context_->GetResourceContext()->GetRequestContext(); |
| 95 base::RunLoop().RunUntilIdle(); |
| 96 } |
| 97 |
| 98 ~URLLoaderFactoryImplTest() override { |
| 99 rdh_.SetDelegate(nullptr); |
| 100 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| 101 |
| 102 rdh_.CancelRequestsForProcess(resource_message_filter_->child_id()); |
| 103 base::RunLoop().RunUntilIdle(); |
| 104 MojoAsyncResourceHandler::SetAllocationSizeForTesting( |
| 105 MojoAsyncResourceHandler::kDefaultAllocationSize); |
| 106 } |
| 107 |
| 108 void GetContexts(ResourceType resource_type, |
| 109 ResourceContext** resource_context, |
| 110 net::URLRequestContext** request_context) { |
| 111 *resource_context = browser_context_->GetResourceContext(); |
| 112 *request_context = |
| 113 browser_context_->GetResourceContext()->GetRequestContext(); |
| 114 } |
| 115 |
| 116 TestBrowserThreadBundle thread_bundle_; |
| 117 LoaderDelegateImpl loader_deleate_; |
| 118 ResourceDispatcherHostImpl rdh_; |
| 119 std::unique_ptr<TestBrowserContext> browser_context_; |
| 120 scoped_refptr<ResourceMessageFilter> resource_message_filter_; |
| 121 mojom::URLLoaderFactoryPtr factory_; |
| 122 std::unique_ptr<URLLoaderFactoryHolder> factory_impl_holder_; |
| 123 |
| 124 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryImplTest); |
| 125 }; |
| 126 |
| 127 TEST_P(URLLoaderFactoryImplTest, GetResponse) { |
| 128 mojom::URLLoaderPtr loader; |
| 129 base::FilePath root; |
| 130 PathService::Get(DIR_TEST_DATA, &root); |
| 131 net::URLRequestMockHTTPJob::AddUrlHandlers(root, |
| 132 BrowserThread::GetBlockingPool()); |
| 133 ResourceRequest request; |
| 134 TestURLLoaderClient client; |
| 135 const base::FilePath::StringType filename = FILE_PATH_LITERAL("hello.html"); |
| 136 // Assume the file contents is small enough to be stored in the data pipe. |
| 137 request.url = net::URLRequestMockHTTPJob::GetMockUrl(filename); |
| 138 request.method = "GET"; |
| 139 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, |
| 140 client.CreateInterfacePtrAndBind()); |
| 141 |
| 142 ASSERT_FALSE(client.has_received_response()); |
| 143 ASSERT_FALSE(client.response_body().is_valid()); |
| 144 ASSERT_FALSE(client.has_received_completion()); |
| 145 |
| 146 client.RunUntilResponseReceived(); |
| 147 ASSERT_FALSE(client.has_received_completion()); |
| 148 ASSERT_FALSE(client.has_received_completion()); |
| 149 |
| 150 client.RunUntilResponseBodyArrived(); |
| 151 ASSERT_TRUE(client.response_body().is_valid()); |
| 152 ASSERT_FALSE(client.has_received_completion()); |
| 153 |
| 154 client.RunUntilComplete(); |
| 155 |
| 156 EXPECT_EQ(200, client.response_head().headers->response_code()); |
| 157 std::string content_type; |
| 158 client.response_head().headers->GetNormalizedHeader("content-type", |
| 159 &content_type); |
| 160 EXPECT_EQ("text/html", content_type); |
| 161 EXPECT_EQ(0, client.completion_status().error_code); |
| 162 |
| 163 std::string contents; |
| 164 while (true) { |
| 165 char buffer[16]; |
| 166 uint32_t read_size = sizeof(buffer); |
| 167 MojoResult r = mojo::ReadDataRaw(client.response_body(), buffer, &read_size, |
| 168 MOJO_READ_DATA_FLAG_NONE); |
| 169 if (r == MOJO_RESULT_FAILED_PRECONDITION) |
| 170 break; |
| 171 if (r == MOJO_RESULT_SHOULD_WAIT) |
| 172 continue; |
| 173 ASSERT_EQ(MOJO_RESULT_OK, r); |
| 174 contents += std::string(buffer, read_size); |
| 175 } |
| 176 std::string expected; |
| 177 base::ReadFileToString(root.Append(base::FilePath(filename)), &expected); |
| 178 EXPECT_EQ(expected, contents); |
| 179 } |
| 180 |
| 181 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { |
| 182 mojom::URLLoaderPtr loader; |
| 183 ResourceRequest request; |
| 184 TestURLLoaderClient client; |
| 185 net::URLRequestFailedJob::AddUrlHandler(); |
| 186 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 187 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); |
| 188 request.method = "GET"; |
| 189 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, |
| 190 client.CreateInterfacePtrAndBind()); |
| 191 |
| 192 client.RunUntilComplete(); |
| 193 ASSERT_FALSE(client.has_received_response()); |
| 194 ASSERT_FALSE(client.response_body().is_valid()); |
| 195 |
| 196 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); |
| 197 } |
| 198 |
| 199 // This test tests a case where resource loading is cancelled before started. |
| 200 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { |
| 201 mojom::URLLoaderPtr loader; |
| 202 ResourceRequest request; |
| 203 TestURLLoaderClient client; |
| 204 request.url = GURL(); |
| 205 request.method = "GET"; |
| 206 ASSERT_FALSE(request.url.is_valid()); |
| 207 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, |
| 208 client.CreateInterfacePtrAndBind()); |
| 209 |
| 210 client.RunUntilComplete(); |
| 211 ASSERT_FALSE(client.has_received_response()); |
| 212 ASSERT_FALSE(client.response_body().is_valid()); |
| 213 |
| 214 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
| 215 } |
| 216 |
| 217 // This test tests a case where resource loading is cancelled before started. |
| 218 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { |
| 219 mojom::URLLoaderPtr loader; |
| 220 RejectingResourceDispatcherHostDelegate rdh_delegate; |
| 221 rdh_.SetDelegate(&rdh_delegate); |
| 222 ResourceRequest request; |
| 223 TestURLLoaderClient client; |
| 224 request.url = GURL("http://localhost/"); |
| 225 request.method = "GET"; |
| 226 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, |
| 227 client.CreateInterfacePtrAndBind()); |
| 228 |
| 229 client.RunUntilComplete(); |
| 230 rdh_.SetDelegate(nullptr); |
| 231 |
| 232 ASSERT_FALSE(client.has_received_response()); |
| 233 ASSERT_FALSE(client.response_body().is_valid()); |
| 234 |
| 235 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
| 236 } |
| 237 |
| 238 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, |
| 239 URLLoaderFactoryImplTest, |
| 240 ::testing::Values(128, 32 * 1024)); |
| 241 |
| 242 } // namespace |
| 243 |
| 244 } // namespace content |
OLD | NEW |