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

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

Issue 1970693002: Use mojo for Chrome Loading, Part 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 4 years, 4 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_impl.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/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 base::Bind(&URLLoaderFactoryImplTest::GetContexts,
84 base::Unretained(this)))) {
85 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam());
86 rdh_.SetLoaderDelegate(&loader_deleate_);
87
88 URLLoaderFactoryImpl::Create(resource_message_filter_,
89 mojo::GetProxy(&factory_));
90
91 // Calling this function creates a request context.
92 browser_context_->GetResourceContext()->GetRequestContext();
93 base::RunLoop().RunUntilIdle();
94 }
95
96 ~URLLoaderFactoryImplTest() override {
97 rdh_.SetDelegate(nullptr);
98 net::URLRequestFilter::GetInstance()->ClearHandlers();
99
100 rdh_.CancelRequestsForProcess(resource_message_filter_->child_id());
101 base::RunLoop().RunUntilIdle();
102 MojoAsyncResourceHandler::SetAllocationSizeForTesting(
103 MojoAsyncResourceHandler::kDefaultAllocationSize);
104 }
105
106 void GetContexts(ResourceType resource_type,
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 TestBrowserThreadBundle thread_bundle_;
115 LoaderDelegateImpl loader_deleate_;
116 ResourceDispatcherHostImpl rdh_;
117 std::unique_ptr<TestBrowserContext> browser_context_;
118 scoped_refptr<ResourceMessageFilter> resource_message_filter_;
119 mojom::URLLoaderFactoryPtr factory_;
120
121 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryImplTest);
122 };
123
124 TEST_P(URLLoaderFactoryImplTest, GetResponse) {
125 mojom::URLLoaderPtr loader;
126 base::FilePath root;
127 PathService::Get(DIR_TEST_DATA, &root);
128 net::URLRequestMockHTTPJob::AddUrlHandlers(root,
129 BrowserThread::GetBlockingPool());
130 ResourceRequest request;
131 TestURLLoaderClient client;
132 // Assume the file contents is small enough to be stored in the data pipe.
133 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html");
134 request.method = "GET";
135 request.is_main_frame = true;
136 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request,
137 client.CreateInterfacePtrAndBind());
138
139 ASSERT_FALSE(client.has_received_response());
140 ASSERT_FALSE(client.response_body().is_valid());
141 ASSERT_FALSE(client.has_received_completion());
142
143 client.RunUntilResponseReceived();
144 ASSERT_FALSE(client.has_received_completion());
145 ASSERT_FALSE(client.has_received_completion());
146
147 client.RunUntilResponseBodyArrived();
148 ASSERT_TRUE(client.response_body().is_valid());
149 ASSERT_FALSE(client.has_received_completion());
150
151 client.RunUntilComplete();
152
153 EXPECT_EQ(200, client.response_head().headers->response_code());
154 std::string content_type;
155 client.response_head().headers->GetNormalizedHeader("content-type",
156 &content_type);
157 EXPECT_EQ("text/html", content_type);
158 EXPECT_EQ(0, client.completion_status().error_code);
159
160 std::string contents;
161 while (true) {
162 char buffer[16];
163 uint32_t read_size = sizeof(buffer);
164 MojoResult r = mojo::ReadDataRaw(client.response_body(), buffer, &read_size,
165 MOJO_READ_DATA_FLAG_NONE);
166 if (r == MOJO_RESULT_FAILED_PRECONDITION)
167 break;
168 if (r == MOJO_RESULT_SHOULD_WAIT)
169 continue;
170 ASSERT_EQ(MOJO_RESULT_OK, r);
171 contents += std::string(buffer, read_size);
172 }
173 std::string expected;
174 base::ReadFileToString(root.Append(
175 base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected);
176 EXPECT_EQ(expected, contents);
177 }
178
179 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) {
180 mojom::URLLoaderPtr loader;
181 ResourceRequest request;
182 TestURLLoaderClient client;
183 net::URLRequestFailedJob::AddUrlHandler();
184 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
185 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT);
186 request.method = "GET";
187 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request,
188 client.CreateInterfacePtrAndBind());
189
190 client.RunUntilComplete();
191 ASSERT_FALSE(client.has_received_response());
192 ASSERT_FALSE(client.response_body().is_valid());
193
194 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code);
195 }
196
197 // This test tests a case where resource loading is cancelled before started.
198 TEST_P(URLLoaderFactoryImplTest, InvalidURL) {
199 mojom::URLLoaderPtr loader;
200 ResourceRequest request;
201 TestURLLoaderClient client;
202 request.url = GURL();
203 request.method = "GET";
204 ASSERT_FALSE(request.url.is_valid());
205 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request,
206 client.CreateInterfacePtrAndBind());
207
208 client.RunUntilComplete();
209 ASSERT_FALSE(client.has_received_response());
210 ASSERT_FALSE(client.response_body().is_valid());
211
212 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code);
213 }
214
215 // This test tests a case where resource loading is cancelled before started.
216 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) {
217 mojom::URLLoaderPtr loader;
218 RejectingResourceDispatcherHostDelegate rdh_delegate;
219 rdh_.SetDelegate(&rdh_delegate);
220 ResourceRequest request;
221 TestURLLoaderClient client;
222 request.url = GURL("http://localhost/");
223 request.method = "GET";
224 factory_->CreateLoaderAndStart(mojo::GetProxy(&loader), 1, request,
225 client.CreateInterfacePtrAndBind());
226
227 client.RunUntilComplete();
228 rdh_.SetDelegate(nullptr);
229
230 ASSERT_FALSE(client.has_received_response());
231 ASSERT_FALSE(client.response_body().is_valid());
232
233 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code);
234 }
235
236 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest,
237 URLLoaderFactoryImplTest,
238 ::testing::Values(128, 32 * 1024));
239
240 } // namespace
241
242 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698