1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/http/http_auth_handler_mock.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "net/base/net_errors.h"
#include "net/http/http_request_info.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
HttpAuthHandlerMock::HttpAuthHandlerMock()
: resolve_(RESOLVE_INIT), user_callback_(NULL),
ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
generate_async_(false), generate_rv_(OK),
auth_token_(NULL),
first_round_(true),
connection_based_(false) {
}
HttpAuthHandlerMock::~HttpAuthHandlerMock() {
}
void HttpAuthHandlerMock::SetResolveExpectation(Resolve resolve) {
EXPECT_EQ(RESOLVE_INIT, resolve_);
resolve_ = resolve;
}
bool HttpAuthHandlerMock::NeedsCanonicalName() {
switch (resolve_) {
case RESOLVE_SYNC:
case RESOLVE_ASYNC:
return true;
case RESOLVE_SKIP:
resolve_ = RESOLVE_TESTED;
return false;
default:
NOTREACHED();
return false;
}
}
int HttpAuthHandlerMock::ResolveCanonicalName(HostResolver* host_resolver,
CompletionCallback* callback) {
EXPECT_NE(RESOLVE_TESTED, resolve_);
int rv = OK;
switch (resolve_) {
case RESOLVE_SYNC:
resolve_ = RESOLVE_TESTED;
break;
case RESOLVE_ASYNC:
EXPECT_TRUE(user_callback_ == NULL);
rv = ERR_IO_PENDING;
user_callback_ = callback;
MessageLoop::current()->PostTask(
FROM_HERE, method_factory_.NewRunnableMethod(
&HttpAuthHandlerMock::OnResolveCanonicalName));
break;
default:
NOTREACHED();
break;
}
return rv;
}
void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) {
generate_async_ = async;
generate_rv_ = rv;
}
bool HttpAuthHandlerMock::Init(HttpAuth::ChallengeTokenizer* challenge) {
scheme_ = "mock";
score_ = 1;
properties_ = connection_based_ ? IS_CONNECTION_BASED : 0;
return true;
}
HttpAuth::AuthorizationResult HttpAuthHandlerMock::HandleAnotherChallenge(
HttpAuth::ChallengeTokenizer* challenge) {
if (!is_connection_based())
return HttpAuth::AUTHORIZATION_RESULT_REJECT;
if (!LowerCaseEqualsASCII(challenge->scheme(), "mock"))
return HttpAuth::AUTHORIZATION_RESULT_INVALID;
return HttpAuth::AUTHORIZATION_RESULT_ACCEPT;
}
int HttpAuthHandlerMock::GenerateAuthTokenImpl(const string16* username,
const string16* password,
const HttpRequestInfo* request,
CompletionCallback* callback,
std::string* auth_token) {
first_round_ = false;
request_url_ = request->url;
if (generate_async_) {
EXPECT_TRUE(user_callback_ == NULL);
EXPECT_TRUE(auth_token_ == NULL);
user_callback_ = callback;
auth_token_ = auth_token;
MessageLoop::current()->PostTask(
FROM_HERE, method_factory_.NewRunnableMethod(
&HttpAuthHandlerMock::OnGenerateAuthToken));
return ERR_IO_PENDING;
} else {
if (generate_rv_ == OK)
*auth_token = "auth_token";
return generate_rv_;
}
}
void HttpAuthHandlerMock::OnResolveCanonicalName() {
EXPECT_EQ(RESOLVE_ASYNC, resolve_);
EXPECT_TRUE(user_callback_ != NULL);
resolve_ = RESOLVE_TESTED;
CompletionCallback* callback = user_callback_;
user_callback_ = NULL;
callback->Run(OK);
}
void HttpAuthHandlerMock::OnGenerateAuthToken() {
EXPECT_TRUE(generate_async_);
EXPECT_TRUE(user_callback_ != NULL);
if (generate_rv_ == OK)
*auth_token_ = "auth_token";
auth_token_ = NULL;
CompletionCallback* callback = user_callback_;
user_callback_ = NULL;
callback->Run(generate_rv_);
}
HttpAuthHandlerMock::Factory::Factory()
: do_init_from_challenge_(false) {
// TODO(cbentzel): Default do_init_from_challenge_ to true.
}
HttpAuthHandlerMock::Factory::~Factory() {
}
void HttpAuthHandlerMock::Factory::set_mock_handler(
HttpAuthHandler* handler, HttpAuth::Target target) {
EXPECT_TRUE(handlers_[target].get() == NULL);
handlers_[target].reset(handler);
}
int HttpAuthHandlerMock::Factory::CreateAuthHandler(
HttpAuth::ChallengeTokenizer* challenge,
HttpAuth::Target target,
const GURL& origin,
CreateReason reason,
int nonce_count,
const BoundNetLog& net_log,
scoped_ptr<HttpAuthHandler>* handler) {
if (!handlers_[target].get())
return ERR_UNEXPECTED;
scoped_ptr<HttpAuthHandler> tmp_handler(handlers_[target].release());
if (do_init_from_challenge_ &&
!tmp_handler->InitFromChallenge(challenge, target, origin, net_log))
return ERR_INVALID_RESPONSE;
handler->swap(tmp_handler);
return OK;
}
} // namespace net
|