| 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), on_request_handled_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 on_request_handled_counter() const { return on_request_handled_counter_; } |
| 75 | 75 |
| 76 void WaitForRequestRedirected() { | 76 void WaitForRequestRedirected() { |
| 77 request_redirected_.reset(new base::RunLoop); | 77 request_redirected_.reset(new base::RunLoop); |
| 78 request_redirected_->Run(); | 78 request_redirected_->Run(); |
| 79 request_redirected_.reset(); | 79 request_redirected_.reset(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 void WaitForResponseStarted() { | 82 void WaitForResponseStarted() { |
| 83 response_started_.reset(new base::RunLoop); | 83 response_started_.reset(new base::RunLoop); |
| 84 response_started_->Run(); | 84 response_started_->Run(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 112 ASSERT_TRUE(response_started_); | 112 ASSERT_TRUE(response_started_); |
| 113 response_started_->Quit(); | 113 response_started_->Quit(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void OnRequestFailed(int net_error) override { | 116 void OnRequestFailed(int net_error) override { |
| 117 net_error_ = net_error; | 117 net_error_ = net_error; |
| 118 ASSERT_TRUE(request_failed_); | 118 ASSERT_TRUE(request_failed_); |
| 119 request_failed_->Quit(); | 119 request_failed_->Quit(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 void OnRequestStarted(base::TimeTicks timestamp) override { |
| 123 ASSERT_FALSE(timestamp.is_null()); |
| 124 ++on_request_handled_counter_; |
| 125 } |
| 126 |
| 122 private: | 127 private: |
| 123 net::RedirectInfo redirect_info_; | 128 net::RedirectInfo redirect_info_; |
| 124 scoped_refptr<ResourceResponse> redirect_response_; | 129 scoped_refptr<ResourceResponse> redirect_response_; |
| 125 scoped_refptr<ResourceResponse> response_; | 130 scoped_refptr<ResourceResponse> response_; |
| 126 scoped_ptr<StreamHandle> body_; | 131 scoped_ptr<StreamHandle> body_; |
| 127 int net_error_; | 132 int net_error_; |
| 133 int on_request_handled_counter_; |
| 128 | 134 |
| 129 scoped_ptr<base::RunLoop> request_redirected_; | 135 scoped_ptr<base::RunLoop> request_redirected_; |
| 130 scoped_ptr<base::RunLoop> response_started_; | 136 scoped_ptr<base::RunLoop> response_started_; |
| 131 scoped_ptr<base::RunLoop> request_failed_; | 137 scoped_ptr<base::RunLoop> request_failed_; |
| 132 }; | 138 }; |
| 133 | 139 |
| 134 class RequestBlockingResourceDispatcherHostDelegate | 140 class RequestBlockingResourceDispatcherHostDelegate |
| 135 : public ResourceDispatcherHostDelegate { | 141 : public ResourceDispatcherHostDelegate { |
| 136 public: | 142 public: |
| 137 // ResourceDispatcherHostDelegate implementation: | 143 // ResourceDispatcherHostDelegate implementation: |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 // Wait for the response to come back. | 221 // Wait for the response to come back. |
| 216 delegate.WaitForResponseStarted(); | 222 delegate.WaitForResponseStarted(); |
| 217 | 223 |
| 218 // Check the response is correct. | 224 // Check the response is correct. |
| 219 EXPECT_EQ("text/html", delegate.response()->head.mime_type); | 225 EXPECT_EQ("text/html", delegate.response()->head.mime_type); |
| 220 EXPECT_EQ(200, delegate.response()->head.headers->response_code()); | 226 EXPECT_EQ(200, delegate.response()->head.headers->response_code()); |
| 221 | 227 |
| 222 // Check the body is correct. | 228 // Check the body is correct. |
| 223 EXPECT_EQ(net::URLRequestTestJob::test_data_1(), | 229 EXPECT_EQ(net::URLRequestTestJob::test_data_1(), |
| 224 FetchURL(delegate.body()->GetURL())); | 230 FetchURL(delegate.body()->GetURL())); |
| 231 |
| 232 EXPECT_EQ(1, delegate.on_request_handled_counter()); |
| 225 } | 233 } |
| 226 | 234 |
| 227 // Tests that request failures are propagated correctly. | 235 // Tests that request failures are propagated correctly. |
| 228 TEST_F(NavigationURLLoaderTest, RequestFailed) { | 236 TEST_F(NavigationURLLoaderTest, RequestFailed) { |
| 229 TestNavigationURLLoaderDelegate delegate; | 237 TestNavigationURLLoaderDelegate delegate; |
| 230 scoped_ptr<NavigationURLLoader> loader = | 238 scoped_ptr<NavigationURLLoader> loader = |
| 231 MakeTestLoader(GURL("bogus:bogus"), &delegate); | 239 MakeTestLoader(GURL("bogus:bogus"), &delegate); |
| 232 | 240 |
| 233 // Wait for the request to fail as expected. | 241 // Wait for the request to fail as expected. |
| 234 delegate.WaitForRequestFailed(); | 242 delegate.WaitForRequestFailed(); |
| 235 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, delegate.net_error()); | 243 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, delegate.net_error()); |
| 244 EXPECT_EQ(1, delegate.on_request_handled_counter()); |
| 236 } | 245 } |
| 237 | 246 |
| 238 // Test that redirects are sent to the delegate. | 247 // Test that redirects are sent to the delegate. |
| 239 TEST_F(NavigationURLLoaderTest, RequestRedirected) { | 248 TEST_F(NavigationURLLoaderTest, RequestRedirected) { |
| 240 // Fake a top-level request. Choose a URL which redirects so the request can | 249 // Fake a top-level request. Choose a URL which redirects so the request can |
| 241 // be paused before the response comes in. | 250 // be paused before the response comes in. |
| 242 TestNavigationURLLoaderDelegate delegate; | 251 TestNavigationURLLoaderDelegate delegate; |
| 243 scoped_ptr<NavigationURLLoader> loader = | 252 scoped_ptr<NavigationURLLoader> loader = |
| 244 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), | 253 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), |
| 245 &delegate); | 254 &delegate); |
| 246 | 255 |
| 247 // Wait for the request to redirect. | 256 // Wait for the request to redirect. |
| 248 delegate.WaitForRequestRedirected(); | 257 delegate.WaitForRequestRedirected(); |
| 249 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), | 258 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), |
| 250 delegate.redirect_info().new_url); | 259 delegate.redirect_info().new_url); |
| 251 EXPECT_EQ("GET", delegate.redirect_info().new_method); | 260 EXPECT_EQ("GET", delegate.redirect_info().new_method); |
| 252 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), | 261 EXPECT_EQ(net::URLRequestTestJob::test_url_2(), |
| 253 delegate.redirect_info().new_first_party_for_cookies); | 262 delegate.redirect_info().new_first_party_for_cookies); |
| 254 EXPECT_EQ(302, delegate.redirect_response()->head.headers->response_code()); | 263 EXPECT_EQ(302, delegate.redirect_response()->head.headers->response_code()); |
| 264 EXPECT_EQ(1, delegate.on_request_handled_counter()); |
| 255 | 265 |
| 256 // Wait for the response to complete. | 266 // Wait for the response to complete. |
| 257 loader->FollowRedirect(); | 267 loader->FollowRedirect(); |
| 258 delegate.WaitForResponseStarted(); | 268 delegate.WaitForResponseStarted(); |
| 259 | 269 |
| 260 // Check the response is correct. | 270 // Check the response is correct. |
| 261 EXPECT_EQ("text/html", delegate.response()->head.mime_type); | 271 EXPECT_EQ("text/html", delegate.response()->head.mime_type); |
| 262 EXPECT_EQ(200, delegate.response()->head.headers->response_code()); | 272 EXPECT_EQ(200, delegate.response()->head.headers->response_code()); |
| 263 | 273 |
| 264 // Release the body and check it is correct. | 274 // Release the body and check it is correct. |
| 265 EXPECT_TRUE(net::URLRequestTestJob::ProcessOnePendingMessage()); | 275 EXPECT_TRUE(net::URLRequestTestJob::ProcessOnePendingMessage()); |
| 266 EXPECT_EQ(net::URLRequestTestJob::test_data_2(), | 276 EXPECT_EQ(net::URLRequestTestJob::test_data_2(), |
| 267 FetchURL(delegate.body()->GetURL())); | 277 FetchURL(delegate.body()->GetURL())); |
| 278 |
| 279 EXPECT_EQ(1, delegate.on_request_handled_counter()); |
| 268 } | 280 } |
| 269 | 281 |
| 270 // Tests that the destroying the loader cancels the request. | 282 // Tests that the destroying the loader cancels the request. |
| 271 TEST_F(NavigationURLLoaderTest, CancelOnDestruct) { | 283 TEST_F(NavigationURLLoaderTest, CancelOnDestruct) { |
| 272 // Fake a top-level request. Choose a URL which redirects so the request can | 284 // Fake a top-level request. Choose a URL which redirects so the request can |
| 273 // be paused before the response comes in. | 285 // be paused before the response comes in. |
| 274 TestNavigationURLLoaderDelegate delegate; | 286 TestNavigationURLLoaderDelegate delegate; |
| 275 scoped_ptr<NavigationURLLoader> loader = | 287 scoped_ptr<NavigationURLLoader> loader = |
| 276 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), | 288 MakeTestLoader(net::URLRequestTestJob::test_url_redirect_to_url_2(), |
| 277 &delegate); | 289 &delegate); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 | 330 |
| 319 // Wait for the request to redirect. | 331 // Wait for the request to redirect. |
| 320 delegate.WaitForRequestRedirected(); | 332 delegate.WaitForRequestRedirected(); |
| 321 | 333 |
| 322 // Cancel all requests. | 334 // Cancel all requests. |
| 323 host_.CancelRequestsForContext(browser_context_->GetResourceContext()); | 335 host_.CancelRequestsForContext(browser_context_->GetResourceContext()); |
| 324 | 336 |
| 325 // Wait for the request to now be aborted. | 337 // Wait for the request to now be aborted. |
| 326 delegate.WaitForRequestFailed(); | 338 delegate.WaitForRequestFailed(); |
| 327 EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); | 339 EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); |
| 340 EXPECT_EQ(1, delegate.on_request_handled_counter()); |
| 328 } | 341 } |
| 329 | 342 |
| 330 // Tests that, if the request is blocked by the ResourceDispatcherHostDelegate, | 343 // Tests that, if the request is blocked by the ResourceDispatcherHostDelegate, |
| 331 // the caller is informed appropriately. | 344 // the caller is informed appropriately. |
| 332 TEST_F(NavigationURLLoaderTest, RequestBlocked) { | 345 TEST_F(NavigationURLLoaderTest, RequestBlocked) { |
| 333 RequestBlockingResourceDispatcherHostDelegate rdh_delegate; | 346 RequestBlockingResourceDispatcherHostDelegate rdh_delegate; |
| 334 host_.SetDelegate(&rdh_delegate); | 347 host_.SetDelegate(&rdh_delegate); |
| 335 | 348 |
| 336 TestNavigationURLLoaderDelegate delegate; | 349 TestNavigationURLLoaderDelegate delegate; |
| 337 scoped_ptr<NavigationURLLoader> loader = | 350 scoped_ptr<NavigationURLLoader> loader = |
| 338 MakeTestLoader(net::URLRequestTestJob::test_url_1(), &delegate); | 351 MakeTestLoader(net::URLRequestTestJob::test_url_1(), &delegate); |
| 339 | 352 |
| 340 // Wait for the request to fail as expected. | 353 // Wait for the request to fail as expected. |
| 341 delegate.WaitForRequestFailed(); | 354 delegate.WaitForRequestFailed(); |
| 342 EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); | 355 EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); |
| 356 EXPECT_EQ(1, delegate.on_request_handled_counter()); |
| 343 | 357 |
| 344 host_.SetDelegate(nullptr); | 358 host_.SetDelegate(nullptr); |
| 345 } | 359 } |
| 346 | 360 |
| 347 // Tests that ownership leaves the loader once the response is received. | 361 // Tests that ownership leaves the loader once the response is received. |
| 348 TEST_F(NavigationURLLoaderTest, LoaderDetached) { | 362 TEST_F(NavigationURLLoaderTest, LoaderDetached) { |
| 349 // Fake a top-level request to a URL whose body does not load immediately. | 363 // Fake a top-level request to a URL whose body does not load immediately. |
| 350 TestNavigationURLLoaderDelegate delegate; | 364 TestNavigationURLLoaderDelegate delegate; |
| 351 scoped_ptr<NavigationURLLoader> loader = | 365 scoped_ptr<NavigationURLLoader> loader = |
| 352 MakeTestLoader(net::URLRequestTestJob::test_url_2(), &delegate); | 366 MakeTestLoader(net::URLRequestTestJob::test_url_2(), &delegate); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 380 | 394 |
| 381 // Release the body. | 395 // Release the body. |
| 382 delegate.ReleaseBody(); | 396 delegate.ReleaseBody(); |
| 383 base::RunLoop().RunUntilIdle(); | 397 base::RunLoop().RunUntilIdle(); |
| 384 | 398 |
| 385 // Verify that URLRequestTestJob no longer has anything paused. | 399 // Verify that URLRequestTestJob no longer has anything paused. |
| 386 EXPECT_FALSE(net::URLRequestTestJob::ProcessOnePendingMessage()); | 400 EXPECT_FALSE(net::URLRequestTestJob::ProcessOnePendingMessage()); |
| 387 } | 401 } |
| 388 | 402 |
| 389 } // namespace content | 403 } // namespace content |
| OLD | NEW |