| 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/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/http/http_auth_challenge_tokenizer.h" | 11 #include "net/http/http_auth_challenge_tokenizer.h" |
| 12 #include "net/http/http_request_info.h" | 12 #include "net/http/http_request_info.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 | 16 |
| 17 HttpAuthHandlerMock::HttpAuthHandlerMock() | 17 HttpAuthHandlerMock::HttpAuthHandlerMock() |
| 18 : resolve_(RESOLVE_INIT), | 18 : resolve_(RESOLVE_INIT), |
| 19 weak_factory_(this), | 19 weak_factory_(this), |
| 20 generate_async_(false), | 20 generate_async_(false), |
| 21 generate_rv_(OK), | 21 generate_rv_(OK), |
| 22 auth_token_(NULL), | 22 auth_token_(NULL), |
| 23 first_round_(true), | 23 first_round_(true), |
| 24 connection_based_(false), | 24 connection_based_(false), |
| 25 allows_default_credentials_(false), | 25 allows_default_credentials_(false), |
| 26 allows_explicit_credentials_(true) { | 26 allows_explicit_credentials_(true) { |
| 27 } | 27 } |
| 28 | 28 |
| 29 HttpAuthHandlerMock::~HttpAuthHandlerMock() { | 29 HttpAuthHandlerMock::~HttpAuthHandlerMock() { |
| 30 } | 30 } |
| 31 | 31 |
| 32 void HttpAuthHandlerMock::SetResolveExpectation(Resolve resolve) { | 32 void HttpAuthHandlerMock::SetResolveExpectation(Resolve resolve) { |
| 33 EXPECT_EQ(RESOLVE_INIT, resolve_); | 33 EXPECT_EQ(RESOLVE_INIT, resolve_); |
| 34 resolve_ = resolve; | 34 resolve_ = resolve; |
| 35 } | 35 } |
| 36 | 36 |
| 37 bool HttpAuthHandlerMock::NeedsCanonicalName() { | 37 bool HttpAuthHandlerMock::NeedsCanonicalName() { |
| 38 switch (resolve_) { | 38 switch (resolve_) { |
| 39 case RESOLVE_SYNC: | 39 case RESOLVE_SYNC: |
| 40 case RESOLVE_ASYNC: | 40 case RESOLVE_ASYNC: |
| 41 return true; | 41 return true; |
| 42 case RESOLVE_SKIP: | 42 case RESOLVE_SKIP: |
| 43 resolve_ = RESOLVE_TESTED; | 43 resolve_ = RESOLVE_TESTED; |
| 44 return false; | 44 return false; |
| 45 default: | 45 default: |
| 46 NOTREACHED(); | 46 NOTREACHED(); |
| 47 return false; | 47 return false; |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 int HttpAuthHandlerMock::ResolveCanonicalName( | 51 int HttpAuthHandlerMock::ResolveCanonicalName( |
| 52 HostResolver* host_resolver, const CompletionCallback& callback) { | 52 HostResolver* host_resolver, |
| 53 const CompletionCallback& callback) { |
| 53 EXPECT_NE(RESOLVE_TESTED, resolve_); | 54 EXPECT_NE(RESOLVE_TESTED, resolve_); |
| 54 int rv = OK; | 55 int rv = OK; |
| 55 switch (resolve_) { | 56 switch (resolve_) { |
| 56 case RESOLVE_SYNC: | 57 case RESOLVE_SYNC: |
| 57 resolve_ = RESOLVE_TESTED; | 58 resolve_ = RESOLVE_TESTED; |
| 58 break; | 59 break; |
| 59 case RESOLVE_ASYNC: | 60 case RESOLVE_ASYNC: |
| 60 EXPECT_TRUE(callback_.is_null()); | 61 EXPECT_TRUE(callback_.is_null()); |
| 61 rv = ERR_IO_PENDING; | 62 rv = ERR_IO_PENDING; |
| 62 callback_ = callback; | 63 callback_ = callback; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 EXPECT_TRUE(generate_async_); | 145 EXPECT_TRUE(generate_async_); |
| 145 EXPECT_TRUE(!callback_.is_null()); | 146 EXPECT_TRUE(!callback_.is_null()); |
| 146 if (generate_rv_ == OK) | 147 if (generate_rv_ == OK) |
| 147 *auth_token_ = "auth_token"; | 148 *auth_token_ = "auth_token"; |
| 148 auth_token_ = NULL; | 149 auth_token_ = NULL; |
| 149 CompletionCallback callback = callback_; | 150 CompletionCallback callback = callback_; |
| 150 callback_.Reset(); | 151 callback_.Reset(); |
| 151 callback.Run(generate_rv_); | 152 callback.Run(generate_rv_); |
| 152 } | 153 } |
| 153 | 154 |
| 154 HttpAuthHandlerMock::Factory::Factory() | 155 HttpAuthHandlerMock::Factory::Factory() : do_init_from_challenge_(false) { |
| 155 : do_init_from_challenge_(false) { | |
| 156 // TODO(cbentzel): Default do_init_from_challenge_ to true. | 156 // TODO(cbentzel): Default do_init_from_challenge_ to true. |
| 157 } | 157 } |
| 158 | 158 |
| 159 HttpAuthHandlerMock::Factory::~Factory() { | 159 HttpAuthHandlerMock::Factory::~Factory() { |
| 160 } | 160 } |
| 161 | 161 |
| 162 void HttpAuthHandlerMock::Factory::AddMockHandler( | 162 void HttpAuthHandlerMock::Factory::AddMockHandler(HttpAuthHandler* handler, |
| 163 HttpAuthHandler* handler, HttpAuth::Target target) { | 163 HttpAuth::Target target) { |
| 164 handlers_[target].push_back(handler); | 164 handlers_[target].push_back(handler); |
| 165 } | 165 } |
| 166 | 166 |
| 167 int HttpAuthHandlerMock::Factory::CreateAuthHandler( | 167 int HttpAuthHandlerMock::Factory::CreateAuthHandler( |
| 168 HttpAuthChallengeTokenizer* challenge, | 168 HttpAuthChallengeTokenizer* challenge, |
| 169 HttpAuth::Target target, | 169 HttpAuth::Target target, |
| 170 const GURL& origin, | 170 const GURL& origin, |
| 171 CreateReason reason, | 171 CreateReason reason, |
| 172 int nonce_count, | 172 int nonce_count, |
| 173 const BoundNetLog& net_log, | 173 const BoundNetLog& net_log, |
| 174 scoped_ptr<HttpAuthHandler>* handler) { | 174 scoped_ptr<HttpAuthHandler>* handler) { |
| 175 if (handlers_[target].empty()) | 175 if (handlers_[target].empty()) |
| 176 return ERR_UNEXPECTED; | 176 return ERR_UNEXPECTED; |
| 177 scoped_ptr<HttpAuthHandler> tmp_handler(handlers_[target][0]); | 177 scoped_ptr<HttpAuthHandler> tmp_handler(handlers_[target][0]); |
| 178 std::vector<HttpAuthHandler*>& handlers = handlers_[target].get(); | 178 std::vector<HttpAuthHandler*>& handlers = handlers_[target].get(); |
| 179 handlers.erase(handlers.begin()); | 179 handlers.erase(handlers.begin()); |
| 180 if (do_init_from_challenge_ && | 180 if (do_init_from_challenge_ && |
| 181 !tmp_handler->InitFromChallenge(challenge, target, origin, net_log)) | 181 !tmp_handler->InitFromChallenge(challenge, target, origin, net_log)) |
| 182 return ERR_INVALID_RESPONSE; | 182 return ERR_INVALID_RESPONSE; |
| 183 handler->swap(tmp_handler); | 183 handler->swap(tmp_handler); |
| 184 return OK; | 184 return OK; |
| 185 } | 185 } |
| 186 | 186 |
| 187 } // namespace net | 187 } // namespace net |
| OLD | NEW |