Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1315)

Side by Side Diff: content/browser/loader/url_loader_factory_holder_unittest.cc

Issue 1970693002: Use mojo for Chrome Loading, Part 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 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 void RunUntilNextNotification(FakeURLLoaderClient* client) {
116 base::RunLoop run_loop;
117 client->set_quit_closure(run_loop.QuitClosure());
118 run_loop.Run();
119 }
120
121 TestBrowserThreadBundle thread_bundle_;
122 LoaderDelegateImpl loader_deleate_;
123 ResourceDispatcherHostImpl rdh_;
124 std::unique_ptr<TestBrowserContext> browser_context_;
125 scoped_refptr<ResourceMessageFilter> resource_message_filter_;
126 mojom::URLLoaderFactoryPtr factory_;
127 std::unique_ptr<URLLoaderFactoryHolder> factory_impl_holder_;
128
129 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryImplTest);
130 };
131
132 TEST_P(URLLoaderFactoryImplTest, GetResponse) {
133 mojom::URLLoaderPtr loader;
134 base::FilePath root;
135 PathService::Get(DIR_TEST_DATA, &root);
136 net::URLRequestMockHTTPJob::AddUrlHandlers(root,
137 BrowserThread::GetBlockingPool());
138 ResourceRequest request;
139 FakeURLLoaderClient client;
140 // Assume the file contents is small enough to be stored in the data pipe.
141 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html");
142 request.method = "GET";
143 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request,
144 client.CreateInterfacePtrAndBind());
145
146 ASSERT_FALSE(client.has_received_response());
147 ASSERT_FALSE(client.response_body().is_valid());
148 ASSERT_FALSE(client.has_received_completion());
149
150 RunUntilNextNotification(&client);
151 ASSERT_TRUE(client.has_received_response());
152 ASSERT_FALSE(client.has_received_completion());
153 ASSERT_FALSE(client.has_received_completion());
154
155 RunUntilNextNotification(&client);
156 ASSERT_TRUE(client.has_received_response());
157 ASSERT_TRUE(client.response_body().is_valid());
158 ASSERT_FALSE(client.has_received_completion());
159
160 RunUntilNextNotification(&client);
161 ASSERT_TRUE(client.has_received_completion());
162
163 EXPECT_EQ(200, client.response_head().headers->response_code());
164 std::string content_type;
165 client.response_head().headers->GetNormalizedHeader("content-type",
166 &content_type);
167 EXPECT_EQ("text/html", content_type);
168 EXPECT_EQ(0, client.completion_status().error_code);
169
170 std::string contents;
171 while (true) {
172 char buffer[16];
173 uint32_t read = sizeof(buffer);
174 MojoResult r = mojo::ReadDataRaw(client.response_body(), buffer, &read,
175 MOJO_READ_DATA_FLAG_NONE);
176 if (r == MOJO_RESULT_FAILED_PRECONDITION)
177 break;
178 if (r == MOJO_RESULT_SHOULD_WAIT)
179 continue;
180 ASSERT_EQ(MOJO_RESULT_OK, r);
181 contents += std::string(buffer, read);
182 }
183 EXPECT_EQ(
184 "<!doctype html>\n"
185 "<p>hello</p>\n",
186 contents);
187 }
188
189 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) {
190 mojom::URLLoaderPtr loader;
191 ResourceRequest request;
192 FakeURLLoaderClient client;
193 net::URLRequestFailedJob::AddUrlHandler();
194 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
195 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT);
196 request.method = "GET";
197 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request,
198 client.CreateInterfacePtrAndBind());
199
200 RunUntilNextNotification(&client);
201 ASSERT_FALSE(client.has_received_response());
202 ASSERT_FALSE(client.response_body().is_valid());
203 ASSERT_TRUE(client.has_received_completion());
204
205 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code);
206 }
207
208 // This test tests a case where resource loading is cancelled before started.
209 TEST_P(URLLoaderFactoryImplTest, InvalidURL) {
210 mojom::URLLoaderPtr loader;
211 ResourceRequest request;
212 FakeURLLoaderClient client;
213 request.url = GURL();
214 request.method = "GET";
215 ASSERT_FALSE(request.url.is_valid());
216 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request,
217 client.CreateInterfacePtrAndBind());
218
219 RunUntilNextNotification(&client);
220 ASSERT_FALSE(client.has_received_response());
221 ASSERT_FALSE(client.response_body().is_valid());
222 ASSERT_TRUE(client.has_received_completion());
223
224 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code);
225 }
226
227 // This test tests a case where resource loading is cancelled before started.
228 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) {
229 mojom::URLLoaderPtr loader;
230 RejectingResourceDispatcherHostDelegate rdh_delegate;
231 rdh_.SetDelegate(&rdh_delegate);
232 ResourceRequest request;
233 FakeURLLoaderClient client;
234 request.url = GURL("http://localhost/");
235 request.method = "GET";
236 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request,
237 client.CreateInterfacePtrAndBind());
238
239 RunUntilNextNotification(&client);
240 rdh_.SetDelegate(nullptr);
241
242 ASSERT_FALSE(client.has_received_response());
243 ASSERT_FALSE(client.response_body().is_valid());
244 ASSERT_TRUE(client.has_received_completion());
245
246 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code);
247 }
248
249 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest,
250 URLLoaderFactoryImplTest,
251 ::testing::Values(128, 32 * 1024));
252
253 } // namespace
254
255 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698