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/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/string_util.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "net/base/net_errors.h"
13 #include "net/http/http_auth_challenge_tokenizer.h"
14 #include "net/http/http_request_info.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 HttpAuthHandlerMock::HttpAuthHandlerMock()
20 : resolve_(RESOLVE_INIT
),
21 generate_async_(false),
25 connection_based_(false),
26 allows_default_credentials_(false),
27 allows_explicit_credentials_(true),
31 HttpAuthHandlerMock::~HttpAuthHandlerMock() {
34 void HttpAuthHandlerMock::SetResolveExpectation(Resolve resolve
) {
35 EXPECT_EQ(RESOLVE_INIT
, resolve_
);
39 bool HttpAuthHandlerMock::NeedsCanonicalName() {
45 resolve_
= RESOLVE_TESTED
;
53 int HttpAuthHandlerMock::ResolveCanonicalName(
54 HostResolver
* host_resolver
, const CompletionCallback
& callback
) {
55 EXPECT_NE(RESOLVE_TESTED
, resolve_
);
59 resolve_
= RESOLVE_TESTED
;
62 EXPECT_TRUE(callback_
.is_null());
65 base::ThreadTaskRunnerHandle::Get()->PostTask(
66 FROM_HERE
, base::Bind(&HttpAuthHandlerMock::OnResolveCanonicalName
,
67 weak_factory_
.GetWeakPtr()));
76 void HttpAuthHandlerMock::SetGenerateExpectation(bool async
, int rv
) {
77 generate_async_
= async
;
81 HttpAuth::AuthorizationResult
HttpAuthHandlerMock::HandleAnotherChallenge(
82 HttpAuthChallengeTokenizer
* challenge
) {
83 // If we receive an empty challenge for a connection based scheme, or a second
84 // challenge for a non connection based scheme, assume it's a rejection.
85 if (!is_connection_based() || challenge
->base64_param().empty())
86 return HttpAuth::AUTHORIZATION_RESULT_REJECT
;
87 if (!base::LowerCaseEqualsASCII(challenge
->scheme(), "mock"))
88 return HttpAuth::AUTHORIZATION_RESULT_INVALID
;
89 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT
;
92 bool HttpAuthHandlerMock::NeedsIdentity() {
96 bool HttpAuthHandlerMock::AllowsDefaultCredentials() {
97 return allows_default_credentials_
;
100 bool HttpAuthHandlerMock::AllowsExplicitCredentials() {
101 return allows_explicit_credentials_
;
104 bool HttpAuthHandlerMock::Init(HttpAuthChallengeTokenizer
* challenge
) {
105 auth_scheme_
= HttpAuth::AUTH_SCHEME_MOCK
;
107 properties_
= connection_based_
? IS_CONNECTION_BASED
: 0;
111 int HttpAuthHandlerMock::GenerateAuthTokenImpl(
112 const AuthCredentials
* credentials
,
113 const HttpRequestInfo
* request
,
114 const CompletionCallback
& callback
,
115 std::string
* auth_token
) {
116 first_round_
= false;
117 request_url_
= request
->url
;
118 if (generate_async_
) {
119 EXPECT_TRUE(callback_
.is_null());
120 EXPECT_TRUE(auth_token_
== NULL
);
121 callback_
= callback
;
122 auth_token_
= auth_token
;
123 base::ThreadTaskRunnerHandle::Get()->PostTask(
124 FROM_HERE
, base::Bind(&HttpAuthHandlerMock::OnGenerateAuthToken
,
125 weak_factory_
.GetWeakPtr()));
126 return ERR_IO_PENDING
;
128 if (generate_rv_
== OK
)
129 *auth_token
= "auth_token";
134 void HttpAuthHandlerMock::OnResolveCanonicalName() {
135 EXPECT_EQ(RESOLVE_ASYNC
, resolve_
);
136 EXPECT_TRUE(!callback_
.is_null());
137 resolve_
= RESOLVE_TESTED
;
138 CompletionCallback callback
= callback_
;
143 void HttpAuthHandlerMock::OnGenerateAuthToken() {
144 EXPECT_TRUE(generate_async_
);
145 EXPECT_TRUE(!callback_
.is_null());
146 if (generate_rv_
== OK
)
147 *auth_token_
= "auth_token";
149 CompletionCallback callback
= callback_
;
151 callback
.Run(generate_rv_
);
154 HttpAuthHandlerMock::Factory::Factory()
155 : do_init_from_challenge_(false) {
156 // TODO(cbentzel): Default do_init_from_challenge_ to true.
159 HttpAuthHandlerMock::Factory::~Factory() {
162 void HttpAuthHandlerMock::Factory::AddMockHandler(
163 HttpAuthHandler
* handler
, HttpAuth::Target target
) {
164 handlers_
[target
].push_back(handler
);
167 int HttpAuthHandlerMock::Factory::CreateAuthHandler(
168 HttpAuthChallengeTokenizer
* challenge
,
169 HttpAuth::Target target
,
173 const BoundNetLog
& net_log
,
174 scoped_ptr
<HttpAuthHandler
>* handler
) {
175 if (handlers_
[target
].empty())
176 return ERR_UNEXPECTED
;
177 scoped_ptr
<HttpAuthHandler
> tmp_handler(handlers_
[target
][0]);
178 std::vector
<HttpAuthHandler
*>& handlers
= handlers_
[target
].get();
179 handlers
.erase(handlers
.begin());
180 if (do_init_from_challenge_
&&
181 !tmp_handler
->InitFromChallenge(challenge
, target
, origin
, net_log
))
182 return ERR_INVALID_RESPONSE
;
183 handler
->swap(tmp_handler
);