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

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

Powered by Google App Engine
This is Rietveld 408576698