Chromium Code Reviews| 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/resource_dispatcher_host_impl.h" | |
| 22 #include "content/browser/loader/resource_message_filter.h" | |
| 23 #include "content/browser/loader/test_url_loader_client.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 MojoAsyncResourceHandler::SetAllocationSizeForTesting( | |
| 104 MojoAsyncResourceHandler::kDefaultAllocationSize); | |
| 105 } | |
| 106 | |
| 107 void GetContexts(ResourceType resource_type, | |
| 108 ResourceContext** resource_context, | |
| 109 net::URLRequestContext** request_context) { | |
| 110 *resource_context = browser_context_->GetResourceContext(); | |
| 111 *request_context = | |
| 112 browser_context_->GetResourceContext()->GetRequestContext(); | |
| 113 } | |
| 114 | |
| 115 TestBrowserThreadBundle thread_bundle_; | |
| 116 LoaderDelegateImpl loader_deleate_; | |
| 117 ResourceDispatcherHostImpl rdh_; | |
| 118 std::unique_ptr<TestBrowserContext> browser_context_; | |
| 119 scoped_refptr<ResourceMessageFilter> resource_message_filter_; | |
| 120 mojom::URLLoaderFactoryPtr factory_; | |
| 121 std::unique_ptr<URLLoaderFactoryHolder> factory_impl_holder_; | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryImplTest); | |
| 124 }; | |
| 125 | |
| 126 TEST_P(URLLoaderFactoryImplTest, GetResponse) { | |
| 127 mojom::URLLoaderPtr loader; | |
| 128 base::FilePath root; | |
| 129 PathService::Get(DIR_TEST_DATA, &root); | |
| 130 net::URLRequestMockHTTPJob::AddUrlHandlers(root, | |
| 131 BrowserThread::GetBlockingPool()); | |
| 132 ResourceRequest request; | |
| 133 TestURLLoaderClient client; | |
| 134 // Assume the file contents is small enough to be stored in the data pipe. | |
| 135 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); | |
| 136 request.method = "GET"; | |
| 137 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, | |
| 138 client.CreateInterfacePtrAndBind()); | |
| 139 | |
| 140 ASSERT_FALSE(client.has_received_response()); | |
| 141 ASSERT_FALSE(client.response_body().is_valid()); | |
| 142 ASSERT_FALSE(client.has_received_completion()); | |
| 143 | |
| 144 client.RunUntilResponseReceived(); | |
| 145 ASSERT_FALSE(client.has_received_completion()); | |
| 146 ASSERT_FALSE(client.has_received_completion()); | |
| 147 | |
| 148 client.RunUntilResponseBodyArrived(); | |
| 149 ASSERT_TRUE(client.response_body().is_valid()); | |
| 150 ASSERT_FALSE(client.has_received_completion()); | |
| 151 | |
| 152 client.RunUntilComplete(); | |
| 153 | |
| 154 EXPECT_EQ(200, client.response_head().headers->response_code()); | |
| 155 std::string content_type; | |
| 156 client.response_head().headers->GetNormalizedHeader("content-type", | |
| 157 &content_type); | |
| 158 EXPECT_EQ("text/html", content_type); | |
| 159 EXPECT_EQ(0, client.completion_status().error_code); | |
| 160 | |
| 161 std::string contents; | |
| 162 while (true) { | |
| 163 char buffer[16]; | |
| 164 uint32_t read = sizeof(buffer); | |
|
kinuko
2016/08/04 16:07:03
ditto
yhirano
2016/08/05 12:21:40
Done.
| |
| 165 MojoResult r = mojo::ReadDataRaw(client.response_body(), buffer, &read, | |
| 166 MOJO_READ_DATA_FLAG_NONE); | |
| 167 if (r == MOJO_RESULT_FAILED_PRECONDITION) | |
| 168 break; | |
| 169 if (r == MOJO_RESULT_SHOULD_WAIT) | |
| 170 continue; | |
| 171 ASSERT_EQ(MOJO_RESULT_OK, r); | |
| 172 contents += std::string(buffer, read); | |
| 173 } | |
| 174 EXPECT_EQ( | |
| 175 "<!doctype html>\n" | |
| 176 "<p>hello</p>\n", | |
|
kinuko
2016/08/04 16:07:03
nit: reading out file contents is easy in tests (e
yhirano
2016/08/05 12:21:39
Done.
| |
| 177 contents); | |
| 178 } | |
| 179 | |
| 180 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { | |
| 181 mojom::URLLoaderPtr loader; | |
| 182 ResourceRequest request; | |
| 183 TestURLLoaderClient client; | |
| 184 net::URLRequestFailedJob::AddUrlHandler(); | |
| 185 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | |
| 186 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); | |
| 187 request.method = "GET"; | |
| 188 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, | |
| 189 client.CreateInterfacePtrAndBind()); | |
| 190 | |
| 191 client.RunUntilComplete(); | |
| 192 ASSERT_FALSE(client.has_received_response()); | |
| 193 ASSERT_FALSE(client.response_body().is_valid()); | |
| 194 | |
| 195 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); | |
| 196 } | |
| 197 | |
| 198 // This test tests a case where resource loading is cancelled before started. | |
| 199 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { | |
| 200 mojom::URLLoaderPtr loader; | |
| 201 ResourceRequest request; | |
| 202 TestURLLoaderClient client; | |
| 203 request.url = GURL(); | |
| 204 request.method = "GET"; | |
| 205 ASSERT_FALSE(request.url.is_valid()); | |
| 206 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, | |
| 207 client.CreateInterfacePtrAndBind()); | |
| 208 | |
| 209 client.RunUntilComplete(); | |
| 210 ASSERT_FALSE(client.has_received_response()); | |
| 211 ASSERT_FALSE(client.response_body().is_valid()); | |
| 212 | |
| 213 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); | |
| 214 } | |
| 215 | |
| 216 // This test tests a case where resource loading is cancelled before started. | |
| 217 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { | |
| 218 mojom::URLLoaderPtr loader; | |
| 219 RejectingResourceDispatcherHostDelegate rdh_delegate; | |
| 220 rdh_.SetDelegate(&rdh_delegate); | |
| 221 ResourceRequest request; | |
| 222 TestURLLoaderClient client; | |
| 223 request.url = GURL("http://localhost/"); | |
| 224 request.method = "GET"; | |
| 225 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request, | |
| 226 client.CreateInterfacePtrAndBind()); | |
| 227 | |
| 228 client.RunUntilComplete(); | |
| 229 rdh_.SetDelegate(nullptr); | |
| 230 | |
| 231 ASSERT_FALSE(client.has_received_response()); | |
| 232 ASSERT_FALSE(client.response_body().is_valid()); | |
| 233 | |
| 234 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); | |
| 235 } | |
| 236 | |
| 237 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, | |
| 238 URLLoaderFactoryImplTest, | |
| 239 ::testing::Values(128, 32 * 1024)); | |
| 240 | |
| 241 } // namespace | |
| 242 | |
| 243 } // namespace content | |
| OLD | NEW |