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

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: Created 4 years, 7 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 "base/bind.h"
8 #include "base/callback.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "content/browser/loader/resource_dispatcher_host_impl.h"
12 #include "content/browser/loader/resource_message_filter.h"
13 #include "content/common/url_loader.mojom.h"
14 #include "content/common/url_loader_factory.mojom.h"
15 #include "content/public/browser/resource_context.h"
16 #include "content/public/test/test_browser_context.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "net/base/net_errors.h"
19 #include "net/test/url_request/url_request_failed_job.h"
20 #include "net/url_request/url_request.h"
21 #include "net/url_request/url_request_test_job.h"
22 #include "net/url_request/url_request_test_util.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "url/gurl.h"
25
26 namespace content {
27
28 namespace {
29
30 class URLLoaderClientImpl final : public mojom::URLLoaderClient {
31 public:
32 URLLoaderClientImpl() : binding_(this) {}
33 void OnReceiveResponse(const ResourceResponseHead& response_head) override {
34 has_received_response_ = true;
35 response_head_ = response_head;
36 }
37 void OnStartLoadingResponseBody(
38 mojo::ScopedDataPipeConsumerHandle body) override {
39 response_body_ = std::move(body);
40 }
41 void OnComplete(const ResourceRequestCompletionStatus& status) override {
42 has_received_completion_ = true;
43 completion_status_ = status;
44 }
45
46 bool has_received_response() const { return has_received_response_; }
47 bool has_received_completion() const { return has_received_completion_; }
48 const ResourceResponseHead& response_head() const { return response_head_; }
49 mojo::Handle response_body() { return response_body_.get(); }
50 const ResourceRequestCompletionStatus& completion_status() const {
51 return completion_status_;
52 }
53
54 mojom::URLLoaderClientPtr CreateInterfacePtrAndBind() {
55 return binding_.CreateInterfacePtrAndBind();
56 }
57
58 private:
59 mojo::Binding<mojom::URLLoaderClient> binding_;
60 ResourceResponseHead response_head_;
61 mojo::ScopedDataPipeConsumerHandle response_body_;
62 ResourceRequestCompletionStatus completion_status_;
63 bool has_received_response_ = false;
64 bool has_received_completion_ = false;
65 };
66
67 class TestURLRequestJobFactory : public net::URLRequestJobFactory {
68 public:
69 using Callback = base::Callback<net::URLRequestJob*(
70 const std::string& scheme,
71 net::URLRequest* request,
72 net::NetworkDelegate* network_delegate)>;
73
74 TestURLRequestJobFactory() : callback_(base::Bind(&ConstNull)) {}
75 void SetCallback(Callback callback) { callback_ = callback; }
76
77 net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
78 const std::string& scheme,
79 net::URLRequest* request,
80 net::NetworkDelegate* delegate) const override {
81 return callback_.Run(scheme, request, delegate);
82 }
83
84 net::URLRequestJob* MaybeInterceptRedirect(
85 net::URLRequest* request,
86 net::NetworkDelegate* delegate,
87 const GURL& location) const override {
88 return nullptr;
89 }
90
91 net::URLRequestJob* MaybeInterceptResponse(
92 net::URLRequest* request,
93 net::NetworkDelegate* delegate) const override {
94 return nullptr;
95 }
96
97 bool IsHandledProtocol(const std::string& scheme) const override {
98 return true;
99 }
100
101 bool IsHandledURL(const GURL& url) const override { return true; }
102
103 bool IsSafeRedirectTarget(const GURL& location) const override {
104 return false;
105 }
106
107 private:
108 static net::URLRequestJob* ConstNull(const std::string& scheme,
109 net::URLRequest* request,
110 net::NetworkDelegate* delegate) {
111 return nullptr;
112 }
113
114 Callback callback_;
115
116 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJobFactory);
117 };
118
119 class URLLoaderFactoryImplTest : public ::testing::Test {
120 public:
121 URLLoaderFactoryImplTest()
122 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
123 browser_context_(new TestBrowserContext()),
124 resource_message_filter_(new ResourceMessageFilter(
125 0,
126 0,
127 nullptr,
128 nullptr,
129 nullptr,
130 nullptr,
131 base::Bind(&URLLoaderFactoryImplTest::GetContexts,
132 base::Unretained(this)))) {
133 net::URLRequestContext* request_context =
134 browser_context_->GetResourceContext()->GetRequestContext();
135 job_factory_.reset(new TestURLRequestJobFactory());
136 request_context->set_job_factory(job_factory_.get());
137 request_context->set_network_delegate(&network_delegate_);
138
139 factory_impl_retainer_.reset(new URLLoaderFactoryImpl(
140 resource_message_filter_, mojo::GetProxy(&factory_)));
141 base::RunLoop().RunUntilIdle();
142 }
143
144 ~URLLoaderFactoryImplTest() override {
145 rdh_.CancelRequestsForProcess(resource_message_filter_->child_id());
146 base::RunLoop().RunUntilIdle();
147 }
148
149 void GetContexts(ResourceType resource_type,
150 int origin_pid,
151 ResourceContext** resource_context,
152 net::URLRequestContext** request_context) {
153 *resource_context = browser_context_->GetResourceContext();
154 *request_context =
155 browser_context_->GetResourceContext()->GetRequestContext();
156 }
157
158 net::URLRequestJob* RecordURLRequest(const std::string& scheme,
159 net::URLRequest* request,
160 net::NetworkDelegate* delegate) {
161 url_request_job_factory_url_ = request->url();
162 url_request_job_factory_method_ = request->method();
163 return new net::URLRequestFailedJob(request, delegate, net::ERR_IO_PENDING);
164 }
165
166 static net::URLRequestJob* CreateSuccessfulJob(
167 const std::string& scheme,
168 net::URLRequest* request,
169 net::NetworkDelegate* delegate) {
170 return new net::URLRequestTestJob(request, delegate,
171 net::URLRequestTestJob::test_headers(),
172 "hello", true);
173 }
174
175 static net::URLRequestJob* CreateFailedJob(const std::string& scheme,
176 net::URLRequest* request,
177 net::NetworkDelegate* delegate) {
178 return new net::URLRequestFailedJob(request, delegate, net::ERR_FAILED);
179 }
180
181 TestBrowserThreadBundle thread_bundle_;
182 ResourceDispatcherHostImpl rdh_;
183 std::unique_ptr<TestBrowserContext> browser_context_;
184 std::unique_ptr<TestURLRequestJobFactory> job_factory_;
185 net::TestNetworkDelegate network_delegate_;
186 scoped_refptr<ResourceMessageFilter> resource_message_filter_;
187
188 mojom::URLLoaderFactoryPtr factory_;
189 std::unique_ptr<URLLoaderFactoryImpl> factory_impl_retainer_;
190
191 GURL url_request_job_factory_url_;
192 std::string url_request_job_factory_method_;
193 };
194
195 TEST_F(URLLoaderFactoryImplTest, Request) {
196 mojom::URLLoaderPtr loader;
197 factory_->CreateURLLoader(mojo::GetProxy(&loader));
198
199 job_factory_->SetCallback(base::Bind(
200 &URLLoaderFactoryImplTest::RecordURLRequest, base::Unretained(this)));
201
202 ResourceRequest request;
203 URLLoaderClientImpl client;
204 request.url = GURL("http://www.example.com/foo/bar");
205 request.method = "GET";
206 loader->Load(1, request, client.CreateInterfacePtrAndBind());
207
208 base::RunLoop().RunUntilIdle();
209 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {
210 }
211 base::RunLoop().RunUntilIdle();
212
213 EXPECT_FALSE(client.has_received_response());
214 EXPECT_FALSE(client.has_received_completion());
215 EXPECT_FALSE(client.response_body().is_valid());
216 EXPECT_EQ(request.url, url_request_job_factory_url_);
217 EXPECT_EQ(request.method, url_request_job_factory_method_);
218 }
219
220 TEST_F(URLLoaderFactoryImplTest, GetResponse) {
221 mojom::URLLoaderPtr loader;
222 factory_->CreateURLLoader(mojo::GetProxy(&loader));
223
224 job_factory_->SetCallback(base::Bind(CreateSuccessfulJob));
225
226 ResourceRequest request;
227 URLLoaderClientImpl client;
228 request.url = GURL("http://www.example.com/");
229 request.method = "GET";
230 loader->Load(1, request, client.CreateInterfacePtrAndBind());
231
232 EXPECT_FALSE(client.has_received_response());
233 EXPECT_FALSE(client.has_received_completion());
234 base::RunLoop().RunUntilIdle();
235 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {
236 }
237 base::RunLoop().RunUntilIdle();
238
239 ASSERT_TRUE(client.has_received_response());
240 EXPECT_EQ(200, client.response_head().headers->response_code());
241 std::string content_type;
242 client.response_head().headers->GetNormalizedHeader("content-type",
243 &content_type);
244 EXPECT_EQ("text/html", content_type);
245
246 ASSERT_TRUE(client.has_received_completion());
247 EXPECT_TRUE(client.response_body().is_valid());
248 EXPECT_EQ(0, client.completion_status().error_code);
249 }
250
251 TEST_F(URLLoaderFactoryImplTest, GetFailedResponse) {
252 mojom::URLLoaderPtr loader;
253 factory_->CreateURLLoader(mojo::GetProxy(&loader));
254
255 job_factory_->SetCallback(base::Bind(CreateFailedJob));
256
257 ResourceRequest request;
258 URLLoaderClientImpl client;
259 request.url = GURL("http://www.example.com/");
260 request.method = "GET";
261 loader->Load(1, request, client.CreateInterfacePtrAndBind());
262
263 EXPECT_FALSE(client.has_received_response());
264 EXPECT_FALSE(client.has_received_completion());
265 base::RunLoop().RunUntilIdle();
266 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {
267 }
268 base::RunLoop().RunUntilIdle();
269
270 ASSERT_FALSE(client.has_received_response());
271 ASSERT_TRUE(client.has_received_completion());
272 EXPECT_FALSE(client.response_body().is_valid());
273 EXPECT_EQ(net::ERR_FAILED, client.completion_status().error_code);
274 }
275
276 } // namespace
277
278 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698