Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "content/browser/frame_host/navigation_request_info.h" | 10 #include "content/browser/frame_host/navigation_request_info.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 | 55 |
| 56 private: | 56 private: |
| 57 StreamRegistry* registry_; | 57 StreamRegistry* registry_; |
| 58 | 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(StreamProtocolHandler); | 59 DISALLOW_COPY_AND_ASSIGN(StreamProtocolHandler); |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 class TestNavigationURLLoaderDelegate : public NavigationURLLoaderDelegate { | 62 class TestNavigationURLLoaderDelegate : public NavigationURLLoaderDelegate { |
| 63 public: | 63 public: |
| 64 TestNavigationURLLoaderDelegate() | 64 TestNavigationURLLoaderDelegate() |
| 65 : net_error_(0) { | 65 : net_error_(0), about_to_begin_navigation_counter_(0) {} |
| 66 } | |
| 67 | 66 |
| 68 const net::RedirectInfo& redirect_info() const { return redirect_info_; } | 67 const net::RedirectInfo& redirect_info() const { return redirect_info_; } |
| 69 ResourceResponse* redirect_response() const { | 68 ResourceResponse* redirect_response() const { |
| 70 return redirect_response_.get(); | 69 return redirect_response_.get(); |
| 71 } | 70 } |
| 72 ResourceResponse* response() const { return response_.get(); } | 71 ResourceResponse* response() const { return response_.get(); } |
| 73 StreamHandle* body() const { return body_.get(); } | 72 StreamHandle* body() const { return body_.get(); } |
| 74 int net_error() const { return net_error_; } | 73 int net_error() const { return net_error_; } |
| 74 int about_to_begin_navigation_counter() const { | |
| 75 return about_to_begin_navigation_counter_; | |
| 76 } | |
| 75 | 77 |
| 76 void WaitForRequestRedirected() { | 78 void WaitForRequestRedirected() { |
| 77 request_redirected_.reset(new base::RunLoop); | 79 request_redirected_.reset(new base::RunLoop); |
| 78 request_redirected_->Run(); | 80 request_redirected_->Run(); |
| 79 request_redirected_.reset(); | 81 request_redirected_.reset(); |
| 80 } | 82 } |
| 81 | 83 |
| 82 void WaitForResponseStarted() { | 84 void WaitForResponseStarted() { |
| 83 response_started_.reset(new base::RunLoop); | 85 response_started_.reset(new base::RunLoop); |
| 84 response_started_->Run(); | 86 response_started_->Run(); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 112 ASSERT_TRUE(response_started_); | 114 ASSERT_TRUE(response_started_); |
| 113 response_started_->Quit(); | 115 response_started_->Quit(); |
| 114 } | 116 } |
| 115 | 117 |
| 116 void OnRequestFailed(int net_error) override { | 118 void OnRequestFailed(int net_error) override { |
| 117 net_error_ = net_error; | 119 net_error_ = net_error; |
| 118 ASSERT_TRUE(request_failed_); | 120 ASSERT_TRUE(request_failed_); |
| 119 request_failed_->Quit(); | 121 request_failed_->Quit(); |
| 120 } | 122 } |
| 121 | 123 |
| 124 void NavigationRequested(base::TimeTicks timestamp) override { | |
| 125 ASSERT_FALSE(timestamp.is_null()); | |
| 126 ++about_to_begin_navigation_counter_; | |
| 127 } | |
| 128 | |
| 122 private: | 129 private: |
| 123 net::RedirectInfo redirect_info_; | 130 net::RedirectInfo redirect_info_; |
| 124 scoped_refptr<ResourceResponse> redirect_response_; | 131 scoped_refptr<ResourceResponse> redirect_response_; |
| 125 scoped_refptr<ResourceResponse> response_; | 132 scoped_refptr<ResourceResponse> response_; |
| 126 scoped_ptr<StreamHandle> body_; | 133 scoped_ptr<StreamHandle> body_; |
| 127 int net_error_; | 134 int net_error_; |
| 135 int about_to_begin_navigation_counter_; | |
| 128 | 136 |
| 129 scoped_ptr<base::RunLoop> request_redirected_; | 137 scoped_ptr<base::RunLoop> request_redirected_; |
| 130 scoped_ptr<base::RunLoop> response_started_; | 138 scoped_ptr<base::RunLoop> response_started_; |
| 131 scoped_ptr<base::RunLoop> request_failed_; | 139 scoped_ptr<base::RunLoop> request_failed_; |
| 132 }; | 140 }; |
| 133 | 141 |
| 134 class RequestBlockingResourceDispatcherHostDelegate | 142 class RequestBlockingResourceDispatcherHostDelegate |
| 135 : public ResourceDispatcherHostDelegate { | 143 : public ResourceDispatcherHostDelegate { |
| 136 public: | 144 public: |
| 137 // ResourceDispatcherHostDelegate implementation: | 145 // ResourceDispatcherHostDelegate implementation: |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 // Wait for the response to come back. | 223 // Wait for the response to come back. |
| 216 delegate.WaitForResponseStarted(); | 224 delegate.WaitForResponseStarted(); |
| 217 | 225 |
| 218 // Check the response is correct. | 226 // Check the response is correct. |
| 219 EXPECT_EQ("text/html", delegate.response()->head.mime_type); | 227 EXPECT_EQ("text/html", delegate.response()->head.mime_type); |
| 220 EXPECT_EQ(200, delegate.response()->head.headers->response_code()); | 228 EXPECT_EQ(200, delegate.response()->head.headers->response_code()); |
| 221 | 229 |
| 222 // Check the body is correct. | 230 // Check the body is correct. |
| 223 EXPECT_EQ(net::URLRequestTestJob::test_data_1(), | 231 EXPECT_EQ(net::URLRequestTestJob::test_data_1(), |
| 224 FetchURL(delegate.body()->GetURL())); | 232 FetchURL(delegate.body()->GetURL())); |
| 233 | |
| 234 EXPECT_EQ(1, delegate.about_to_begin_navigation_counter()); | |
| 225 } | 235 } |
| 226 | 236 |
| 227 // Tests that request failures are propagated correctly. | 237 // Tests that request failures are propagated correctly. |
| 228 TEST_F(NavigationURLLoaderTest, RequestFailed) { | 238 TEST_F(NavigationURLLoaderTest, RequestFailed) { |
| 229 TestNavigationURLLoaderDelegate delegate; | 239 TestNavigationURLLoaderDelegate delegate; |
| 230 scoped_ptr<NavigationURLLoader> loader = | 240 scoped_ptr<NavigationURLLoader> loader = |
| 231 MakeTestLoader(GURL("bogus:bogus"), &delegate); | 241 MakeTestLoader(GURL("bogus:bogus"), &delegate); |
| 232 | 242 |
| 233 // Wait for the request to fail as expected. | 243 // Wait for the request to fail as expected. |
| 234 delegate.WaitForRequestFailed(); | 244 delegate.WaitForRequestFailed(); |
| 235 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, delegate.net_error()); | 245 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, delegate.net_error()); |
| 246 EXPECT_EQ(1, delegate.about_to_begin_navigation_counter()); | |
| 236 } | 247 } |
| 237 | 248 |
| 238 // Test that redirects are sent to the delegate. | 249 // Test that redirects are sent to the delegate. |
| 239 TEST_F(NavigationURLLoaderTest, RequestRedirected) { | 250 TEST_F(NavigationURLLoaderTest, RequestRedirected) { |
| 240 // Fake a top-level request. Choose a URL which redirects so the request can | 251 // Fake a top-level request. Choose a URL which redirects so the request can |
| 241 // be paused before the response comes in. | 252 // be paused before the response comes in. |
| 242 TestNavigationURLLoaderDelegate delegate; | 253 TestNavigationURLLoaderDelegate delegate; |
| 243 scoped_ptr<NavigationURLLoader> loader = | 254 scoped_ptr<NavigationURLLoader> loader = |
| 244 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), | 255 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), |
| 245 &delegate); | 256 &delegate); |
| 246 | 257 |
| 247 // Wait for the request to redirect. | 258 // Wait for the request to redirect. |
| 248 delegate.WaitForRequestRedirected(); | 259 delegate.WaitForRequestRedirected(); |
| 249 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), | 260 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), |
| 250 delegate.redirect_info().new_url); | 261 delegate.redirect_info().new_url); |
| 251 EXPECT_EQ("GET", delegate.redirect_info().new_method); | 262 EXPECT_EQ("GET", delegate.redirect_info().new_method); |
| 252 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), | 263 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), |
| 253 delegate.redirect_info().new_first_party_for_cookies); | 264 delegate.redirect_info().new_first_party_for_cookies); |
| 254 EXPECT_EQ(302, delegate.redirect_response()->head.headers->response_code()); | 265 EXPECT_EQ(302, delegate.redirect_response()->head.headers->response_code()); |
| 266 EXPECT_EQ(1, delegate.about_to_begin_navigation_counter()); | |
| 255 | 267 |
| 256 // Wait for the response to complete. | 268 // Wait for the response to complete. |
| 257 loader->FollowRedirect(); | 269 loader->FollowRedirect(); |
| 258 delegate.WaitForResponseStarted(); | 270 delegate.WaitForResponseStarted(); |
| 259 | 271 |
| 260 // Check the response is correct. | 272 // Check the response is correct. |
| 261 EXPECT_EQ("text/html", delegate.response()->head.mime_type); | 273 EXPECT_EQ("text/html", delegate.response()->head.mime_type); |
| 262 EXPECT_EQ(200, delegate.response()->head.headers->response_code()); | 274 EXPECT_EQ(200, delegate.response()->head.headers->response_code()); |
| 263 | 275 |
| 264 // Release the body and check it is correct. | 276 // Release the body and check it is correct. |
| 265 EXPECT_TRUE(net::URLRequestTestJob::ProcessOnePendingMessage()); | 277 EXPECT_TRUE(net::URLRequestTestJob::ProcessOnePendingMessage()); |
| 266 EXPECT_EQ(net::URLRequestTestJob::test_data_2(), | 278 EXPECT_EQ(net::URLRequestTestJob::test_data_2(), |
| 267 FetchURL(delegate.body()->GetURL())); | 279 FetchURL(delegate.body()->GetURL())); |
| 280 | |
| 281 EXPECT_EQ(1, delegate.about_to_begin_navigation_counter()); | |
| 268 } | 282 } |
| 269 | 283 |
| 270 // Tests that the destroying the loader cancels the request. | 284 // Tests that the destroying the loader cancels the request. |
| 271 TEST_F(NavigationURLLoaderTest, CancelOnDestruct) { | 285 TEST_F(NavigationURLLoaderTest, CancelOnDestruct) { |
| 272 // Fake a top-level request. Choose a URL which redirects so the request can | 286 // Fake a top-level request. Choose a URL which redirects so the request can |
| 273 // be paused before the response comes in. | 287 // be paused before the response comes in. |
| 274 TestNavigationURLLoaderDelegate delegate; | 288 TestNavigationURLLoaderDelegate delegate; |
| 275 scoped_ptr<NavigationURLLoader> loader = | 289 scoped_ptr<NavigationURLLoader> loader = |
| 276 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), | 290 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), |
| 277 &delegate); | 291 &delegate); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 | 332 |
| 319 // Wait for the request to redirect. | 333 // Wait for the request to redirect. |
| 320 delegate.WaitForRequestRedirected(); | 334 delegate.WaitForRequestRedirected(); |
| 321 | 335 |
| 322 // Cancel all requests. | 336 // Cancel all requests. |
| 323 host_.CancelRequestsForContext(browser_context_->GetResourceContext()); | 337 host_.CancelRequestsForContext(browser_context_->GetResourceContext()); |
| 324 | 338 |
| 325 // Wait for the request to now be aborted. | 339 // Wait for the request to now be aborted. |
| 326 delegate.WaitForRequestFailed(); | 340 delegate.WaitForRequestFailed(); |
| 327 EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); | 341 EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); |
| 342 EXPECT_EQ(1, delegate.about_to_begin_navigation_counter()); | |
| 328 } | 343 } |
| 329 | 344 |
| 330 // Tests that, if the request is blocked by the ResourceDispatcherHostDelegate, | 345 // Tests that, if the request is blocked by the ResourceDispatcherHostDelegate, |
| 331 // the caller is informed appropriately. | 346 // the caller is informed appropriately. |
| 332 TEST_F(NavigationURLLoaderTest, RequestBlocked) { | 347 TEST_F(NavigationURLLoaderTest, RequestBlocked) { |
| 333 RequestBlockingResourceDispatcherHostDelegate rdh_delegate; | 348 RequestBlockingResourceDispatcherHostDelegate rdh_delegate; |
| 334 host_.SetDelegate(&rdh_delegate); | 349 host_.SetDelegate(&rdh_delegate); |
| 335 | 350 |
| 336 TestNavigationURLLoaderDelegate delegate; | 351 TestNavigationURLLoaderDelegate delegate; |
| 337 scoped_ptr<NavigationURLLoader> loader = | 352 scoped_ptr<NavigationURLLoader> loader = |
| 338 MakeTestLoader(net::URLRequestTestJob::test_url_1(), &delegate); | 353 MakeTestLoader(net::URLRequestTestJob::test_url_1(), &delegate); |
| 339 | 354 |
| 340 // Wait for the request to fail as expected. | 355 // Wait for the request to fail as expected. |
| 341 delegate.WaitForRequestFailed(); | 356 delegate.WaitForRequestFailed(); |
| 342 EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); | 357 EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); |
| 358 EXPECT_EQ(0, delegate.about_to_begin_navigation_counter()); | |
|
davidben
2015/01/28 21:33:27
This doesn't quite match the other path with OnReq
carlosk
2015/01/30 14:01:47
Done. This behavior changed once I moved the initi
| |
| 343 | 359 |
| 344 host_.SetDelegate(nullptr); | 360 host_.SetDelegate(nullptr); |
| 345 } | 361 } |
| 346 | 362 |
| 347 // Tests that ownership leaves the loader once the response is received. | 363 // Tests that ownership leaves the loader once the response is received. |
| 348 TEST_F(NavigationURLLoaderTest, LoaderDetached) { | 364 TEST_F(NavigationURLLoaderTest, LoaderDetached) { |
| 349 // Fake a top-level request to a URL whose body does not load immediately. | 365 // Fake a top-level request to a URL whose body does not load immediately. |
| 350 TestNavigationURLLoaderDelegate delegate; | 366 TestNavigationURLLoaderDelegate delegate; |
| 351 scoped_ptr<NavigationURLLoader> loader = | 367 scoped_ptr<NavigationURLLoader> loader = |
| 352 MakeTestLoader(net::URLRequestTestJob::test_url_2(), &delegate); | 368 MakeTestLoader(net::URLRequestTestJob::test_url_2(), &delegate); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 380 | 396 |
| 381 // Release the body. | 397 // Release the body. |
| 382 delegate.ReleaseBody(); | 398 delegate.ReleaseBody(); |
| 383 base::RunLoop().RunUntilIdle(); | 399 base::RunLoop().RunUntilIdle(); |
| 384 | 400 |
| 385 // Verify that URLRequestTestJob no longer has anything paused. | 401 // Verify that URLRequestTestJob no longer has anything paused. |
| 386 EXPECT_FALSE(net::URLRequestTestJob::ProcessOnePendingMessage()); | 402 EXPECT_FALSE(net::URLRequestTestJob::ProcessOnePendingMessage()); |
| 387 } | 403 } |
| 388 | 404 |
| 389 } // namespace content | 405 } // namespace content |
| OLD | NEW |