Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 { | |
|
mmenke
2016/05/26 16:28:29
I'd suggest giving this at least a marginally more
yhirano
2016/06/01 14:40:52
Done.
| |
| 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; | |
|
mmenke
2016/05/26 16:28:29
DISALLOW_COPY_AND_ASSIGN
yhirano
2016/06/01 14:40:52
Done.
| |
| 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; } | |
|
mmenke
2016/05/26 16:28:29
nit: const Callback&
Note that I made two sugges
yhirano
2016/06/01 14:40:52
Deleted
| |
| 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 } | |
|
mmenke
2016/05/26 16:28:29
Suggest just checking if the callback_ is_null (is
yhirano
2016/06/01 14:40:52
Deleted
| |
| 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 nullptr, | |
| 132 base::Bind(&URLLoaderFactoryImplTest::GetContexts, | |
| 133 base::Unretained(this)))) { | |
| 134 net::URLRequestContext* request_context = | |
| 135 browser_context_->GetResourceContext()->GetRequestContext(); | |
| 136 job_factory_.reset(new TestURLRequestJobFactory()); | |
| 137 request_context->set_job_factory(job_factory_.get()); | |
|
mmenke
2016/05/26 16:28:29
Modifying URLRequestContexts you didn't just creat
yhirano
2016/06/01 14:40:52
Done.
| |
| 138 request_context->set_network_delegate(&network_delegate_); | |
|
mmenke
2016/05/26 16:28:29
Why do you need to change the NetworkDelegate?
yhirano
2016/06/01 14:40:51
Deleted
| |
| 139 | |
| 140 factory_impl_retainer_.reset(new URLLoaderFactoryImpl( | |
| 141 resource_message_filter_, mojo::GetProxy(&factory_))); | |
| 142 base::RunLoop().RunUntilIdle(); | |
| 143 } | |
| 144 | |
| 145 ~URLLoaderFactoryImplTest() override { | |
| 146 rdh_.CancelRequestsForProcess(resource_message_filter_->child_id()); | |
| 147 base::RunLoop().RunUntilIdle(); | |
| 148 } | |
| 149 | |
| 150 void GetContexts(ResourceType resource_type, | |
| 151 int origin_pid, | |
| 152 ResourceContext** resource_context, | |
| 153 net::URLRequestContext** request_context) { | |
| 154 *resource_context = browser_context_->GetResourceContext(); | |
| 155 *request_context = | |
| 156 browser_context_->GetResourceContext()->GetRequestContext(); | |
| 157 } | |
| 158 | |
| 159 net::URLRequestJob* RecordURLRequest(const std::string& scheme, | |
| 160 net::URLRequest* request, | |
| 161 net::NetworkDelegate* delegate) { | |
| 162 url_request_job_factory_url_ = request->url(); | |
| 163 url_request_job_factory_method_ = request->method(); | |
|
mmenke
2016/05/26 16:28:29
Any reason to not just record these on every reque
mmenke
2016/05/26 16:28:29
Also, perhaps count URLRequests, to see if we see
yhirano
2016/06/01 14:40:52
Deleted. Updating variables in const functions is
| |
| 164 return new net::URLRequestFailedJob(request, delegate, net::ERR_IO_PENDING); | |
| 165 } | |
| 166 | |
| 167 static net::URLRequestJob* CreateSuccessfulJob( | |
| 168 const std::string& scheme, | |
| 169 net::URLRequest* request, | |
| 170 net::NetworkDelegate* delegate) { | |
| 171 return new net::URLRequestTestJob(request, delegate, | |
| 172 net::URLRequestTestJob::test_headers(), | |
| 173 "hello", true); | |
|
mmenke
2016/05/26 16:28:29
I'd recommend against using URLRequestTestJob - th
yhirano
2016/06/01 14:40:52
Done.
| |
| 174 } | |
| 175 | |
| 176 static net::URLRequestJob* CreateFailedJob(const std::string& scheme, | |
| 177 net::URLRequest* request, | |
| 178 net::NetworkDelegate* delegate) { | |
| 179 return new net::URLRequestFailedJob(request, delegate, net::ERR_FAILED); | |
| 180 } | |
| 181 | |
| 182 TestBrowserThreadBundle thread_bundle_; | |
| 183 ResourceDispatcherHostImpl rdh_; | |
| 184 std::unique_ptr<TestBrowserContext> browser_context_; | |
| 185 std::unique_ptr<TestURLRequestJobFactory> job_factory_; | |
| 186 net::TestNetworkDelegate network_delegate_; | |
| 187 scoped_refptr<ResourceMessageFilter> resource_message_filter_; | |
| 188 | |
| 189 mojom::URLLoaderFactoryPtr factory_; | |
| 190 std::unique_ptr<URLLoaderFactoryImpl> factory_impl_retainer_; | |
| 191 | |
| 192 GURL url_request_job_factory_url_; | |
| 193 std::string url_request_job_factory_method_; | |
| 194 }; | |
| 195 | |
| 196 TEST_F(URLLoaderFactoryImplTest, Request) { | |
| 197 mojom::URLLoaderPtr loader; | |
| 198 factory_->CreateURLLoader(mojo::GetProxy(&loader)); | |
| 199 | |
| 200 job_factory_->SetCallback(base::Bind( | |
| 201 &URLLoaderFactoryImplTest::RecordURLRequest, base::Unretained(this))); | |
| 202 | |
| 203 ResourceRequest request; | |
| 204 URLLoaderClientImpl client; | |
| 205 request.url = GURL("http://www.example.com/foo/bar"); | |
| 206 request.method = "GET"; | |
| 207 loader->Start(1, request, client.CreateInterfacePtrAndBind()); | |
| 208 | |
| 209 base::RunLoop().RunUntilIdle(); | |
| 210 while (net::URLRequestTestJob::ProcessOnePendingMessage()) { | |
| 211 } | |
| 212 base::RunLoop().RunUntilIdle(); | |
| 213 | |
| 214 EXPECT_FALSE(client.has_received_response()); | |
| 215 EXPECT_FALSE(client.has_received_completion()); | |
| 216 EXPECT_FALSE(client.response_body().is_valid()); | |
| 217 EXPECT_EQ(request.url, url_request_job_factory_url_); | |
| 218 EXPECT_EQ(request.method, url_request_job_factory_method_); | |
| 219 } | |
| 220 | |
| 221 TEST_F(URLLoaderFactoryImplTest, GetResponse) { | |
| 222 mojom::URLLoaderPtr loader; | |
| 223 factory_->CreateURLLoader(mojo::GetProxy(&loader)); | |
| 224 | |
| 225 job_factory_->SetCallback(base::Bind(CreateSuccessfulJob)); | |
| 226 | |
| 227 ResourceRequest request; | |
| 228 URLLoaderClientImpl client; | |
| 229 request.url = GURL("http://www.example.com/"); | |
| 230 request.method = "GET"; | |
| 231 loader->Start(1, request, client.CreateInterfacePtrAndBind()); | |
| 232 | |
| 233 EXPECT_FALSE(client.has_received_response()); | |
| 234 EXPECT_FALSE(client.has_received_completion()); | |
| 235 base::RunLoop().RunUntilIdle(); | |
| 236 while (net::URLRequestTestJob::ProcessOnePendingMessage()) { | |
| 237 } | |
| 238 base::RunLoop().RunUntilIdle(); | |
|
mmenke
2016/05/26 16:28:29
All this spinning of the RunLoop is a bit of an an
yhirano
2016/06/01 14:40:52
Done.
| |
| 239 | |
| 240 ASSERT_TRUE(client.has_received_response()); | |
| 241 EXPECT_EQ(200, client.response_head().headers->response_code()); | |
| 242 std::string content_type; | |
| 243 client.response_head().headers->GetNormalizedHeader("content-type", | |
| 244 &content_type); | |
| 245 EXPECT_EQ("text/html", content_type); | |
| 246 | |
| 247 ASSERT_TRUE(client.has_received_completion()); | |
| 248 EXPECT_TRUE(client.response_body().is_valid()); | |
|
mmenke
2016/05/26 16:28:29
Shouldn't we actually read the response body in on
yhirano
2016/06/01 14:40:52
Done.
| |
| 249 EXPECT_EQ(0, client.completion_status().error_code); | |
| 250 } | |
| 251 | |
| 252 TEST_F(URLLoaderFactoryImplTest, GetFailedResponse) { | |
|
mmenke
2016/05/26 16:28:29
Other test suggestions:
* One that goes through Ab
yhirano
2016/06/01 14:40:52
Done.
| |
| 253 mojom::URLLoaderPtr loader; | |
| 254 factory_->CreateURLLoader(mojo::GetProxy(&loader)); | |
| 255 | |
| 256 job_factory_->SetCallback(base::Bind(CreateFailedJob)); | |
| 257 | |
| 258 ResourceRequest request; | |
| 259 URLLoaderClientImpl client; | |
| 260 request.url = GURL("http://www.example.com/"); | |
| 261 request.method = "GET"; | |
| 262 loader->Start(1, request, client.CreateInterfacePtrAndBind()); | |
| 263 | |
| 264 EXPECT_FALSE(client.has_received_response()); | |
| 265 EXPECT_FALSE(client.has_received_completion()); | |
| 266 base::RunLoop().RunUntilIdle(); | |
| 267 while (net::URLRequestTestJob::ProcessOnePendingMessage()) { | |
| 268 } | |
| 269 base::RunLoop().RunUntilIdle(); | |
| 270 | |
| 271 ASSERT_FALSE(client.has_received_response()); | |
| 272 ASSERT_TRUE(client.has_received_completion()); | |
| 273 EXPECT_FALSE(client.response_body().is_valid()); | |
| 274 EXPECT_EQ(net::ERR_FAILED, client.completion_status().error_code); | |
| 275 } | |
| 276 | |
| 277 } // namespace | |
| 278 | |
| 279 } // namespace content | |
| OLD | NEW |