1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/http/http_auth_handler_mock.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_util.h"
10 #include "net/base/net_errors.h"
11 #include "net/http/http_request_info.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 HttpAuthHandlerMock::HttpAuthHandlerMock()
17 : resolve_(RESOLVE_INIT),
19 generate_async_(false),
23 connection_based_(false),
24 allows_default_credentials_(false),
25 allows_explicit_credentials_(true) {
28 HttpAuthHandlerMock::~HttpAuthHandlerMock() {
31 void HttpAuthHandlerMock::SetResolveExpectation(Resolve resolve) {
32 EXPECT_EQ(RESOLVE_INIT, resolve_);
36 bool HttpAuthHandlerMock::NeedsCanonicalName() {
42 resolve_ = RESOLVE_TESTED;
50 int HttpAuthHandlerMock::ResolveCanonicalName(
51 HostResolver* host_resolver, const CompletionCallback& callback) {
52 EXPECT_NE(RESOLVE_TESTED, resolve_);
56 resolve_ = RESOLVE_TESTED;
59 EXPECT_TRUE(callback_.is_null());
62 base::MessageLoop::current()->PostTask(
64 base::Bind(&HttpAuthHandlerMock::OnResolveCanonicalName,
65 weak_factory_.GetWeakPtr()));
74 void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) {
75 generate_async_ = async;
79 HttpAuth::AuthorizationResult HttpAuthHandlerMock::HandleAnotherChallenge(
80 HttpAuth::ChallengeTokenizer* challenge) {
81 // If we receive an empty challenge for a connection based scheme, or a second
82 // challenge for a non connection based scheme, assume it's a rejection.
83 if (!is_connection_based() || challenge->base64_param().empty())
84 return HttpAuth::AUTHORIZATION_RESULT_REJECT;
85 if (!LowerCaseEqualsASCII(challenge->scheme(), "mock"))
86 return HttpAuth::AUTHORIZATION_RESULT_INVALID;
87 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT;
90 bool HttpAuthHandlerMock::NeedsIdentity() {
94 bool HttpAuthHandlerMock::AllowsDefaultCredentials() {
95 return allows_default_credentials_;
98 bool HttpAuthHandlerMock::AllowsExplicitCredentials() {
99 return allows_explicit_credentials_;
102 bool HttpAuthHandlerMock::Init(HttpAuth::ChallengeTokenizer* challenge) {
103 auth_scheme_ = HttpAuth::AUTH_SCHEME_MOCK;
105 properties_ = connection_based_ ? IS_CONNECTION_BASED : 0;
109 int HttpAuthHandlerMock::GenerateAuthTokenImpl(
110 const AuthCredentials* credentials,
111 const HttpRequestInfo* request,
112 const CompletionCallback& callback,
113 std::string* auth_token) {
114 first_round_ = false;
115 request_url_ = request->url;
116 if (generate_async_) {
117 EXPECT_TRUE(callback_.is_null());
118 EXPECT_TRUE(auth_token_ == NULL);
119 callback_ = callback;
120 auth_token_ = auth_token;
121 base::MessageLoop::current()->PostTask(
123 base::Bind(&HttpAuthHandlerMock::OnGenerateAuthToken,
124 weak_factory_.GetWeakPtr()));
125 return ERR_IO_PENDING;
127 if (generate_rv_ == OK)
128 *auth_token = "auth_token";
133 void HttpAuthHandlerMock::OnResolveCanonicalName() {
134 EXPECT_EQ(RESOLVE_ASYNC, resolve_);
135 EXPECT_TRUE(!callback_.is_null());
136 resolve_ = RESOLVE_TESTED;
137 CompletionCallback callback = callback_;
142 void HttpAuthHandlerMock::OnGenerateAuthToken() {
143 EXPECT_TRUE(generate_async_);
144 EXPECT_TRUE(!callback_.is_null());
145 if (generate_rv_ == OK)
146 *auth_token_ = "auth_token";
148 CompletionCallback callback = callback_;
150 callback.Run(generate_rv_);
153 HttpAuthHandlerMock::Factory::Factory()
154 : do_init_from_challenge_(false) {
155 // TODO(cbentzel): Default do_init_from_challenge_ to true.
158 HttpAuthHandlerMock::Factory::~Factory() {
161 void HttpAuthHandlerMock::Factory::AddMockHandler(
162 HttpAuthHandler* handler, HttpAuth::Target target) {
163 handlers_[target].push_back(handler);
166 int HttpAuthHandlerMock::Factory::CreateAuthHandler(
167 HttpAuth::ChallengeTokenizer* challenge,
168 HttpAuth::Target target,
172 const BoundNetLog& net_log,
173 scoped_ptr<HttpAuthHandler>* handler) {
174 if (handlers_[target].empty())
175 return ERR_UNEXPECTED;
176 scoped_ptr<HttpAuthHandler> tmp_handler(handlers_[target][0]);
177 std::vector<HttpAuthHandler*>& handlers = handlers_[target].get();
178 handlers.erase(handlers.begin());
179 if (do_init_from_challenge_ &&
180 !tmp_handler->InitFromChallenge(challenge, target, origin, net_log))
181 return ERR_INVALID_RESPONSE;
182 handler->swap(tmp_handler);