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

Side by Side Diff: net/url_request/url_request_unittest.cc

Issue 154473002: Support redirectUrl at onHeadersReceived in WebRequest / DWR API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: +net test, fix nit Created 6 years, 10 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
« no previous file with comments | « net/url_request/url_request_test_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <shlobj.h> 9 #include <shlobj.h>
10 #endif 10 #endif
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); 331 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
332 auth_retval_ = auth_retval; 332 auth_retval_ = auth_retval;
333 } 333 }
334 void set_auth_credentials(const AuthCredentials& auth_credentials) { 334 void set_auth_credentials(const AuthCredentials& auth_credentials) {
335 auth_credentials_ = auth_credentials; 335 auth_credentials_ = auth_credentials;
336 } 336 }
337 337
338 void set_redirect_url(const GURL& url) { 338 void set_redirect_url(const GURL& url) {
339 redirect_url_ = url; 339 redirect_url_ = url;
340 } 340 }
341 void set_redirect_url_on_headers_received(const GURL& url) {
342 redirect_url_on_headers_received_ = url;
343 }
341 344
342 void set_block_on(int block_on) { 345 void set_block_on(int block_on) {
343 block_on_ = block_on; 346 block_on_ = block_on;
344 } 347 }
345 348
346 // Allows the user to check in which state did we block. 349 // Allows the user to check in which state did we block.
347 Stage stage_blocked_for_callback() const { 350 Stage stage_blocked_for_callback() const {
348 EXPECT_EQ(USER_CALLBACK, block_mode_); 351 EXPECT_EQ(USER_CALLBACK, block_mode_);
349 return stage_blocked_for_callback_; 352 return stage_blocked_for_callback_;
350 } 353 }
351 354
352 private: 355 private:
353 void RunCallback(int response, const CompletionCallback& callback); 356 void RunCallback(int response, const CompletionCallback& callback);
354 void RunAuthCallback(AuthRequiredResponse response, 357 void RunAuthCallback(AuthRequiredResponse response,
355 const AuthCallback& callback); 358 const AuthCallback& callback);
356 359
357 // TestNetworkDelegate implementation. 360 // TestNetworkDelegate implementation.
358 virtual int OnBeforeURLRequest(URLRequest* request, 361 virtual int OnBeforeURLRequest(URLRequest* request,
359 const CompletionCallback& callback, 362 const CompletionCallback& callback,
360 GURL* new_url) OVERRIDE; 363 GURL* new_url) OVERRIDE;
361 364
362 virtual int OnBeforeSendHeaders(URLRequest* request, 365 virtual int OnBeforeSendHeaders(URLRequest* request,
363 const CompletionCallback& callback, 366 const CompletionCallback& callback,
364 HttpRequestHeaders* headers) OVERRIDE; 367 HttpRequestHeaders* headers) OVERRIDE;
365 368
366 virtual int OnHeadersReceived( 369 virtual int OnHeadersReceived(
367 URLRequest* request, 370 URLRequest* request,
368 const CompletionCallback& callback, 371 const CompletionCallback& callback,
372 GURL* new_url,
369 const HttpResponseHeaders* original_response_headers, 373 const HttpResponseHeaders* original_response_headers,
370 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE; 374 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE;
371 375
372 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( 376 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
373 URLRequest* request, 377 URLRequest* request,
374 const AuthChallengeInfo& auth_info, 378 const AuthChallengeInfo& auth_info,
375 const AuthCallback& callback, 379 const AuthCallback& callback,
376 AuthCredentials* credentials) OVERRIDE; 380 AuthCredentials* credentials) OVERRIDE;
377 381
378 // Resets the callbacks and |stage_blocked_for_callback_|. 382 // Resets the callbacks and |stage_blocked_for_callback_|.
379 void Reset(); 383 void Reset();
380 384
381 // Checks whether we should block in |stage|. If yes, returns an error code 385 // Checks whether we should block in |stage|. If yes, returns an error code
382 // and optionally sets up callback based on |block_mode_|. If no, returns OK. 386 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
383 int MaybeBlockStage(Stage stage, const CompletionCallback& callback); 387 int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
384 388
385 // Configuration parameters, can be adjusted by public methods: 389 // Configuration parameters, can be adjusted by public methods:
386 const BlockMode block_mode_; 390 const BlockMode block_mode_;
387 391
388 // Values returned on blocking stages when mode is SYNCHRONOUS or 392 // Values returned on blocking stages when mode is SYNCHRONOUS or
389 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. 393 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
390 int retval_; // To be returned in non-auth stages. 394 int retval_; // To be returned in non-auth stages.
391 AuthRequiredResponse auth_retval_; 395 AuthRequiredResponse auth_retval_;
392 396
393 GURL redirect_url_; // Used if non-empty. 397 GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest.
398 GURL redirect_url_on_headers_received_; // Used if non-empty.
394 int block_on_; // Bit mask: in which stages to block. 399 int block_on_; // Bit mask: in which stages to block.
395 400
396 // |auth_credentials_| will be copied to |*target_auth_credential_| on 401 // |auth_credentials_| will be copied to |*target_auth_credential_| on
397 // callback. 402 // callback.
398 AuthCredentials auth_credentials_; 403 AuthCredentials auth_credentials_;
399 AuthCredentials* target_auth_credentials_; 404 AuthCredentials* target_auth_credentials_;
400 405
401 // Internal variables, not set by not the user: 406 // Internal variables, not set by not the user:
402 // Last blocked stage waiting for user callback (unused if |block_mode_| != 407 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
403 // USER_CALLBACK). 408 // USER_CALLBACK).
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 const CompletionCallback& callback, 479 const CompletionCallback& callback,
475 HttpRequestHeaders* headers) { 480 HttpRequestHeaders* headers) {
476 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); 481 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
477 482
478 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); 483 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
479 } 484 }
480 485
481 int BlockingNetworkDelegate::OnHeadersReceived( 486 int BlockingNetworkDelegate::OnHeadersReceived(
482 URLRequest* request, 487 URLRequest* request,
483 const CompletionCallback& callback, 488 const CompletionCallback& callback,
489 GURL* new_url,
484 const HttpResponseHeaders* original_response_headers, 490 const HttpResponseHeaders* original_response_headers,
485 scoped_refptr<HttpResponseHeaders>* override_response_headers) { 491 scoped_refptr<HttpResponseHeaders>* override_response_headers) {
486 TestNetworkDelegate::OnHeadersReceived( 492 TestNetworkDelegate::OnHeadersReceived(
487 request, callback, original_response_headers, 493 request, callback, new_url, original_response_headers,
488 override_response_headers); 494 override_response_headers);
489 495
496 // Redirects to the same URL are allowed, but for simplicity, assume that
497 // the tests only redirect to a different URL.
498 if (!redirect_url_on_headers_received_.is_empty() &&
499 redirect_url_on_headers_received_ != request->url())
500 *new_url = redirect_url_on_headers_received_;
501
490 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); 502 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
491 } 503 }
492 504
493 NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( 505 NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
494 URLRequest* request, 506 URLRequest* request,
495 const AuthChallengeInfo& auth_info, 507 const AuthChallengeInfo& auth_info,
496 const AuthCallback& callback, 508 const AuthCallback& callback,
497 AuthCredentials* credentials) { 509 AuthCredentials* credentials) {
498 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, 510 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
499 credentials); 511 credentials);
(...skipping 1908 matching lines...) Expand 10 before | Expand all | Expand 10 after
2408 class FixedDateNetworkDelegate : public TestNetworkDelegate { 2420 class FixedDateNetworkDelegate : public TestNetworkDelegate {
2409 public: 2421 public:
2410 explicit FixedDateNetworkDelegate(const std::string& fixed_date) 2422 explicit FixedDateNetworkDelegate(const std::string& fixed_date)
2411 : fixed_date_(fixed_date) {} 2423 : fixed_date_(fixed_date) {}
2412 virtual ~FixedDateNetworkDelegate() {} 2424 virtual ~FixedDateNetworkDelegate() {}
2413 2425
2414 // net::NetworkDelegate implementation 2426 // net::NetworkDelegate implementation
2415 virtual int OnHeadersReceived( 2427 virtual int OnHeadersReceived(
2416 net::URLRequest* request, 2428 net::URLRequest* request,
2417 const net::CompletionCallback& callback, 2429 const net::CompletionCallback& callback,
2430 GURL* new_url,
2418 const net::HttpResponseHeaders* original_response_headers, 2431 const net::HttpResponseHeaders* original_response_headers,
2419 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) 2432 scoped_refptr<net::HttpResponseHeaders>* override_response_headers)
2420 OVERRIDE; 2433 OVERRIDE;
2421 2434
2422 private: 2435 private:
2423 std::string fixed_date_; 2436 std::string fixed_date_;
2424 2437
2425 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); 2438 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate);
2426 }; 2439 };
2427 2440
2428 int FixedDateNetworkDelegate::OnHeadersReceived( 2441 int FixedDateNetworkDelegate::OnHeadersReceived(
2429 net::URLRequest* request, 2442 net::URLRequest* request,
2430 const net::CompletionCallback& callback, 2443 const net::CompletionCallback& callback,
2444 GURL* new_url,
2431 const net::HttpResponseHeaders* original_response_headers, 2445 const net::HttpResponseHeaders* original_response_headers,
2432 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) { 2446 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
2433 net::HttpResponseHeaders* new_response_headers = 2447 net::HttpResponseHeaders* new_response_headers =
2434 new net::HttpResponseHeaders(original_response_headers->raw_headers()); 2448 new net::HttpResponseHeaders(original_response_headers->raw_headers());
2435 2449
2436 new_response_headers->RemoveHeader("Date"); 2450 new_response_headers->RemoveHeader("Date");
2437 new_response_headers->AddHeader("Date: " + fixed_date_); 2451 new_response_headers->AddHeader("Date: " + fixed_date_);
2438 2452
2439 *override_response_headers = new_response_headers; 2453 *override_response_headers = new_response_headers;
2440 return TestNetworkDelegate::OnHeadersReceived(request, 2454 return TestNetworkDelegate::OnHeadersReceived(request,
2441 callback, 2455 callback,
2456 new_url,
2442 original_response_headers, 2457 original_response_headers,
2443 override_response_headers); 2458 override_response_headers);
2444 } 2459 }
2445 2460
2446 // Test that cookie expiration times are adjusted for server/client clock 2461 // Test that cookie expiration times are adjusted for server/client clock
2447 // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date 2462 // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2448 // headers by defaulting to GMT. (crbug.com/135131) 2463 // headers by defaulting to GMT. (crbug.com/135131)
2449 TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { 2464 TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) {
2450 LocalHttpTestServer test_server; 2465 LocalHttpTestServer test_server;
2451 ASSERT_TRUE(test_server.Start()); 2466 ASSERT_TRUE(test_server.Start());
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after
3000 EXPECT_EQ(original_url, r.original_url()); 3015 EXPECT_EQ(original_url, r.original_url());
3001 EXPECT_EQ(2U, r.url_chain().size()); 3016 EXPECT_EQ(2U, r.url_chain().size());
3002 EXPECT_EQ(1, network_delegate.created_requests()); 3017 EXPECT_EQ(1, network_delegate.created_requests());
3003 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3018 EXPECT_EQ(0, network_delegate.destroyed_requests());
3004 EXPECT_EQ("POST", r.method()); 3019 EXPECT_EQ("POST", r.method());
3005 EXPECT_EQ(kData, d.data_received()); 3020 EXPECT_EQ(kData, d.data_received());
3006 } 3021 }
3007 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3022 EXPECT_EQ(1, network_delegate.destroyed_requests());
3008 } 3023 }
3009 3024
3025 // Tests that the network delegate can block and redirect a request to a new
3026 // URL during OnHeadersReceived.
3027 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) {
3028 ASSERT_TRUE(test_server_.Start());
3029
3030 TestDelegate d;
3031 BlockingNetworkDelegate network_delegate(
3032 BlockingNetworkDelegate::AUTO_CALLBACK);
3033 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3034 GURL redirect_url(test_server_.GetURL("simple.html"));
3035 network_delegate.set_redirect_url_on_headers_received(redirect_url);
3036
3037 TestURLRequestContextWithProxy context(
3038 test_server_.host_port_pair().ToString(), &network_delegate);
3039
3040 {
3041 GURL original_url(test_server_.GetURL("empty.html"));
3042 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
3043
3044 r.Start();
3045 base::RunLoop().Run();
3046
3047 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3048 EXPECT_EQ(0, r.status().error());
3049 EXPECT_EQ(redirect_url, r.url());
3050 EXPECT_EQ(original_url, r.original_url());
3051 EXPECT_EQ(2U, r.url_chain().size());
3052 EXPECT_EQ(2, network_delegate.created_requests());
3053 EXPECT_EQ(0, network_delegate.destroyed_requests());
3054 }
3055 EXPECT_EQ(1, network_delegate.destroyed_requests());
battre 2014/02/13 15:30:33 This looks suspicious. Shouldn't there be a) only
3056 }
3057
3058 // Tests that redirects caused by the network delegate during OnHeadersReceived
3059 // preserve POST data.
3060 TEST_F(URLRequestTestHTTP,
3061 NetworkDelegateRedirectRequestOnHeadersReceivedPost) {
3062 ASSERT_TRUE(test_server_.Start());
3063
3064 const char kData[] = "hello world";
3065
3066 TestDelegate d;
3067 BlockingNetworkDelegate network_delegate(
3068 BlockingNetworkDelegate::AUTO_CALLBACK);
3069 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3070 GURL redirect_url(test_server_.GetURL("echo"));
3071 network_delegate.set_redirect_url_on_headers_received(redirect_url);
3072
3073 TestURLRequestContext context(true);
3074 context.set_network_delegate(&network_delegate);
3075 context.Init();
3076
3077 {
3078 GURL original_url(test_server_.GetURL("empty.html"));
3079 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
3080 r.set_method("POST");
3081 r.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
3082 HttpRequestHeaders headers;
3083 headers.SetHeader(HttpRequestHeaders::kContentLength,
3084 base::UintToString(arraysize(kData) - 1));
3085 r.SetExtraRequestHeaders(headers);
3086 r.Start();
3087 base::RunLoop().Run();
3088
3089 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3090 EXPECT_EQ(0, r.status().error());
3091 EXPECT_EQ(redirect_url, r.url());
3092 EXPECT_EQ(original_url, r.original_url());
3093 EXPECT_EQ(2U, r.url_chain().size());
3094 EXPECT_EQ(2, network_delegate.created_requests());
3095 EXPECT_EQ(0, network_delegate.destroyed_requests());
3096 EXPECT_EQ("POST", r.method());
3097 EXPECT_EQ(kData, d.data_received());
3098 }
3099 EXPECT_EQ(1, network_delegate.destroyed_requests());
3100 }
3101
3010 // Tests that the network delegate can synchronously complete OnAuthRequired 3102 // Tests that the network delegate can synchronously complete OnAuthRequired
3011 // by taking no action. This indicates that the NetworkDelegate does not want to 3103 // by taking no action. This indicates that the NetworkDelegate does not want to
3012 // handle the challenge, and is passing the buck along to the 3104 // handle the challenge, and is passing the buck along to the
3013 // URLRequest::Delegate. 3105 // URLRequest::Delegate.
3014 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { 3106 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
3015 ASSERT_TRUE(test_server_.Start()); 3107 ASSERT_TRUE(test_server_.Start());
3016 3108
3017 TestDelegate d; 3109 TestDelegate d;
3018 BlockingNetworkDelegate network_delegate( 3110 BlockingNetworkDelegate network_delegate(
3019 BlockingNetworkDelegate::SYNCHRONOUS); 3111 BlockingNetworkDelegate::SYNCHRONOUS);
(...skipping 891 matching lines...) Expand 10 before | Expand all | Expand 10 after
3911 virtual int OnBeforeSendHeaders(URLRequest* request, 4003 virtual int OnBeforeSendHeaders(URLRequest* request,
3912 const CompletionCallback& callback, 4004 const CompletionCallback& callback,
3913 HttpRequestHeaders* headers) OVERRIDE { 4005 HttpRequestHeaders* headers) OVERRIDE {
3914 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); 4006 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
3915 return RunCallbackAsynchronously(request, callback); 4007 return RunCallbackAsynchronously(request, callback);
3916 } 4008 }
3917 4009
3918 virtual int OnHeadersReceived( 4010 virtual int OnHeadersReceived(
3919 URLRequest* request, 4011 URLRequest* request,
3920 const CompletionCallback& callback, 4012 const CompletionCallback& callback,
4013 GURL* new_url,
3921 const HttpResponseHeaders* original_response_headers, 4014 const HttpResponseHeaders* original_response_headers,
3922 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE { 4015 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE {
3923 TestNetworkDelegate::OnHeadersReceived(request, callback, 4016 TestNetworkDelegate::OnHeadersReceived(request, callback, new_url,
3924 original_response_headers, 4017 original_response_headers,
3925 override_response_headers); 4018 override_response_headers);
3926 return RunCallbackAsynchronously(request, callback); 4019 return RunCallbackAsynchronously(request, callback);
3927 } 4020 }
3928 4021
3929 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( 4022 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
3930 URLRequest* request, 4023 URLRequest* request,
3931 const AuthChallengeInfo& auth_info, 4024 const AuthChallengeInfo& auth_info,
3932 const AuthCallback& callback, 4025 const AuthCallback& callback,
3933 AuthCredentials* credentials) OVERRIDE { 4026 AuthCredentials* credentials) OVERRIDE {
(...skipping 3659 matching lines...) Expand 10 before | Expand all | Expand 10 after
7593 7686
7594 EXPECT_FALSE(r.is_pending()); 7687 EXPECT_FALSE(r.is_pending());
7595 EXPECT_EQ(1, d->response_started_count()); 7688 EXPECT_EQ(1, d->response_started_count());
7596 EXPECT_FALSE(d->received_data_before_response()); 7689 EXPECT_FALSE(d->received_data_before_response());
7597 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 7690 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
7598 } 7691 }
7599 } 7692 }
7600 #endif // !defined(DISABLE_FTP_SUPPORT) 7693 #endif // !defined(DISABLE_FTP_SUPPORT)
7601 7694
7602 } // namespace net 7695 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request_test_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698