| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/http_auth_handler_mock.h" | 5 #include "net/http/http_auth_handler_mock.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/http/http_auth_challenge_tokenizer.h" | 13 #include "net/http/http_auth_challenge_tokenizer.h" |
| 14 #include "net/http/http_request_info.h" | 14 #include "net/http/http_request_info.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 HttpAuthHandlerMock::HttpAuthHandlerMock() | 19 HttpAuthHandlerMock::HttpAuthHandlerMock() |
| 20 : expected_auth_scheme_("mock"), weak_factory_(this) {} | 20 : HttpAuthHandler("mock"), weak_factory_(this) {} |
| 21 | 21 |
| 22 HttpAuthHandlerMock::~HttpAuthHandlerMock() { | 22 HttpAuthHandlerMock::~HttpAuthHandlerMock() {} |
| 23 } | |
| 24 | 23 |
| 25 void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) { | 24 void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) { |
| 26 generate_async_ = async; | 25 generate_async_ = async; |
| 27 generate_rv_ = rv; | 26 generate_rv_ = rv; |
| 28 } | 27 } |
| 29 | 28 |
| 30 HttpAuth::AuthorizationResult HttpAuthHandlerMock::HandleAnotherChallenge( | 29 HttpAuth::AuthorizationResult HttpAuthHandlerMock::HandleAnotherChallenge( |
| 31 const HttpAuthChallengeTokenizer& challenge) { | 30 const HttpAuthChallengeTokenizer& challenge) { |
| 31 EXPECT_TRUE(challenge.SchemeIs(auth_scheme_)); |
| 32 // If we receive a second challenge for a regular scheme, assume it's a | 32 // If we receive a second challenge for a regular scheme, assume it's a |
| 33 // rejection. Receiving an empty second challenge when expecting multiple | 33 // rejection. Receiving an empty second challenge when expecting multiple |
| 34 // rounds is also considered a rejection. | 34 // rounds is also considered a rejection. |
| 35 if (!expect_multiple_challenges_ || challenge.base64_param().empty()) | 35 if (!expect_multiple_challenges_ || challenge.base64_param().empty()) |
| 36 return HttpAuth::AUTHORIZATION_RESULT_REJECT; | 36 return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
| 37 if (!challenge.SchemeIs(auth_scheme_)) | 37 if (!challenge.SchemeIs(auth_scheme_)) |
| 38 return HttpAuth::AUTHORIZATION_RESULT_INVALID; | 38 return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
| 39 auth_token_ = auth_scheme_; | 39 auth_token_ = auth_scheme_; |
| 40 auth_token_.append(" continuation,"); | 40 auth_token_.append(" continuation,"); |
| 41 auth_token_.append(challenge.base64_param()); | 41 auth_token_.append(challenge.base64_param()); |
| 42 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; | 42 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; |
| 43 } | 43 } |
| 44 | 44 |
| 45 bool HttpAuthHandlerMock::NeedsIdentity() { | 45 bool HttpAuthHandlerMock::NeedsIdentity() { |
| 46 return first_round_; | 46 return first_round_; |
| 47 } | 47 } |
| 48 | 48 |
| 49 bool HttpAuthHandlerMock::AllowsDefaultCredentials() { | 49 bool HttpAuthHandlerMock::AllowsDefaultCredentials() { |
| 50 return allows_default_credentials_; | 50 return allows_default_credentials_; |
| 51 } | 51 } |
| 52 | 52 |
| 53 bool HttpAuthHandlerMock::AllowsExplicitCredentials() { | 53 bool HttpAuthHandlerMock::AllowsExplicitCredentials() { |
| 54 return allows_explicit_credentials_; | 54 return allows_explicit_credentials_; |
| 55 } | 55 } |
| 56 | 56 |
| 57 int HttpAuthHandlerMock::Init(const HttpAuthChallengeTokenizer& challenge) { | 57 int HttpAuthHandlerMock::Init(const HttpAuthChallengeTokenizer& challenge) { |
| 58 EXPECT_TRUE(challenge.SchemeIs(expected_auth_scheme_)) | 58 EXPECT_TRUE(challenge.SchemeIs(auth_scheme_)) |
| 59 << "Mismatched scheme for challenge: " << challenge.challenge_text(); | 59 << "Mismatched scheme for challenge: " << challenge.challenge_text(); |
| 60 EXPECT_TRUE(auth_scheme_.empty()) << "Init was already called."; | 60 EXPECT_TRUE(HttpAuth::IsValidNormalizedScheme(auth_scheme_)) |
| 61 EXPECT_TRUE(HttpAuth::IsValidNormalizedScheme(expected_auth_scheme_)) | |
| 62 << "Invalid expected auth scheme."; | 61 << "Invalid expected auth scheme."; |
| 63 auth_scheme_ = expected_auth_scheme_; | 62 auth_token_ = auth_scheme_ + " auth_token"; |
| 64 auth_token_ = expected_auth_scheme_ + " auth_token"; | |
| 65 if (challenge.params_end() != challenge.params_begin()) { | 63 if (challenge.params_end() != challenge.params_begin()) { |
| 66 auth_token_ += ","; | 64 auth_token_ += ","; |
| 67 auth_token_.append(challenge.params_begin(), challenge.params_end()); | 65 auth_token_.append(challenge.params_begin(), challenge.params_end()); |
| 68 } | 66 } |
| 69 return OK; | 67 return OK; |
| 70 } | 68 } |
| 71 | 69 |
| 72 int HttpAuthHandlerMock::GenerateAuthTokenImpl( | 70 int HttpAuthHandlerMock::GenerateAuthTokenImpl( |
| 73 const AuthCredentials* credentials, | 71 const AuthCredentials* credentials, |
| 74 const HttpRequestInfo& request, | 72 const HttpRequestInfo& request, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 callback.Run(generate_rv_); | 114 callback.Run(generate_rv_); |
| 117 } | 115 } |
| 118 | 116 |
| 119 HttpAuthHandlerMock::Factory::Factory() {} | 117 HttpAuthHandlerMock::Factory::Factory() {} |
| 120 | 118 |
| 121 HttpAuthHandlerMock::Factory::~Factory() { | 119 HttpAuthHandlerMock::Factory::~Factory() { |
| 122 } | 120 } |
| 123 | 121 |
| 124 void HttpAuthHandlerMock::Factory::AddMockHandler( | 122 void HttpAuthHandlerMock::Factory::AddMockHandler( |
| 125 scoped_ptr<HttpAuthHandler> handler, | 123 scoped_ptr<HttpAuthHandler> handler, |
| 126 CreateReason reason, | 124 HttpAuthHandlerCreateReason reason) { |
| 127 HttpAuth::Target target) { | 125 if (reason == HttpAuthHandlerCreateReason::PREEMPTIVE) |
| 128 if (reason == CREATE_PREEMPTIVE) | 126 preemptive_handlers_.push_back(handler.Pass()); |
| 129 preemptive_handlers_[target].push_back(handler.Pass()); | |
| 130 else | 127 else |
| 131 challenge_handlers_[target].push_back(handler.Pass()); | 128 challenge_handlers_.push_back(handler.Pass()); |
| 132 } | 129 } |
| 133 | 130 |
| 134 bool HttpAuthHandlerMock::Factory::HaveAuthHandlers( | 131 bool HttpAuthHandlerMock::Factory::HaveAuthHandlers() const { |
| 135 HttpAuth::Target target) const { | 132 return !challenge_handlers_.empty() || !preemptive_handlers_.empty(); |
| 136 return !challenge_handlers_[target].empty() || | |
| 137 !preemptive_handlers_[target].empty(); | |
| 138 } | 133 } |
| 139 | 134 |
| 140 int HttpAuthHandlerMock::Factory::CreateAuthHandler( | 135 scoped_ptr<HttpAuthHandler> HttpAuthHandlerMock::Factory::GetNextAuthHandler( |
| 141 const HttpAuthChallengeTokenizer& challenge, | 136 ScopedVector<HttpAuthHandler>* handler_list) { |
| 137 if (handler_list->empty()) |
| 138 return scoped_ptr<HttpAuthHandler>(); |
| 139 scoped_ptr<HttpAuthHandler> tmp_handler(handler_list->front()); |
| 140 handler_list->weak_erase(handler_list->begin()); |
| 141 return tmp_handler; |
| 142 } |
| 143 |
| 144 scoped_ptr<HttpAuthHandler> |
| 145 HttpAuthHandlerMock::Factory::CreateAuthHandlerForScheme( |
| 146 const std::string& scheme) { |
| 147 scoped_ptr<HttpAuthHandler> handler = |
| 148 GetNextAuthHandler(&challenge_handlers_); |
| 149 EXPECT_TRUE(!handler || handler->auth_scheme() == scheme) |
| 150 << "Next auth handler is for scheme " << handler->auth_scheme() |
| 151 << " while request scheme is " << scheme; |
| 152 return handler.Pass(); |
| 153 } |
| 154 |
| 155 scoped_ptr<HttpAuthHandler> |
| 156 HttpAuthHandlerMock::Factory::CreateAndInitPreemptiveAuthHandler( |
| 157 HttpAuthCache::Entry* cache_entry, |
| 158 const HttpAuthChallengeTokenizer& tokenizer, |
| 142 HttpAuth::Target target, | 159 HttpAuth::Target target, |
| 143 const GURL& origin, | 160 const BoundNetLog& net_log) { |
| 144 CreateReason reason, | 161 scoped_ptr<HttpAuthHandler> handler = |
| 145 int nonce_count, | 162 GetNextAuthHandler(&preemptive_handlers_); |
| 146 const BoundNetLog& net_log, | 163 if (handler) { |
| 147 scoped_ptr<HttpAuthHandler>* handler) { | 164 handler->HandleInitialChallenge(tokenizer, target, cache_entry->origin(), |
| 148 ScopedVector<HttpAuthHandler>& handler_list = | 165 net_log); |
| 149 reason == CREATE_PREEMPTIVE ? preemptive_handlers_[target] | 166 } |
| 150 : challenge_handlers_[target]; | 167 return handler; |
| 151 if (handler_list.empty()) | |
| 152 return ERR_UNEXPECTED; | |
| 153 scoped_ptr<HttpAuthHandler> tmp_handler(handler_list.front()); | |
| 154 handler_list.weak_erase(handler_list.begin()); | |
| 155 int result = | |
| 156 tmp_handler->HandleInitialChallenge(challenge, target, origin, net_log); | |
| 157 if (result == OK) | |
| 158 handler->swap(tmp_handler); | |
| 159 return result; | |
| 160 } | 168 } |
| 161 | 169 |
| 162 } // namespace net | 170 } // namespace net |
| OLD | NEW |