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

Unified Diff: net/http/http_auth_handler_mock.cc

Issue 1391053002: [net/http auth] Make HttpAuthHandler challenge handling asynchronous. Base URL: https://chromium.googlesource.com/chromium/src.git@auth-handler-init-split
Patch Set: Created 5 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/http/http_auth_handler_mock.h ('k') | net/http/http_auth_handler_negotiate.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/http/http_auth_handler_mock.cc
diff --git a/net/http/http_auth_handler_mock.cc b/net/http/http_auth_handler_mock.cc
index c35bde579cc939d2c11e9fad6b20bbbb04d00b51..f43e0d383309fd711897f3d264b747095d85eeac 100644
--- a/net/http/http_auth_handler_mock.cc
+++ b/net/http/http_auth_handler_mock.cc
@@ -5,6 +5,7 @@
#include "net/http/http_auth_handler_mock.h"
#include "base/bind.h"
+#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_util.h"
@@ -21,6 +22,11 @@ HttpAuthHandlerMock::HttpAuthHandlerMock()
HttpAuthHandlerMock::~HttpAuthHandlerMock() {}
+void HttpAuthHandlerMock::SetInitExpectation(bool async, int rv) {
+ init_async_ = async;
+ init_rv_ = rv;
+}
+
void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) {
generate_async_ = async;
generate_rv_ = rv;
@@ -28,42 +34,104 @@ void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) {
HttpAuth::AuthorizationResult HttpAuthHandlerMock::HandleAnotherChallenge(
const HttpAuthChallengeTokenizer& challenge) {
- EXPECT_TRUE(challenge.SchemeIs(auth_scheme_));
+ EXPECT_TRUE(challenge.SchemeIs(auth_scheme_))
+ << "Auth scheme of challenge doesn't match scheme of AuthHandler.";
+ EXPECT_TRUE(initialized_) << __FUNCTION__ << " called without calling Init.";
+ EXPECT_TRUE(initialization_succeeded) << __FUNCTION__
+ << " called after a failed Init.";
// If we receive a second challenge for a regular scheme, assume it's a
// rejection. Receiving an empty second challenge when expecting multiple
// rounds is also considered a rejection.
- if (!expect_multiple_challenges_ || challenge.base64_param().empty())
+ base::StringPiece challenge_params(challenge.params_begin(),
+ challenge.params_end());
+ if (!expect_multiple_challenges_ || challenge_params.empty())
return HttpAuth::AUTHORIZATION_RESULT_REJECT;
- if (!challenge.SchemeIs(auth_scheme_))
- return HttpAuth::AUTHORIZATION_RESULT_INVALID;
auth_token_ = auth_scheme_;
auth_token_.append(" continuation,");
- auth_token_.append(challenge.base64_param());
+ auth_token_.append(challenge.params_begin(), challenge.params_end());
return HttpAuth::AUTHORIZATION_RESULT_ACCEPT;
}
bool HttpAuthHandlerMock::NeedsIdentity() {
+ EXPECT_TRUE(initialized_) << __FUNCTION__ << " called without calling Init.";
+ EXPECT_TRUE(initialization_succeeded) << __FUNCTION__
+ << " called after a failed Init.";
return first_round_;
}
bool HttpAuthHandlerMock::AllowsDefaultCredentials() {
+ EXPECT_TRUE(initialized_) << __FUNCTION__ << " called without calling Init.";
+ EXPECT_TRUE(initialization_succeeded) << __FUNCTION__
+ << " called after a failed Init.";
return allows_default_credentials_;
}
bool HttpAuthHandlerMock::AllowsExplicitCredentials() {
+ EXPECT_TRUE(initialized_) << __FUNCTION__ << " called without calling Init.";
+ EXPECT_TRUE(initialization_succeeded) << __FUNCTION__
+ << " called after a failed Init.";
return allows_explicit_credentials_;
}
-int HttpAuthHandlerMock::Init(const HttpAuthChallengeTokenizer& challenge) {
+int HttpAuthHandlerMock::InitializeFromChallengeInternal(
+ const HttpAuthChallengeTokenizer& challenge,
+ const HttpResponseInfo& response_with_challenge,
+ const CompletionCallback& callback) {
+ EXPECT_FALSE(initialized_) << "Init already called once.";
EXPECT_TRUE(challenge.SchemeIs(auth_scheme_))
<< "Mismatched scheme for challenge: " << challenge.challenge_text();
EXPECT_TRUE(HttpAuth::IsValidNormalizedScheme(auth_scheme_))
<< "Invalid expected auth scheme.";
+
+ base::StringPiece challenge_params(challenge.params_begin(),
+ challenge.params_end());
+ initialized_ = true;
+
auth_token_ = auth_scheme_ + " auth_token";
- if (challenge.params_end() != challenge.params_begin()) {
+ if (!challenge_params.empty()) {
+ auth_token_ += ",";
+ auth_token_.append(challenge.params_begin(), challenge.params_end());
+ }
+
+ if (init_async_) {
+ callback_ = callback;
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(&HttpAuthHandlerMock::OnInitializeComplete,
+ weak_factory_.GetWeakPtr()));
+ return ERR_IO_PENDING;
+ }
+
+ initialization_succeeded = (init_rv_ == OK);
+ return init_rv_;
+}
+
+void HttpAuthHandlerMock::OnInitializeComplete() {
+ ASSERT_FALSE(callback_.is_null());
+ initialization_succeeded = (init_rv_ == OK);
+ base::ResetAndReturn(&callback_).Run(init_rv_);
+}
+
+int HttpAuthHandlerMock::InitializeFromCacheEntryInternal(
+ HttpAuthCache::Entry* cache_entry) {
+ EXPECT_FALSE(initialized_) << "Init already called once.";
+ EXPECT_EQ(auth_scheme_, cache_entry->scheme())
+ << "Mismatched scheme for challenge: " << cache_entry->auth_challenge();
+ EXPECT_TRUE(HttpAuth::IsValidNormalizedScheme(auth_scheme_))
+ << "Invalid expected auth scheme.";
+
+ HttpAuthChallengeTokenizer challenge(cache_entry->auth_challenge().begin(),
+ cache_entry->auth_challenge().end());
+ EXPECT_TRUE(challenge.SchemeIs(auth_scheme_))
+ << "Mismatched scheme for challenge: " << challenge.challenge_text();
+
+ auth_token_ = auth_scheme_ + " preemptive_auth_token";
+ if (challenge.params_begin() != challenge.params_end()) {
auth_token_ += ",";
auth_token_.append(challenge.params_begin(), challenge.params_end());
}
+
+ initialized_ = true;
+ initialization_succeeded = true;
return OK;
}
@@ -72,17 +140,20 @@ int HttpAuthHandlerMock::GenerateAuthTokenImpl(
const HttpRequestInfo& request,
const CompletionCallback& callback,
std::string* auth_token) {
+ EXPECT_TRUE(initialized_) << __FUNCTION__ << " called without calling Init.";
+ EXPECT_TRUE(initialization_succeeded) << __FUNCTION__
+ << " called after a failed Init.";
first_round_ = false;
request_url_ = request.url;
- if (!credentials || credentials->Empty()) {
- EXPECT_TRUE(AllowsDefaultCredentials()) << "Credentials must be specified "
- "if the handler doesn't support "
- "default credentials.";
- } else {
+ if (credentials) {
EXPECT_TRUE(AllowsExplicitCredentials()) << "Explicit credentials can only "
"be specified if the handler "
"supports it.";
+ } else {
+ EXPECT_TRUE(AllowsDefaultCredentials()) << "Credentials must be specified "
+ "if the handler doesn't support "
+ "default credentials.";
}
if (generate_async_) {
@@ -93,7 +164,7 @@ int HttpAuthHandlerMock::GenerateAuthTokenImpl(
callback_ = callback;
generate_auth_token_buffer_ = auth_token;
base::ThreadTaskRunnerHandle::Get()->PostTask(
- FROM_HERE, base::Bind(&HttpAuthHandlerMock::OnGenerateAuthToken,
+ FROM_HERE, base::Bind(&HttpAuthHandlerMock::OnGenerateAuthTokenComplete,
weak_factory_.GetWeakPtr()));
return ERR_IO_PENDING;
} else {
@@ -103,15 +174,13 @@ int HttpAuthHandlerMock::GenerateAuthTokenImpl(
}
}
-void HttpAuthHandlerMock::OnGenerateAuthToken() {
+void HttpAuthHandlerMock::OnGenerateAuthTokenComplete() {
EXPECT_TRUE(generate_async_);
EXPECT_TRUE(!callback_.is_null());
if (generate_rv_ == OK)
*generate_auth_token_buffer_ = auth_token_;
generate_auth_token_buffer_ = nullptr;
- CompletionCallback callback = callback_;
- callback_.Reset();
- callback.Run(generate_rv_);
+ base::ResetAndReturn(&callback_).Run(generate_rv_);
}
HttpAuthHandlerMock::Factory::Factory() {}
@@ -149,21 +218,18 @@ HttpAuthHandlerMock::Factory::CreateAuthHandlerForScheme(
EXPECT_TRUE(!handler || handler->auth_scheme() == scheme)
<< "Next auth handler is for scheme " << handler->auth_scheme()
<< " while request scheme is " << scheme;
- return handler.Pass();
+ return handler;
}
scoped_ptr<HttpAuthHandler>
HttpAuthHandlerMock::Factory::CreateAndInitPreemptiveAuthHandler(
HttpAuthCache::Entry* cache_entry,
- const HttpAuthChallengeTokenizer& tokenizer,
HttpAuth::Target target,
const BoundNetLog& net_log) {
scoped_ptr<HttpAuthHandler> handler =
GetNextAuthHandler(&preemptive_handlers_);
- if (handler) {
- handler->HandleInitialChallenge(tokenizer, target, cache_entry->origin(),
- net_log);
- }
+ if (handler)
+ handler->InitializeFromCacheEntry(cache_entry, target, net_log);
return handler;
}
« no previous file with comments | « net/http/http_auth_handler_mock.h ('k') | net/http/http_auth_handler_negotiate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698