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