[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / net / http / http_auth_handler_mock.cc
blobf800c192ac4060e12695b8cbb5cd22e3a17daee2
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"
7 #include "base/bind.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"
17 namespace net {
19 HttpAuthHandlerMock::HttpAuthHandlerMock()
20 : resolve_(RESOLVE_INIT),
21 generate_async_(false),
22 generate_rv_(OK),
23 auth_token_(NULL),
24 first_round_(true),
25 connection_based_(false),
26 allows_default_credentials_(false),
27 allows_explicit_credentials_(true),
28 weak_factory_(this) {
31 HttpAuthHandlerMock::~HttpAuthHandlerMock() {
34 void HttpAuthHandlerMock::SetResolveExpectation(Resolve resolve) {
35 EXPECT_EQ(RESOLVE_INIT, resolve_);
36 resolve_ = resolve;
39 bool HttpAuthHandlerMock::NeedsCanonicalName() {
40 switch (resolve_) {
41 case RESOLVE_SYNC:
42 case RESOLVE_ASYNC:
43 return true;
44 case RESOLVE_SKIP:
45 resolve_ = RESOLVE_TESTED;
46 return false;
47 default:
48 NOTREACHED();
49 return false;
53 int HttpAuthHandlerMock::ResolveCanonicalName(
54 HostResolver* host_resolver, const CompletionCallback& callback) {
55 EXPECT_NE(RESOLVE_TESTED, resolve_);
56 int rv = OK;
57 switch (resolve_) {
58 case RESOLVE_SYNC:
59 resolve_ = RESOLVE_TESTED;
60 break;
61 case RESOLVE_ASYNC:
62 EXPECT_TRUE(callback_.is_null());
63 rv = ERR_IO_PENDING;
64 callback_ = callback;
65 base::ThreadTaskRunnerHandle::Get()->PostTask(
66 FROM_HERE, base::Bind(&HttpAuthHandlerMock::OnResolveCanonicalName,
67 weak_factory_.GetWeakPtr()));
68 break;
69 default:
70 NOTREACHED();
71 break;
73 return rv;
76 void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) {
77 generate_async_ = async;
78 generate_rv_ = rv;
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() {
93 return first_round_;
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;
106 score_ = 1;
107 properties_ = connection_based_ ? IS_CONNECTION_BASED : 0;
108 return true;
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;
127 } else {
128 if (generate_rv_ == OK)
129 *auth_token = "auth_token";
130 return generate_rv_;
134 void HttpAuthHandlerMock::OnResolveCanonicalName() {
135 EXPECT_EQ(RESOLVE_ASYNC, resolve_);
136 EXPECT_TRUE(!callback_.is_null());
137 resolve_ = RESOLVE_TESTED;
138 CompletionCallback callback = callback_;
139 callback_.Reset();
140 callback.Run(OK);
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";
148 auth_token_ = NULL;
149 CompletionCallback callback = callback_;
150 callback_.Reset();
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,
170 const GURL& origin,
171 CreateReason reason,
172 int nonce_count,
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);
184 return OK;
187 } // namespace net