Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 "base/command_line.h" | |
| 6 #include "base/memory/ref_counted.h" | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "content/browser/frame_host/navigation_request_info.h" | |
| 10 #include "content/browser/loader/navigation_url_loader_impl.h" | |
| 11 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
| 12 #include "content/browser/streams/stream.h" | |
| 13 #include "content/browser/streams/stream_context.h" | |
| 14 #include "content/browser/streams/stream_registry.h" | |
| 15 #include "content/browser/streams/stream_url_request_job.h" | |
| 16 #include "content/common/navigation_params.h" | |
| 17 #include "content/public/browser/browser_context.h" | |
| 18 #include "content/public/browser/resource_context.h" | |
| 19 #include "content/public/browser/resource_dispatcher_host_delegate.h" | |
| 20 #include "content/public/browser/stream_handle.h" | |
| 21 #include "content/public/common/content_switches.h" | |
| 22 #include "content/public/common/resource_response.h" | |
| 23 #include "content/public/test/test_browser_context.h" | |
| 24 #include "content/public/test/test_browser_thread_bundle.h" | |
| 25 #include "net/base/net_errors.h" | |
| 26 #include "net/http/http_response_headers.h" | |
| 27 #include "net/url_request/redirect_info.h" | |
| 28 #include "net/url_request/url_request.h" | |
| 29 #include "net/url_request/url_request_context.h" | |
| 30 #include "net/url_request/url_request_job_factory_impl.h" | |
| 31 #include "net/url_request/url_request_test_job.h" | |
| 32 #include "net/url_request/url_request_test_util.h" | |
| 33 #include "testing/gtest/include/gtest/gtest.h" | |
| 34 | |
| 35 namespace content { | |
| 36 | |
| 37 namespace { | |
| 38 | |
| 39 class StreamProtocolHandler | |
| 40 : public net::URLRequestJobFactory::ProtocolHandler { | |
| 41 public: | |
| 42 StreamProtocolHandler(StreamRegistry* registry) : registry_(registry) {} | |
| 43 | |
| 44 // net::URLRequestJobFactory::ProtocolHandler implementation. | |
| 45 virtual net::URLRequestJob* MaybeCreateJob( | |
| 46 net::URLRequest* request, | |
| 47 net::NetworkDelegate* network_delegate) const override { | |
| 48 scoped_refptr<Stream> stream = registry_->GetStream(request->url()); | |
| 49 if (stream.get()) | |
| 50 return new StreamURLRequestJob(request, network_delegate, stream); | |
| 51 return nullptr; | |
| 52 } | |
| 53 private: | |
| 54 StreamRegistry* registry_; | |
| 55 }; | |
| 56 | |
| 57 class TestNavigationURLLoaderDelegate : public NavigationURLLoader::Delegate { | |
| 58 public: | |
| 59 TestNavigationURLLoaderDelegate() | |
| 60 : net_error_(0), | |
| 61 request_redirected_(new base::RunLoop), | |
| 62 response_started_(new base::RunLoop), | |
| 63 request_failed_(new base::RunLoop) { | |
| 64 } | |
| 65 | |
| 66 const net::RedirectInfo& redirect_info() const { return redirect_info_; } | |
| 67 ResourceResponse* response() const { return response_.get(); } | |
| 68 StreamHandle* body() const { return body_.get(); } | |
| 69 int net_error() const { return net_error_; } | |
| 70 | |
| 71 void WaitForRequestRedirected() { | |
| 72 request_redirected_->Run(); | |
| 73 request_redirected_.reset(new base::RunLoop); | |
| 74 } | |
| 75 | |
| 76 void WaitForResponseStarted() { | |
| 77 response_started_->Run(); | |
| 78 response_started_.reset(new base::RunLoop); | |
| 79 } | |
| 80 | |
| 81 void WaitForRequestFailed() { | |
| 82 request_failed_->Run(); | |
| 83 request_failed_.reset(new base::RunLoop); | |
| 84 } | |
| 85 | |
| 86 void ReleaseBody() { | |
| 87 body_.reset(); | |
| 88 } | |
| 89 | |
| 90 // NavigationURLLoader::Delegate implementation. | |
| 91 virtual void OnRequestRedirected(const net::RedirectInfo& redirect_info, | |
| 92 ResourceResponse* response) override { | |
| 93 redirect_info_ = redirect_info; | |
| 94 response_ = response; | |
| 95 request_redirected_->Quit(); | |
| 96 } | |
| 97 | |
| 98 virtual void OnResponseStarted(ResourceResponse* response, | |
| 99 scoped_ptr<StreamHandle> body) override { | |
| 100 response_ = response; | |
| 101 body_ = body.Pass(); | |
| 102 response_started_->Quit(); | |
| 103 } | |
| 104 | |
| 105 virtual void OnRequestFailed(int net_error) override { | |
| 106 net_error_ = net_error; | |
| 107 request_failed_->Quit(); | |
| 108 } | |
| 109 | |
| 110 private: | |
| 111 net::RedirectInfo redirect_info_; | |
| 112 scoped_refptr<ResourceResponse> response_; | |
| 113 scoped_ptr<StreamHandle> body_; | |
| 114 int net_error_; | |
| 115 | |
| 116 scoped_ptr<base::RunLoop> request_redirected_; | |
| 117 scoped_ptr<base::RunLoop> response_started_; | |
| 118 scoped_ptr<base::RunLoop> request_failed_; | |
| 119 }; | |
| 120 | |
| 121 class RequestBlockingResourceDispatcherHostDelegate | |
| 122 : public ResourceDispatcherHostDelegate { | |
| 123 public: | |
| 124 // ResourceDispatcherHostDelegate implementation: | |
| 125 virtual bool ShouldBeginRequest(const std::string& method, | |
| 126 const GURL& url, | |
| 127 ResourceType resource_type, | |
| 128 ResourceContext* resource_context) override { | |
| 129 return false; | |
| 130 } | |
| 131 }; | |
| 132 | |
| 133 } // namespace | |
| 134 | |
| 135 class NavigationURLLoaderTest : public testing::Test { | |
| 136 public: | |
| 137 NavigationURLLoaderTest() | |
| 138 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | |
| 139 browser_context_(new TestBrowserContext) { | |
| 140 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); | |
| 141 base::RunLoop().RunUntilIdle(); | |
| 142 net::URLRequestContext* request_context = | |
| 143 browser_context_->GetResourceContext()->GetRequestContext(); | |
| 144 // Attach URLRequestTestJob and make streams work. | |
| 145 job_factory_.SetProtocolHandler( | |
| 146 "test", net::URLRequestTestJob::CreateProtocolHandler()); | |
| 147 job_factory_.SetProtocolHandler( | |
| 148 "blob", new StreamProtocolHandler( | |
| 149 StreamContext::GetFor(browser_context_.get())->registry())); | |
| 150 request_context->set_job_factory(&job_factory_); | |
| 151 | |
| 152 // NavigationURLLoader is only used for browser-side navigations. | |
| 153 CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 154 switches::kEnableBrowserSideNavigation); | |
| 155 } | |
| 156 | |
| 157 scoped_ptr<NavigationURLLoader> MakeTestLoader( | |
| 158 const GURL& url, | |
| 159 NavigationURLLoader::Delegate* delegate) { | |
| 160 FrameHostMsg_BeginNavigation_Params begin_params; | |
| 161 CommonNavigationParams common_params; | |
| 162 begin_params.method = "GET"; | |
| 163 common_params.url = url; | |
| 164 scoped_ptr<NavigationRequestInfo> request_info( | |
| 165 new NavigationRequestInfo(begin_params)); | |
| 166 request_info->first_party_for_cookies = url; | |
| 167 request_info->is_main_frame = true; | |
| 168 | |
| 169 return NavigationURLLoader::Create( | |
| 170 browser_context_.get(), 0, | |
| 171 common_params, request_info.Pass(), nullptr, delegate); | |
| 172 } | |
| 173 | |
| 174 // Helper function for fetching the body of a URL to a string. | |
| 175 std::string FetchURL(const GURL& url) { | |
| 176 net::TestDelegate delegate; | |
| 177 net::URLRequestContext* request_context = | |
| 178 browser_context_->GetResourceContext()->GetRequestContext(); | |
| 179 scoped_ptr<net::URLRequest> request(request_context->CreateRequest( | |
| 180 url, net::DEFAULT_PRIORITY, &delegate, nullptr)); | |
| 181 request->Start(); | |
| 182 base::RunLoop().Run(); | |
| 183 | |
| 184 EXPECT_TRUE(request->status().is_success()); | |
| 185 EXPECT_EQ(200, request->response_headers()->response_code()); | |
| 186 return delegate.data_received(); | |
| 187 } | |
| 188 | |
| 189 protected: | |
| 190 TestBrowserThreadBundle thread_bundle_; | |
| 191 net::URLRequestJobFactoryImpl job_factory_; | |
| 192 scoped_ptr<TestBrowserContext> browser_context_; | |
| 193 ResourceDispatcherHostImpl host_; | |
| 194 }; | |
| 195 | |
| 196 // Tests that a basic request works. | |
| 197 TEST_F(NavigationURLLoaderTest, Basic) { | |
| 198 TestNavigationURLLoaderDelegate delegate; | |
| 199 scoped_ptr<NavigationURLLoader> loader = | |
| 200 MakeTestLoader(net::URLRequestTestJob::test_url_1(), &delegate); | |
| 201 | |
| 202 // Wait for the response to come back. | |
| 203 delegate.WaitForResponseStarted(); | |
| 204 | |
| 205 // Check the response is correct. | |
| 206 EXPECT_EQ("text/html", delegate.response()->head.mime_type); | |
| 207 EXPECT_EQ("HTTP/1.1 200 OK", | |
| 208 delegate.response()->head.headers->GetStatusLine()); | |
| 209 | |
| 210 // Check the body is correct. | |
| 211 EXPECT_EQ(net::URLRequestTestJob::test_data_1(), | |
| 212 FetchURL(delegate.body()->GetURL())); | |
| 213 } | |
| 214 | |
| 215 // Tests that request failures are propogated correctly. | |
|
clamy
2014/10/13 15:22:42
s/propogated/propagated
davidben
2014/10/15 22:53:28
Done. (Somehow I never spell that word correctly.)
| |
| 216 TEST_F(NavigationURLLoaderTest, RequestFailed) { | |
| 217 TestNavigationURLLoaderDelegate delegate; | |
| 218 scoped_ptr<NavigationURLLoader> loader = | |
| 219 MakeTestLoader(GURL("bogus:bogus"), &delegate); | |
| 220 | |
| 221 // Wait for the request to fail as expected. | |
| 222 delegate.WaitForRequestFailed(); | |
| 223 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, delegate.net_error()); | |
| 224 } | |
| 225 | |
| 226 // Test that redirects are sent to the delegate. | |
| 227 TEST_F(NavigationURLLoaderTest, RequestRedirected) { | |
| 228 // Fake a top-level request. Choose a URL which redirects so the request can | |
| 229 // be paused before the response comes in. | |
| 230 TestNavigationURLLoaderDelegate delegate; | |
| 231 scoped_ptr<NavigationURLLoader> loader = | |
| 232 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), | |
| 233 &delegate); | |
| 234 | |
| 235 // Wait for the request to redirect. | |
| 236 delegate.WaitForRequestRedirected(); | |
| 237 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), | |
| 238 delegate.redirect_info().new_url); | |
| 239 EXPECT_EQ("GET", delegate.redirect_info().new_method); | |
| 240 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), | |
| 241 delegate.redirect_info().new_first_party_for_cookies); | |
| 242 EXPECT_EQ("HTTP/1.1 302 MOVED", | |
| 243 delegate.response()->head.headers->GetStatusLine()); | |
| 244 | |
| 245 // Wait for the response to complete. | |
| 246 loader->FollowRedirect(); | |
| 247 base::RunLoop().RunUntilIdle(); | |
| 248 EXPECT_TRUE(net::URLRequestTestJob::ProcessOnePendingMessage()); | |
| 249 delegate.WaitForResponseStarted(); | |
| 250 | |
| 251 // Check the response is correct. | |
| 252 EXPECT_EQ("text/html", delegate.response()->head.mime_type); | |
| 253 EXPECT_EQ("HTTP/1.1 200 OK", | |
| 254 delegate.response()->head.headers->GetStatusLine()); | |
| 255 | |
| 256 // Check the body is correct. | |
| 257 EXPECT_EQ(net::URLRequestTestJob::test_data_2(), | |
| 258 FetchURL(delegate.body()->GetURL())); | |
| 259 } | |
| 260 | |
| 261 // Tests that the destroying the loader cancels the request. | |
| 262 TEST_F(NavigationURLLoaderTest, CancelOnDestruct) { | |
| 263 // Fake a top-level request. Choose a URL which redirects so the request can | |
| 264 // be paused before the response comes in. | |
| 265 TestNavigationURLLoaderDelegate delegate; | |
| 266 scoped_ptr<NavigationURLLoader> loader = | |
| 267 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), | |
| 268 &delegate); | |
| 269 | |
| 270 // Wait for the request to redirect. | |
| 271 delegate.WaitForRequestRedirected(); | |
| 272 | |
| 273 // Destroy the loader and verify that URLRequestTestJob no longer has anything | |
| 274 // paused. | |
| 275 loader.reset(); | |
| 276 base::RunLoop().RunUntilIdle(); | |
| 277 EXPECT_FALSE(net::URLRequestTestJob::ProcessOnePendingMessage()); | |
| 278 } | |
| 279 | |
| 280 // Test that the delegate is not called if OnResponseStarted and destroying the | |
| 281 // loader race. | |
| 282 TEST_F(NavigationURLLoaderTest, CancelResponseRace) { | |
| 283 TestNavigationURLLoaderDelegate delegate; | |
| 284 scoped_ptr<NavigationURLLoader> loader = | |
| 285 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), | |
| 286 &delegate); | |
| 287 | |
| 288 // Wait for the request to redirect. | |
| 289 delegate.WaitForRequestRedirected(); | |
| 290 | |
| 291 // In the same event loop iteration, follow the redirect (allowing the | |
| 292 // response to go through) and destroy the loader. | |
| 293 loader->FollowRedirect(); | |
| 294 loader.reset(); | |
| 295 | |
| 296 // Verify the URLRequestTestJob no longer has anything paused and that no | |
| 297 // response body was received. | |
| 298 base::RunLoop().RunUntilIdle(); | |
| 299 EXPECT_FALSE(net::URLRequestTestJob::ProcessOnePendingMessage()); | |
| 300 EXPECT_FALSE(delegate.body()); | |
| 301 } | |
| 302 | |
| 303 // Tests that the loader may be canceled by context. | |
| 304 TEST_F(NavigationURLLoaderTest, CancelByContext) { | |
| 305 TestNavigationURLLoaderDelegate delegate; | |
| 306 scoped_ptr<NavigationURLLoader> loader = | |
| 307 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), | |
| 308 &delegate); | |
| 309 | |
| 310 // Wait for the request to redirect. | |
| 311 delegate.WaitForRequestRedirected(); | |
| 312 | |
| 313 // Cancel all requests. | |
| 314 host_.CancelRequestsForContext(browser_context_->GetResourceContext()); | |
| 315 | |
| 316 // Wait for the request to now be aborted. | |
| 317 delegate.WaitForRequestFailed(); | |
| 318 EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); | |
| 319 } | |
| 320 | |
| 321 // Tests that, if the request is blocked by the ResourceDispatcherHostDelegate, | |
| 322 // the caller is informed appropriately. | |
| 323 TEST_F(NavigationURLLoaderTest, RequestBlocked) { | |
| 324 RequestBlockingResourceDispatcherHostDelegate rdh_delegate; | |
| 325 host_.SetDelegate(&rdh_delegate); | |
| 326 | |
| 327 TestNavigationURLLoaderDelegate delegate; | |
| 328 scoped_ptr<NavigationURLLoader> loader = | |
| 329 MakeTestLoader(net::URLRequestTestJob::test_url_1(), &delegate); | |
| 330 | |
| 331 // Wait for the request to fail as expected. | |
| 332 delegate.WaitForRequestFailed(); | |
| 333 EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); | |
| 334 | |
| 335 host_.SetDelegate(nullptr); | |
| 336 } | |
| 337 | |
| 338 // Tests that ownership leaves the loader once the response is received. | |
| 339 TEST_F(NavigationURLLoaderTest, LoaderDetached) { | |
| 340 // Fake a top-level request to a URL whose body does not load immediately. | |
| 341 TestNavigationURLLoaderDelegate delegate; | |
| 342 scoped_ptr<NavigationURLLoader> loader = | |
| 343 MakeTestLoader(net::URLRequestTestJob::test_url_2(), &delegate); | |
| 344 | |
| 345 // Wait for the response to come back. | |
| 346 delegate.WaitForResponseStarted(); | |
| 347 | |
| 348 // Check the response is correct. | |
| 349 EXPECT_EQ("text/html", delegate.response()->head.mime_type); | |
| 350 EXPECT_EQ("HTTP/1.1 200 OK", | |
| 351 delegate.response()->head.headers->GetStatusLine()); | |
| 352 | |
| 353 // Destroy the loader. | |
| 354 loader.reset(); | |
| 355 base::RunLoop().RunUntilIdle(); | |
| 356 | |
| 357 // Check the body can still be fetched through the StreamHandle. | |
| 358 EXPECT_TRUE(net::URLRequestTestJob::ProcessOnePendingMessage()); | |
| 359 EXPECT_EQ(net::URLRequestTestJob::test_data_2(), | |
| 360 FetchURL(delegate.body()->GetURL())); | |
| 361 } | |
| 362 | |
| 363 // Tests that the request is owned by the body StreamHandle. | |
| 364 TEST_F(NavigationURLLoaderTest, OwnedByHandle) { | |
| 365 // Fake a top-level request to a URL whose body does not load immediately. | |
| 366 TestNavigationURLLoaderDelegate delegate; | |
| 367 scoped_ptr<NavigationURLLoader> loader = | |
| 368 MakeTestLoader(net::URLRequestTestJob::test_url_2(), &delegate); | |
| 369 | |
| 370 // Wait for the response to come back. | |
| 371 delegate.WaitForResponseStarted(); | |
| 372 | |
| 373 // Release the body. | |
| 374 delegate.ReleaseBody(); | |
| 375 base::RunLoop().RunUntilIdle(); | |
| 376 | |
| 377 // Verify that URLRequestTestJob no longer has anything paused. | |
| 378 EXPECT_FALSE(net::URLRequestTestJob::ProcessOnePendingMessage()); | |
| 379 } | |
| 380 | |
| 381 } // namespace content | |
| OLD | NEW |