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_controller.h" | 5 #include "net/http/http_auth_controller.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
9 #include "net/base/test_completion_callback.h" | 9 #include "net/base/test_completion_callback.h" |
10 #include "net/http/http_auth_cache.h" | 10 #include "net/http/http_auth_cache.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 scoped_refptr<HttpResponseHeaders> headers(HeadersFromString( | 59 scoped_refptr<HttpResponseHeaders> headers(HeadersFromString( |
60 "HTTP/1.1 407\r\n" | 60 "HTTP/1.1 407\r\n" |
61 "Proxy-Authenticate: MOCK foo\r\n" | 61 "Proxy-Authenticate: MOCK foo\r\n" |
62 "\r\n")); | 62 "\r\n")); |
63 | 63 |
64 HttpAuthHandlerMock::Factory auth_handler_factory; | 64 HttpAuthHandlerMock::Factory auth_handler_factory; |
65 scoped_ptr<HttpAuthHandlerMock> auth_handler(new HttpAuthHandlerMock()); | 65 scoped_ptr<HttpAuthHandlerMock> auth_handler(new HttpAuthHandlerMock()); |
66 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), | 66 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), |
67 handler_rv); | 67 handler_rv); |
68 auth_handler_factory.AddMockHandler(auth_handler.Pass(), | 68 auth_handler_factory.AddMockHandler(auth_handler.Pass(), |
69 HttpAuthHandlerFactory::CREATE_CHALLENGE, | 69 HttpAuthHandlerCreateReason::CHALLENGE); |
70 HttpAuth::AUTH_PROXY); | |
71 | 70 |
72 scoped_refptr<HttpAuthController> controller( | 71 scoped_refptr<HttpAuthController> controller( |
73 new HttpAuthController(HttpAuth::AUTH_PROXY, | 72 new HttpAuthController(HttpAuth::AUTH_PROXY, |
74 GURL("http://example.com"), | 73 GURL("http://example.com"), |
75 &dummy_auth_cache, &auth_handler_factory)); | 74 &dummy_auth_cache, &auth_handler_factory)); |
76 ASSERT_EQ(OK, | 75 ASSERT_EQ(OK, |
77 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 76 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
78 ASSERT_TRUE(controller->HaveAuthHandler()); | 77 ASSERT_TRUE(controller->HaveAuthHandler()); |
79 controller->ResetAuth( | 78 controller->ResetAuth( |
80 AuthCredentials(base::ASCIIToUTF16("a"), base::ASCIIToUTF16("b"))); | 79 AuthCredentials(base::ASCIIToUTF16("a"), base::ASCIIToUTF16("b"))); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 135 |
137 HttpAuthHandlerMock::Factory auth_handler_factory; | 136 HttpAuthHandlerMock::Factory auth_handler_factory; |
138 | 137 |
139 // Handler for the first attempt at authentication. "Ernie" handler accepts | 138 // Handler for the first attempt at authentication. "Ernie" handler accepts |
140 // the default identity and successfully constructs a token. Handler for the | 139 // the default identity and successfully constructs a token. Handler for the |
141 scoped_ptr<HttpAuthHandlerMock> auth_handler(new HttpAuthHandlerMock()); | 140 scoped_ptr<HttpAuthHandlerMock> auth_handler(new HttpAuthHandlerMock()); |
142 auth_handler->set_allows_default_credentials(true); | 141 auth_handler->set_allows_default_credentials(true); |
143 auth_handler->set_allows_explicit_credentials(false); | 142 auth_handler->set_allows_explicit_credentials(false); |
144 auth_handler->set_expected_auth_scheme("ernie"); | 143 auth_handler->set_expected_auth_scheme("ernie"); |
145 auth_handler_factory.AddMockHandler(auth_handler.Pass(), | 144 auth_handler_factory.AddMockHandler(auth_handler.Pass(), |
146 HttpAuthHandlerFactory::CREATE_CHALLENGE, | 145 HttpAuthHandlerCreateReason::CHALLENGE); |
147 HttpAuth::AUTH_SERVER); | |
148 | 146 |
149 scoped_refptr<HttpAuthController> controller( | 147 scoped_refptr<HttpAuthController> controller( |
150 new HttpAuthController(HttpAuth::AUTH_SERVER, | 148 new HttpAuthController(HttpAuth::AUTH_SERVER, |
151 GURL("http://example.com"), | 149 GURL("http://example.com"), |
152 &dummy_auth_cache, &auth_handler_factory)); | 150 &dummy_auth_cache, &auth_handler_factory)); |
153 ASSERT_EQ(OK, | 151 ASSERT_EQ(OK, |
154 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 152 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
155 ASSERT_TRUE(controller->HaveAuthHandler()); | 153 ASSERT_TRUE(controller->HaveAuthHandler()); |
156 controller->ResetAuth(AuthCredentials()); | 154 controller->ResetAuth(AuthCredentials()); |
157 EXPECT_TRUE(controller->HaveAuth()); | 155 EXPECT_TRUE(controller->HaveAuth()); |
158 EXPECT_FALSE(auth_handler_factory.HaveAuthHandlers(HttpAuth::AUTH_SERVER)); | 156 EXPECT_FALSE(auth_handler_factory.HaveAuthHandlers()); |
159 | 157 |
160 // Should only succeed if we are using the "Ernie" MockHandler. | 158 // Should only succeed if we are using the "Ernie" MockHandler. |
161 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( | 159 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( |
162 &request, CompletionCallback(), dummy_log)); | 160 &request, CompletionCallback(), dummy_log)); |
163 controller->AddAuthorizationHeader(&request_headers); | 161 controller->AddAuthorizationHeader(&request_headers); |
164 | 162 |
165 // Handlers for the second attempt. Neither should be used to generate a | 163 // Handlers for the second attempt. Neither should be used to generate a |
166 // token. Instead the controller should realize that there are no viable | 164 // token. Instead the controller should realize that there are no viable |
167 // identities to use with the "Ernie" handler and fail. | 165 // identities to use with the "Ernie" handler and fail. |
168 auth_handler.reset(new HttpAuthHandlerMock()); | 166 auth_handler.reset(new HttpAuthHandlerMock()); |
169 auth_handler->set_allows_default_credentials(true); | 167 auth_handler->set_allows_default_credentials(true); |
170 auth_handler->set_allows_explicit_credentials(false); | 168 auth_handler->set_allows_explicit_credentials(false); |
171 auth_handler->set_expected_auth_scheme("ernie"); | 169 auth_handler->set_expected_auth_scheme("ernie"); |
172 auth_handler_factory.AddMockHandler(auth_handler.Pass(), | 170 auth_handler_factory.AddMockHandler(auth_handler.Pass(), |
173 HttpAuthHandlerFactory::CREATE_CHALLENGE, | 171 HttpAuthHandlerCreateReason::CHALLENGE); |
174 HttpAuth::AUTH_SERVER); | |
175 | 172 |
176 // Fallback handlers for the second attempt. The "Ernie" handler should be | 173 // Fallback handlers for the second attempt. The "Ernie" handler should be |
177 // discarded due to the disabled scheme, and the "Bert" handler should | 174 // discarded due to the disabled scheme, and the "Bert" handler should |
178 // successfully be used to generate a token. | 175 // successfully be used to generate a token. |
179 auth_handler.reset(new HttpAuthHandlerMock()); | 176 auth_handler.reset(new HttpAuthHandlerMock()); |
180 auth_handler->set_allows_default_credentials(false); | 177 auth_handler->set_allows_default_credentials(false); |
181 auth_handler->set_allows_explicit_credentials(true); | 178 auth_handler->set_allows_explicit_credentials(true); |
182 auth_handler->set_expected_auth_scheme("bert"); | 179 auth_handler->set_expected_auth_scheme("bert"); |
183 auth_handler_factory.AddMockHandler(auth_handler.Pass(), | 180 auth_handler_factory.AddMockHandler(auth_handler.Pass(), |
184 HttpAuthHandlerFactory::CREATE_CHALLENGE, | 181 HttpAuthHandlerCreateReason::CHALLENGE); |
185 HttpAuth::AUTH_SERVER); | |
186 | 182 |
187 // Once a token is generated, simulate the receipt of a server response | 183 // Once a token is generated, simulate the receipt of a server response |
188 // indicating that the authentication attempt was rejected. | 184 // indicating that the authentication attempt was rejected. |
189 ASSERT_EQ(OK, | 185 ASSERT_EQ(OK, |
190 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 186 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
191 ASSERT_TRUE(controller->HaveAuthHandler()); | 187 ASSERT_TRUE(controller->HaveAuthHandler()); |
192 controller->ResetAuth(AuthCredentials(base::ASCIIToUTF16("Hello"), | 188 controller->ResetAuth(AuthCredentials(base::ASCIIToUTF16("Hello"), |
193 base::string16())); | 189 base::string16())); |
194 EXPECT_TRUE(controller->HaveAuth()); | 190 EXPECT_TRUE(controller->HaveAuth()); |
195 EXPECT_TRUE(controller->IsAuthSchemeDisabled("ernie")); | 191 EXPECT_TRUE(controller->IsAuthSchemeDisabled("ernie")); |
196 EXPECT_FALSE(controller->IsAuthSchemeDisabled("bert")); | 192 EXPECT_FALSE(controller->IsAuthSchemeDisabled("bert")); |
197 | 193 |
198 // Should only succeed if we are using the "Bert" MockHandler. | 194 // Should only succeed if we are using the "Bert" MockHandler. |
199 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( | 195 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( |
200 &request, CompletionCallback(), dummy_log)); | 196 &request, CompletionCallback(), dummy_log)); |
201 } | 197 } |
202 | 198 |
203 } // namespace net | 199 } // namespace net |
OLD | NEW |