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

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 protected:
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 nullptr,
130 base::Bind(&URLLoaderFactoryImplTest::GetContexts,
131 base::Unretained(this)))) {
132 net::URLRequestContext* request_context =
133 browser_context_->GetResourceContext()->GetRequestContext();
134 job_factory_.reset(new TestURLRequestJobFactory());
135 request_context->set_job_factory(job_factory_.get());
136 request_context->set_network_delegate(&network_delegate_);
137
138 factory_impl_retainer_.reset(new URLLoaderFactoryImpl(
139 resource_message_filter_, mojo::GetProxy(&factory_)));
140 base::RunLoop().RunUntilIdle();
141 }
142
143 ~URLLoaderFactoryImplTest() override {
144 rdh_.CancelRequestsForProcess(resource_message_filter_->child_id());
145 base::RunLoop().RunUntilIdle();
146 }
147
148 void GetContexts(ResourceType resource_type,
149 int origin_pid,
150 ResourceContext** resource_context,
151 net::URLRequestContext** request_context) {
152 *resource_context = browser_context_->GetResourceContext();
153 *request_context =
154 browser_context_->GetResourceContext()->GetRequestContext();
155 }
156
157 net::URLRequestJob* RecordURLRequest(const std::string& scheme,
158 net::URLRequest* request,
159 net::NetworkDelegate* delegate) {
160 url_request_job_factory_url_ = request->url();
161 url_request_job_factory_method_ = request->method();
162 return nullptr;
163 }
164
165 static net::URLRequestJob* CreateSuccessfulJob(
166 const std::string& scheme,
167 net::URLRequest* request,
168 net::NetworkDelegate* delegate) {
169 return new net::URLRequestTestJob(request, delegate,
170 net::URLRequestTestJob::test_headers(),
171 "hello", true);
172 }
173
174 static net::URLRequestJob* CreateFailedJob(const std::string& scheme,
175 net::URLRequest* request,
176 net::NetworkDelegate* delegate) {
177 return new net::URLRequestFailedJob(request, delegate, net::ERR_FAILED);
178 }
179
180 TestBrowserThreadBundle thread_bundle_;
181 ResourceDispatcherHostImpl rdh_;
182 std::unique_ptr<TestBrowserContext> browser_context_;
183 std::unique_ptr<TestURLRequestJobFactory> job_factory_;
184 net::TestNetworkDelegate network_delegate_;
185 scoped_refptr<ResourceMessageFilter> resource_message_filter_;
186
187 mojom::URLLoaderFactoryPtr factory_;
188 std::unique_ptr<URLLoaderFactoryImpl> factory_impl_retainer_;
189
190 GURL url_request_job_factory_url_;
191 std::string url_request_job_factory_method_;
192 };
193
194 TEST_F(URLLoaderFactoryImplTest, Request) {
195 mojom::URLLoaderPtr loader;
196 factory_->CreateURLLoader(mojo::GetProxy(&loader));
197
198 job_factory_->SetCallback(base::Bind(
199 &std::remove_reference<decltype(*this)>::type::RecordURLRequest,
yhirano 2016/05/17 14:58:54 I tried to avoid compile errors with this but it t
200 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, 2, 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_EQ(request.url, url_request_job_factory_url_);
216 EXPECT_EQ(request.method, url_request_job_factory_method_);
217 }
218
219 TEST_F(URLLoaderFactoryImplTest, GetResponse) {
220 mojom::URLLoaderPtr loader;
221 factory_->CreateURLLoader(mojo::GetProxy(&loader));
222
223 job_factory_->SetCallback(base::Bind(CreateSuccessfulJob));
224
225 ResourceRequest request;
226 URLLoaderClientImpl client;
227 request.url = GURL("http://www.example.com/");
228 request.method = "GET";
229 loader->Load(1, 2, request, client.CreateInterfacePtrAndBind());
230
231 EXPECT_FALSE(client.has_received_response());
232 EXPECT_FALSE(client.has_received_completion());
233 base::RunLoop().RunUntilIdle();
234 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {
235 }
236 base::RunLoop().RunUntilIdle();
237
238 ASSERT_TRUE(client.has_received_response());
239 EXPECT_EQ(200, client.response_head().headers->response_code());
240 std::string content_type;
241 client.response_head().headers->GetNormalizedHeader("content-type",
242 &content_type);
243 EXPECT_EQ("text/html", content_type);
244
245 ASSERT_TRUE(client.has_received_completion());
246 EXPECT_EQ(0, client.completion_status().error_code);
247 }
248
249 TEST_F(URLLoaderFactoryImplTest, GetFailedResponse) {
250 mojom::URLLoaderPtr loader;
251 factory_->CreateURLLoader(mojo::GetProxy(&loader));
252
253 job_factory_->SetCallback(base::Bind(CreateFailedJob));
254
255 ResourceRequest request;
256 URLLoaderClientImpl client;
257 request.url = GURL("http://www.example.com/");
258 request.method = "GET";
259 loader->Load(1, 2, request, client.CreateInterfacePtrAndBind());
260
261 EXPECT_FALSE(client.has_received_response());
262 EXPECT_FALSE(client.has_received_completion());
263 base::RunLoop().RunUntilIdle();
264 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {
265 }
266 base::RunLoop().RunUntilIdle();
267
268 ASSERT_FALSE(client.has_received_response());
269 ASSERT_TRUE(client.has_received_completion());
270 EXPECT_EQ(net::ERR_FAILED, client.completion_status().error_code);
271 }
272
273 } // namespace
274
275 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/url_loader_factory_impl.cc ('k') | content/browser/renderer_host/render_process_host_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698