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