diff options
author | ddorwin@chromium.org <ddorwin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-29 22:24:18 +0000 |
---|---|---|
committer | ddorwin@chromium.org <ddorwin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-29 22:24:18 +0000 |
commit | 83e875da32de6614774f976b49bc2fd42997f433 (patch) | |
tree | ae8dbb8ab3541837fb480d62844d8b4c4f6b25f8 /net | |
parent | 94fb7aec260bbb23ec2c63561289c933c8ea7eb2 (diff) | |
download | chromium_src-83e875da32de6614774f976b49bc2fd42997f433.zip chromium_src-83e875da32de6614774f976b49bc2fd42997f433.tar.gz chromium_src-83e875da32de6614774f976b49bc2fd42997f433.tar.bz2 |
Revert 103360 - NetworkDelegate::OnAuthRequired can set authentication or cancel, in addition to observing.
TBR=ananta@chromium.org
BUG=32056
TEST=net_unittests
Review URL: http://codereview.chromium.org/8037038
TBR=cbentzel@chromium.org
Review URL: http://codereview.chromium.org/8082010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@103372 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/auth.cc | 6 | ||||
-rw-r--r-- | net/base/auth.h | 23 | ||||
-rw-r--r-- | net/base/network_delegate.cc | 9 | ||||
-rw-r--r-- | net/base/network_delegate.h | 47 | ||||
-rw-r--r-- | net/proxy/network_delegate_error_observer_unittest.cc | 13 | ||||
-rw-r--r-- | net/url_request/url_request.cc | 71 | ||||
-rw-r--r-- | net/url_request/url_request.h | 10 | ||||
-rw-r--r-- | net/url_request/url_request_ftp_job.cc | 18 | ||||
-rw-r--r-- | net/url_request/url_request_test_util.cc | 42 | ||||
-rw-r--r-- | net/url_request/url_request_test_util.h | 33 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.cc | 268 |
11 files changed, 74 insertions, 466 deletions
diff --git a/net/base/auth.cc b/net/base/auth.cc index 104400f..f60c8ed 100644 --- a/net/base/auth.cc +++ b/net/base/auth.cc @@ -25,10 +25,4 @@ AuthData::AuthData() : state(AUTH_STATE_NEED_AUTH) { AuthData::~AuthData() { } -AuthCredentials::AuthCredentials() { -} - -AuthCredentials::~AuthCredentials() { -} - } // namespace net diff --git a/net/base/auth.h b/net/base/auth.h index 386adad..51f8db9 100644 --- a/net/base/auth.h +++ b/net/base/auth.h @@ -43,26 +43,6 @@ class NET_EXPORT AuthChallengeInfo : ~AuthChallengeInfo(); }; -// Authentication Credentials for an authentication credentials. -class NET_EXPORT AuthCredentials { - public: - AuthCredentials(); - ~AuthCredentials(); - - // The username to provide, possibly empty. This should be ASCII only to - // minimize compatibility problems, but arbitrary UTF-16 strings are allowed - // and will be attempted. - string16 username; - - // The password to provide, possibly empty. This should be ASCII only to - // minimize compatibility problems, but arbitrary UTF-16 strings are allowed - // and will be attempted. - string16 password; - - // Intentionally allowing the implicit copy constructor and assignment - // operators. -}; - // Authentication structures enum AuthState { AUTH_STATE_DONT_NEED_AUTH, @@ -74,7 +54,8 @@ enum AuthState { class AuthData : public base::RefCountedThreadSafe<AuthData> { public: AuthState state; // whether we need, have, or gave up on authentication. - AuthCredentials credentials; // The credentials to use for auth. + string16 username; // the username supplied to us for auth. + string16 password; // the password supplied to us for auth. // We wouldn't instantiate this class if we didn't need authentication. AuthData(); diff --git a/net/base/network_delegate.cc b/net/base/network_delegate.cc index 1aa1ad4..28d0b31 100644 --- a/net/base/network_delegate.cc +++ b/net/base/network_delegate.cc @@ -69,13 +69,10 @@ void NetworkDelegate::NotifyPACScriptError(int line_number, OnPACScriptError(line_number, error); } -NetworkDelegate::AuthRequiredResponse NetworkDelegate::NotifyAuthRequired( - URLRequest* request, - const AuthChallengeInfo& auth_info, - const AuthCallback& callback, - AuthCredentials* credentials) { +void NetworkDelegate::NotifyAuthRequired(URLRequest* request, + const AuthChallengeInfo& auth_info) { DCHECK(CalledOnValidThread()); - return OnAuthRequired(request, auth_info, callback, credentials); + OnAuthRequired(request, auth_info); } } // namespace net diff --git a/net/base/network_delegate.h b/net/base/network_delegate.h index 2112d3e..2506788 100644 --- a/net/base/network_delegate.h +++ b/net/base/network_delegate.h @@ -6,10 +6,8 @@ #define NET_BASE_NETWORK_DELEGATE_H_ #pragma once -#include "base/callback.h" #include "base/string16.h" #include "base/threading/non_thread_safe.h" -#include "net/base/auth.h" #include "net/base/completion_callback.h" class GURL; @@ -26,6 +24,7 @@ namespace net { // NOTE: It is not okay to add any compile-time dependencies on symbols outside // of net/base here, because we have a net_base library. Forward declarations // are ok. +class AuthChallengeInfo; class HostPortPair; class HttpRequestHeaders; class URLRequest; @@ -33,17 +32,6 @@ class URLRequestJob; class NetworkDelegate : public base::NonThreadSafe { public: - // AuthRequiredResponse indicates how a NetworkDelegate handles an - // OnAuthRequired call. It's placed in this file to prevent url_request.h - // from having to include network_delegate.h. - enum AuthRequiredResponse { - AUTH_REQUIRED_RESPONSE_NO_ACTION, - AUTH_REQUIRED_RESPONSE_SET_AUTH, - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH, - AUTH_REQUIRED_RESPONSE_IO_PENDING, - }; - typedef base::Callback<void(AuthRequiredResponse)> AuthCallback; - virtual ~NetworkDelegate() {} // Notification interface called by the network stack. Note that these @@ -65,10 +53,8 @@ class NetworkDelegate : public base::NonThreadSafe { void NotifyCompleted(URLRequest* request); void NotifyURLRequestDestroyed(URLRequest* request); void NotifyPACScriptError(int line_number, const string16& error); - AuthRequiredResponse NotifyAuthRequired(URLRequest* request, - const AuthChallengeInfo& auth_info, - const AuthCallback& callback, - AuthCredentials* credentials); + void NotifyAuthRequired(URLRequest* request, + const AuthChallengeInfo& auth_info); private: // This is the interface for subclasses of NetworkDelegate to implement. This @@ -86,7 +72,7 @@ class NetworkDelegate : public base::NonThreadSafe { // Called right before the HTTP headers are sent. Allows the delegate to // read/write |headers| before they get sent out. |callback| and |headers| are - // valid only until OnURLRequestDestroyed is called for this request. + // valid only until OnHttpTransactionDestroyed is called for this request. // Returns a net status code. virtual int OnBeforeSendHeaders(URLRequest* request, CompletionCallback* callback, @@ -117,28 +103,9 @@ class NetworkDelegate : public base::NonThreadSafe { // Corresponds to ProxyResolverJSBindings::OnError. virtual void OnPACScriptError(int line_number, const string16& error) = 0; - // Called when a request receives an authentication challenge - // specified by |auth_info|, and is unable to respond using cached - // credentials. |callback| and |credentials| must be non-NULL, and must - // be valid until OnURLRequestDestroyed is called for |request|. - // - // The following return values are allowed: - // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but - // no action is being taken on it. - // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with - // a username and password, which should be used in a response to - // |auth_info|. - // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge - // should not be attempted. - // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided - // asynchronously. |callback| will be invoked when the decision is made, - // and one of the other AuthRequiredResponse values will be passed in with - // the same semantics as described above. - virtual AuthRequiredResponse OnAuthRequired( - URLRequest* request, - const AuthChallengeInfo& auth_info, - const AuthCallback& callback, - AuthCredentials* credentials) = 0; + // Corresponds to URLRequest::Delegate::OnAuthRequired. + virtual void OnAuthRequired(URLRequest* reqest, + const AuthChallengeInfo& auth_info) = 0; }; } // namespace net diff --git a/net/proxy/network_delegate_error_observer_unittest.cc b/net/proxy/network_delegate_error_observer_unittest.cc index 5481e0f..551c006 100644 --- a/net/proxy/network_delegate_error_observer_unittest.cc +++ b/net/proxy/network_delegate_error_observer_unittest.cc @@ -28,12 +28,12 @@ class TestNetworkDelegate : public net::NetworkDelegate { virtual int OnBeforeURLRequest(URLRequest* request, CompletionCallback* callback, GURL* new_url) OVERRIDE { - return OK; + return net::OK; } virtual int OnBeforeSendHeaders(URLRequest* request, CompletionCallback* callback, HttpRequestHeaders* headers) OVERRIDE { - return OK; + return net::OK; } virtual void OnSendHeaders(URLRequest* request, const HttpRequestHeaders& headers) OVERRIDE {} @@ -49,13 +49,8 @@ class TestNetworkDelegate : public net::NetworkDelegate { const string16& error) OVERRIDE { got_pac_error_ = true; } - virtual AuthRequiredResponse OnAuthRequired( - URLRequest* request, - const AuthChallengeInfo& auth_info, - const AuthCallback& callback, - AuthCredentials* credentials) OVERRIDE { - return AUTH_REQUIRED_RESPONSE_NO_ACTION; - } + virtual void OnAuthRequired(URLRequest* request, + const AuthChallengeInfo& auth_info) OVERRIDE {} bool got_pac_error_; }; diff --git a/net/url_request/url_request.cc b/net/url_request/url_request.cc index acc085e..41d2362 100644 --- a/net/url_request/url_request.cc +++ b/net/url_request/url_request.cc @@ -4,14 +4,11 @@ #include "net/url_request/url_request.h" -#include "base/bind.h" -#include "base/callback.h" #include "base/compiler_specific.h" #include "base/memory/singleton.h" #include "base/message_loop.h" #include "base/metrics/stats_counters.h" #include "base/synchronization/lock.h" -#include "net/base/auth.h" #include "net/base/host_port_pair.h" #include "net/base/load_flags.h" #include "net/base/net_errors.h" @@ -433,7 +430,8 @@ void URLRequest::BeforeRequestComplete(int error) { DCHECK(!job_); DCHECK_NE(ERR_IO_PENDING, error); - SetUnblockedOnDelegate(); + if (blocked_on_delegate_) + SetUnblockedOnDelegate(); if (error != OK) { net_log_.AddEvent(NetLog::TYPE_CANCELLED, make_scoped_refptr(new NetLogStringParameter("source", "delegate"))); @@ -768,57 +766,20 @@ void URLRequest::SetUserData(const void* key, UserData* data) { } void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) { - NetworkDelegate::AuthRequiredResponse rv = - NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; - auth_info_ = auth_info; - if (context_ && context_->network_delegate()) { - rv = context_->network_delegate()->NotifyAuthRequired( - this, - *auth_info, - base::Bind(&URLRequest::NotifyAuthRequiredComplete, - base::Unretained(this)), - &auth_credentials_); - } - - if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING) { - SetBlockedOnDelegate(); - } else { - NotifyAuthRequiredComplete(rv); - } -} + // TODO(battre): We could simulate a redirection there as follows: + // if (context_ && context_->network_delegate()) { + // // We simulate a redirection. + // context_->network_delegate()->NotifyBeforeRedirect(this, url()); + //} + // This fixes URLRequestTestHTTP.BasicAuth but not + // URLRequestTestHTTP.BasicAuthWithCookies. In both cases we observe a + // call sequence of OnBeforeSendHeaders -> OnSendHeaders -> + // OnBeforeSendHeaders. + if (context_ && context_->network_delegate()) + context_->network_delegate()->NotifyAuthRequired(this, *auth_info); -void URLRequest::NotifyAuthRequiredComplete( - NetworkDelegate::AuthRequiredResponse result) { - SetUnblockedOnDelegate(); - - // NotifyAuthRequired may be called multiple times, such as - // when an authentication attempt fails. Clear out the data - // so it can be reset on another round. - AuthCredentials credentials = auth_credentials_; - auth_credentials_ = AuthCredentials(); - scoped_refptr<AuthChallengeInfo> auth_info; - auth_info.swap(auth_info_); - - switch (result) { - case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION: - // Defer to the URLRequest::Delegate, since the NetworkDelegate - // didn't take an action. - if (delegate_) - delegate_->OnAuthRequired(this, auth_info.get()); - break; - - case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH: - SetAuth(credentials.username, credentials.password); - break; - - case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: - CancelAuth(); - break; - - case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING: - NOTREACHED(); - break; - } + if (delegate_) + delegate_->OnAuthRequired(this, auth_info); } void URLRequest::NotifyCertificateRequested( @@ -876,8 +837,6 @@ void URLRequest::SetBlockedOnDelegate() { } void URLRequest::SetUnblockedOnDelegate() { - if (!blocked_on_delegate_) - return; blocked_on_delegate_ = false; load_state_param_.clear(); net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_BLOCKED_ON_DELEGATE, NULL); diff --git a/net/url_request/url_request.h b/net/url_request/url_request.h index 6c4e664..7f5f961 100644 --- a/net/url_request/url_request.h +++ b/net/url_request/url_request.h @@ -17,12 +17,10 @@ #include "base/string16.h" #include "base/threading/non_thread_safe.h" #include "googleurl/src/gurl.h" -#include "net/base/auth.h" #include "net/base/completion_callback.h" #include "net/base/load_states.h" #include "net/base/net_export.h" #include "net/base/net_log.h" -#include "net/base/network_delegate.h" #include "net/base/request_priority.h" #include "net/http/http_request_headers.h" #include "net/http/http_response_info.h" @@ -718,7 +716,6 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe) { // |delegate_| is not NULL. See URLRequest::Delegate for the meaning // of these functions. void NotifyAuthRequired(AuthChallengeInfo* auth_info); - void NotifyAuthRequiredComplete(NetworkDelegate::AuthRequiredResponse result); void NotifyCertificateRequested(SSLCertRequestInfo* cert_request_info); void NotifySSLCertificateError(const SSLInfo& ssl_info, bool is_hsts_host); @@ -811,13 +808,6 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe) { // TODO(battre): Remove this. http://crbug.com/89049 bool has_notified_completion_; - // Authentication data used by the NetworkDelegate for this request, - // if one is present. |auth_credentials_| may be filled in when calling - // |NotifyAuthRequired| on the NetworkDelegate. |auth_info_| holds - // the authentication challenge being handled by |NotifyAuthRequired|. - AuthCredentials auth_credentials_; - scoped_refptr<AuthChallengeInfo> auth_info_; - DISALLOW_COPY_AND_ASSIGN(URLRequest); }; diff --git a/net/url_request/url_request_ftp_job.cc b/net/url_request/url_request_ftp_job.cc index b14d88d..1f04548 100644 --- a/net/url_request/url_request_ftp_job.cc +++ b/net/url_request/url_request_ftp_job.cc @@ -106,10 +106,9 @@ void URLRequestFtpJob::OnStartCompleted(int result) { } else if (transaction_->GetResponseInfo()->needs_auth) { GURL origin = request_->url().GetOrigin(); if (server_auth_ && server_auth_->state == AUTH_STATE_HAVE_AUTH) { - request_->context()->ftp_auth_cache()->Remove( - origin, - server_auth_->credentials.username, - server_auth_->credentials.password); + request_->context()->ftp_auth_cache()->Remove(origin, + server_auth_->username, + server_auth_->password); } else if (!server_auth_) { server_auth_ = new AuthData(); } @@ -120,8 +119,7 @@ void URLRequestFtpJob::OnStartCompleted(int result) { if (cached_auth) { // Retry using cached auth data. - SetAuth(cached_auth->username, - cached_auth->password); + SetAuth(cached_auth->username, cached_auth->password); } else { // Prompt for a username/password. NotifyHeadersComplete(); @@ -151,8 +149,8 @@ void URLRequestFtpJob::RestartTransactionWithAuth() { // be notifying our consumer asynchronously via OnStartCompleted. SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); - int rv = transaction_->RestartWithAuth(server_auth_->credentials.username, - server_auth_->credentials.password, + int rv = transaction_->RestartWithAuth(server_auth_->username, + server_auth_->password, &start_callback_); if (rv == ERR_IO_PENDING) return; @@ -207,8 +205,8 @@ void URLRequestFtpJob::SetAuth(const string16& username, const string16& password) { DCHECK(NeedsAuth()); server_auth_->state = AUTH_STATE_HAVE_AUTH; - server_auth_->credentials.username = username; - server_auth_->credentials.password = password; + server_auth_->username = username; + server_auth_->password = password; request_->context()->ftp_auth_cache()->Add(request_->url().GetOrigin(), username, password); diff --git a/net/url_request/url_request_test_util.cc b/net/url_request/url_request_test_util.cc index bf72b2b..bffad74 100644 --- a/net/url_request/url_request_test_util.cc +++ b/net/url_request/url_request_test_util.cc @@ -21,13 +21,12 @@ namespace { const int kStageBeforeURLRequest = 1 << 0; const int kStageBeforeSendHeaders = 1 << 1; const int kStageSendHeaders = 1 << 2; -const int kStageAuthRequired = 1 << 3; -const int kStageBeforeRedirect = 1 << 4; -const int kStageResponseStarted = 1 << 5; -const int kStageCompletedSuccess = 1 << 6; -const int kStageCompletedError = 1 << 7; -const int kStageURLRequestDestroyed = 1 << 8; -const int kStageDestruction = 1 << 9; +const int kStageBeforeRedirect = 1 << 3; +const int kStageResponseStarted = 1 << 4; +const int kStageCompletedSuccess = 1 << 5; +const int kStageCompletedError = 1 << 6; +const int kStageURLRequestDestroyed = 1 << 7; +const int kStageDestruction = 1 << 8; } // namespace @@ -167,7 +166,6 @@ TestDelegate::TestDelegate() received_data_before_response_(false), request_failed_(false), have_certificate_errors_(false), - auth_required_(false), buf_(new net::IOBuffer(kBufferSize)) { } @@ -187,7 +185,6 @@ void TestDelegate::OnReceivedRedirect(net::URLRequest* request, void TestDelegate::OnAuthRequired(net::URLRequest* request, net::AuthChallengeInfo* auth_info) { - auth_required_ = true; if (!username_.empty() || !password_.empty()) { request->SetAuth(username_, password_); } else { @@ -374,7 +371,6 @@ void TestNetworkDelegate::OnSendHeaders( next_states_[req_id] = kStageBeforeRedirect | kStageResponseStarted | - kStageAuthRequired | kStageCompletedError; // e.g. proxy resolution problem // Basic authentication sends a second request from the URLRequestHttpJob @@ -449,23 +445,19 @@ void TestNetworkDelegate::OnURLRequestDestroyed( destroyed_requests_++; } +void TestNetworkDelegate::OnHttpTransactionDestroyed(uint64 request_id) { +} + +net::URLRequestJob* TestNetworkDelegate::OnMaybeCreateURLRequestJob( + net::URLRequest* request) { + return NULL; +} + void TestNetworkDelegate::OnPACScriptError(int line_number, const string16& error) { } -net::NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired( - net::URLRequest* request, - const net::AuthChallengeInfo& auth_info, - const AuthCallback& callback, - net::AuthCredentials* credentials) { - int req_id = request->identifier(); - event_order_[req_id] += "OnAuthRequired\n"; - InitRequestStatesIfNew(req_id); - EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) << - event_order_[req_id]; - next_states_[req_id] = kStageBeforeSendHeaders | - kStageResponseStarted | // data: URLs do not trigger sending headers - kStageBeforeRedirect | // a delegate can trigger a redirection - kStageCompletedError; // request canceled by delegate - return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; +void TestNetworkDelegate::OnAuthRequired( + net::URLRequest* reqest, + const net::AuthChallengeInfo& auth_info) { } diff --git a/net/url_request/url_request_test_util.h b/net/url_request/url_request_test_util.h index 11fef2c..f8ef867 100644 --- a/net/url_request/url_request_test_util.h +++ b/net/url_request/url_request_test_util.h @@ -11,7 +11,6 @@ #include <sstream> #include <string> -#include "base/compiler_specific.h" #include "base/path_service.h" #include "base/process_util.h" #include "base/string_util.h" @@ -123,7 +122,6 @@ class TestDelegate : public net::URLRequest::Delegate { } bool request_failed() const { return request_failed_; } bool have_certificate_errors() const { return have_certificate_errors_; } - bool auth_required_called() const { return auth_required_; } // net::URLRequest::Delegate: virtual void OnReceivedRedirect(net::URLRequest* request, const GURL& new_url, @@ -170,7 +168,6 @@ class TestDelegate : public net::URLRequest::Delegate { bool received_data_before_response_; bool request_failed_; bool have_certificate_errors_; - bool auth_required_; std::string data_received_; // our read buffer @@ -194,26 +191,24 @@ class TestNetworkDelegate : public net::NetworkDelegate { // net::NetworkDelegate: virtual int OnBeforeURLRequest(net::URLRequest* request, net::CompletionCallback* callback, - GURL* new_url) OVERRIDE; + GURL* new_url); virtual int OnBeforeSendHeaders(net::URLRequest* request, net::CompletionCallback* callback, - net::HttpRequestHeaders* headers) OVERRIDE; + net::HttpRequestHeaders* headers); virtual void OnSendHeaders(net::URLRequest* request, - const net::HttpRequestHeaders& headers) OVERRIDE; + const net::HttpRequestHeaders& headers); virtual void OnBeforeRedirect(net::URLRequest* request, - const GURL& new_location) OVERRIDE; - virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; - virtual void OnRawBytesRead(const net::URLRequest& request, - int bytes_read) OVERRIDE; - virtual void OnCompleted(net::URLRequest* request) OVERRIDE; - virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE; - virtual void OnPACScriptError(int line_number, - const string16& error) OVERRIDE; - virtual net::NetworkDelegate::AuthRequiredResponse OnAuthRequired( - net::URLRequest* request, - const net::AuthChallengeInfo& auth_info, - const AuthCallback& callback, - net::AuthCredentials* credentials) OVERRIDE; + const GURL& new_location); + virtual void OnResponseStarted(net::URLRequest* request); + virtual void OnRawBytesRead(const net::URLRequest& request, int bytes_read); + virtual void OnCompleted(net::URLRequest* request); + virtual void OnURLRequestDestroyed(net::URLRequest* request); + virtual void OnHttpTransactionDestroyed(uint64 request_id); + virtual net::URLRequestJob* OnMaybeCreateURLRequestJob( + net::URLRequest* request); + virtual void OnPACScriptError(int line_number, const string16& error); + virtual void OnAuthRequired(net::URLRequest* request, + const net::AuthChallengeInfo& auth_info); void InitRequestStatesIfNew(int request_id); diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index 906036b..3dd539b 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -128,27 +128,17 @@ class BlockingNetworkDelegate : public TestNetworkDelegate { BlockingNetworkDelegate() : retval_(net::ERR_IO_PENDING), callback_retval_(net::OK), - auth_retval_(NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING), - auth_callback_retval_( - NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION), ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} void set_retval(int retval) { retval_ = retval; } void set_callback_retval(int retval) { callback_retval_ = retval; } void set_redirect_url(const GURL& url) { redirect_url_ = url; } - void set_auth_retval(NetworkDelegate::AuthRequiredResponse retval) { - auth_retval_ = retval; } - void set_auth_callback_retval(NetworkDelegate::AuthRequiredResponse retval) { - auth_callback_retval_ = retval; } - void set_auth_credentials(const AuthCredentials& auth_credentials) { - auth_credentials_ = auth_credentials; - } private: // TestNetworkDelegate: - virtual int OnBeforeURLRequest(URLRequest* request, - CompletionCallback* callback, - GURL* new_url) OVERRIDE { + virtual int OnBeforeURLRequest(net::URLRequest* request, + net::CompletionCallback* callback, + GURL* new_url) { if (redirect_url_ == request->url()) { // We've already seen this request and redirected elsewhere. return net::OK; @@ -169,51 +159,13 @@ class BlockingNetworkDelegate : public TestNetworkDelegate { return net::ERR_IO_PENDING; } - virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( - URLRequest* request, - const AuthChallengeInfo& auth_info, - const AuthCallback& callback, - AuthCredentials* credentials) OVERRIDE { - TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, - credentials); - switch (auth_retval_) { - case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION: - break; - case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH: - *credentials = auth_credentials_; - case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: - break; - case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING: - MessageLoop::current()->PostTask( - FROM_HERE, - method_factory_.NewRunnableMethod( - &BlockingNetworkDelegate::DoAuthCallback, - callback, credentials)); - break; - } - return auth_retval_; - } - - void DoCallback(CompletionCallback* callback) { + void DoCallback(net::CompletionCallback* callback) { callback->Run(callback_retval_); } - void DoAuthCallback(const AuthCallback& callback, - AuthCredentials* credentials) { - if (auth_callback_retval_ == - NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH) { - *credentials = auth_credentials_; - } - callback.Run(auth_callback_retval_); - } - - int retval_; int callback_retval_; GURL redirect_url_; - NetworkDelegate::AuthRequiredResponse auth_retval_; - NetworkDelegate::AuthRequiredResponse auth_callback_retval_; - AuthCredentials auth_credentials_; ScopedRunnableMethodFactory<BlockingNetworkDelegate> method_factory_; }; @@ -545,218 +497,6 @@ TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { EXPECT_EQ(1, network_delegate.destroyed_requests()); } -// Tests that the network delegate can synchronously complete OnAuthRequired -// by taking no action. This indicates that the NetworkDelegate does not want to -// handle the challenge, and is passing the buck along to the -// URLRequest::Delegate. -TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { - ASSERT_TRUE(test_server_.Start()); - - TestDelegate d; - BlockingNetworkDelegate network_delegate; - network_delegate.set_auth_retval( - NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); - - scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); - context->set_network_delegate(&network_delegate); - context->Init(); - - d.set_username(kUser); - d.set_password(kSecret); - - { - GURL url(test_server_.GetURL("auth-basic")); - TestURLRequest r(url, &d); - r.set_context(context); - r.Start(); - MessageLoop::current()->Run(); - - EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); - EXPECT_EQ(0, r.status().error()); - EXPECT_EQ(200, r.GetResponseCode()); - EXPECT_TRUE(d.auth_required_called()); - EXPECT_EQ(1, network_delegate.created_requests()); - EXPECT_EQ(0, network_delegate.destroyed_requests()); - } - EXPECT_EQ(1, network_delegate.destroyed_requests()); -} - -// Tests that the network delegate can synchronously complete OnAuthRequired -// by setting credentials. -TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { - ASSERT_TRUE(test_server_.Start()); - - TestDelegate d; - BlockingNetworkDelegate network_delegate; - network_delegate.set_auth_retval( - NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); - - AuthCredentials auth_credentials; - auth_credentials.username = kUser; - auth_credentials.password = kSecret; - network_delegate.set_auth_credentials(auth_credentials); - - scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); - context->set_network_delegate(&network_delegate); - context->Init(); - - { - GURL url(test_server_.GetURL("auth-basic")); - TestURLRequest r(url, &d); - r.set_context(context); - r.Start(); - MessageLoop::current()->Run(); - - EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); - EXPECT_EQ(0, r.status().error()); - EXPECT_EQ(200, r.GetResponseCode()); - EXPECT_FALSE(d.auth_required_called()); - EXPECT_EQ(1, network_delegate.created_requests()); - EXPECT_EQ(0, network_delegate.destroyed_requests()); - } - EXPECT_EQ(1, network_delegate.destroyed_requests()); -} - -// Tests that the network delegate can synchronously complete OnAuthRequired -// by cancelling authentication. -TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { - ASSERT_TRUE(test_server_.Start()); - - TestDelegate d; - BlockingNetworkDelegate network_delegate; - network_delegate.set_auth_retval( - NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); - - scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); - context->set_network_delegate(&network_delegate); - context->Init(); - - { - GURL url(test_server_.GetURL("auth-basic")); - TestURLRequest r(url, &d); - r.set_context(context); - r.Start(); - MessageLoop::current()->Run(); - - EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); - EXPECT_EQ(OK, r.status().error()); - EXPECT_EQ(401, r.GetResponseCode()); - EXPECT_FALSE(d.auth_required_called()); - EXPECT_EQ(1, network_delegate.created_requests()); - EXPECT_EQ(0, network_delegate.destroyed_requests()); - } - EXPECT_EQ(1, network_delegate.destroyed_requests()); -} - -// Tests that the network delegate can asynchronously complete OnAuthRequired -// by taking no action. This indicates that the NetworkDelegate does not want -// to handle the challenge, and is passing the buck along to the -// URLRequest::Delegate. -TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { - ASSERT_TRUE(test_server_.Start()); - - TestDelegate d; - BlockingNetworkDelegate network_delegate; - network_delegate.set_auth_retval( - NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); - network_delegate.set_auth_callback_retval( - NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); - - scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); - context->set_network_delegate(&network_delegate); - context->Init(); - - d.set_username(kUser); - d.set_password(kSecret); - - { - GURL url(test_server_.GetURL("auth-basic")); - TestURLRequest r(url, &d); - r.set_context(context); - r.Start(); - MessageLoop::current()->Run(); - - EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); - EXPECT_EQ(0, r.status().error()); - EXPECT_EQ(200, r.GetResponseCode()); - EXPECT_TRUE(d.auth_required_called()); - EXPECT_EQ(1, network_delegate.created_requests()); - EXPECT_EQ(0, network_delegate.destroyed_requests()); - } - EXPECT_EQ(1, network_delegate.destroyed_requests()); -} - -// Tests that the network delegate can asynchronously complete OnAuthRequired -// by setting credentials. -TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetCredentials) { - ASSERT_TRUE(test_server_.Start()); - - TestDelegate d; - BlockingNetworkDelegate network_delegate; - network_delegate.set_auth_retval( - NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); - network_delegate.set_auth_callback_retval( - NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); - - AuthCredentials auth_credentials; - auth_credentials.username = kUser; - auth_credentials.password = kSecret; - network_delegate.set_auth_credentials(auth_credentials); - - scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); - context->set_network_delegate(&network_delegate); - context->Init(); - - { - GURL url(test_server_.GetURL("auth-basic")); - TestURLRequest r(url, &d); - r.set_context(context); - r.Start(); - MessageLoop::current()->Run(); - - EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); - EXPECT_EQ(0, r.status().error()); - EXPECT_EQ(200, r.GetResponseCode()); - EXPECT_FALSE(d.auth_required_called()); - EXPECT_EQ(1, network_delegate.created_requests()); - EXPECT_EQ(0, network_delegate.destroyed_requests()); - } - EXPECT_EQ(1, network_delegate.destroyed_requests()); -} - -// Tests that the network delegate can asynchronously complete OnAuthRequired -// by cancelling authentication. -TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { - ASSERT_TRUE(test_server_.Start()); - - TestDelegate d; - BlockingNetworkDelegate network_delegate; - network_delegate.set_auth_retval( - NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); - network_delegate.set_auth_callback_retval( - NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); - - scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); - context->set_network_delegate(&network_delegate); - context->Init(); - - { - GURL url(test_server_.GetURL("auth-basic")); - TestURLRequest r(url, &d); - r.set_context(context); - r.Start(); - MessageLoop::current()->Run(); - - EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); - EXPECT_EQ(OK, r.status().error()); - EXPECT_EQ(401, r.GetResponseCode()); - EXPECT_FALSE(d.auth_required_called()); - EXPECT_EQ(1, network_delegate.created_requests()); - EXPECT_EQ(0, network_delegate.destroyed_requests()); - } - EXPECT_EQ(1, network_delegate.destroyed_requests()); -} - // In this unit test, we're using the HTTPTestServer as a proxy server and // issuing a CONNECT request with the magic host name "www.server-auth.com". // The HTTPTestServer will return a 401 response, which we should balk at. |