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 <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/dns/mock_host_resolver.h" | 12 #include "net/dns/mock_host_resolver.h" |
13 #include "net/http/http_auth.h" | 13 #include "net/http/http_auth.h" |
14 #include "net/http/http_auth_challenge_tokenizer.h" | 14 #include "net/http/http_auth_challenge_tokenizer.h" |
15 #include "net/http/http_auth_filter.h" | 15 #include "net/http/http_auth_filter.h" |
16 #include "net/http/http_auth_handler.h" | 16 #include "net/http/http_auth_handler.h" |
17 #include "net/http/http_auth_handler_factory.h" | 17 #include "net/http/http_auth_handler_factory.h" |
18 #include "net/http/http_auth_handler_mock.h" | 18 #include "net/http/http_auth_handler_mock.h" |
19 #include "net/http/http_auth_scheme.h" | 19 #include "net/http/http_auth_scheme.h" |
20 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
| 21 #include "net/http/http_response_info.h" |
21 #include "net/http/http_util.h" | 22 #include "net/http/http_util.h" |
22 #include "net/http/mock_allow_http_auth_preferences.h" | 23 #include "net/http/mock_allow_http_auth_preferences.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
25 namespace net { | 26 namespace net { |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 HttpAuthHandlerMock* CreateMockHandler(bool connection_based) { | 30 HttpAuthHandlerMock* CreateMockHandler(bool connection_based) { |
30 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); | 31 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); |
31 auth_handler->set_connection_based(connection_based); | 32 auth_handler->set_connection_based(connection_based); |
32 std::string challenge_text = "Basic"; | 33 std::string challenge_text = "Basic"; |
33 HttpAuthChallengeTokenizer challenge(challenge_text.begin(), | 34 HttpAuthChallengeTokenizer challenge(challenge_text.begin(), |
34 challenge_text.end()); | 35 challenge_text.end()); |
35 GURL origin("www.example.com"); | 36 GURL origin("www.example.com"); |
36 EXPECT_TRUE(auth_handler->InitFromChallenge(&challenge, | 37 HttpResponseInfo empty_response_info; |
37 HttpAuth::AUTH_SERVER, | 38 EXPECT_TRUE(auth_handler->InitFromChallenge(&challenge, HttpAuth::AUTH_SERVER, |
38 origin, | 39 empty_response_info, origin, |
39 BoundNetLog())); | 40 BoundNetLog())); |
40 return auth_handler; | 41 return auth_handler; |
41 } | 42 } |
42 | 43 |
43 HttpResponseHeaders* HeadersFromResponseText(const std::string& response) { | 44 HttpResponseHeaders* HeadersFromResponseText(const std::string& response) { |
44 return new HttpResponseHeaders( | 45 return new HttpResponseHeaders( |
45 HttpUtil::AssembleRawHeaders(response.c_str(), response.length())); | 46 HttpUtil::AssembleRawHeaders(response.c_str(), response.length())); |
46 } | 47 } |
47 | 48 |
48 HttpAuth::AuthorizationResult HandleChallengeResponse( | 49 HttpAuth::AuthorizationResult HandleChallengeResponse( |
49 bool connection_based, | 50 bool connection_based, |
50 const std::string& headers_text, | 51 const std::string& headers_text, |
51 std::string* challenge_used) { | 52 std::string* challenge_used) { |
52 scoped_ptr<HttpAuthHandlerMock> mock_handler( | 53 scoped_ptr<HttpAuthHandlerMock> mock_handler( |
53 CreateMockHandler(connection_based)); | 54 CreateMockHandler(connection_based)); |
54 std::set<HttpAuth::Scheme> disabled_schemes; | 55 std::set<HttpAuth::Scheme> disabled_schemes; |
55 scoped_refptr<HttpResponseHeaders> headers( | 56 HttpResponseInfo response_info; |
56 HeadersFromResponseText(headers_text)); | 57 response_info.headers = HeadersFromResponseText(headers_text); |
57 return HttpAuth::HandleChallengeResponse( | 58 return HttpAuth::HandleChallengeResponse(mock_handler.get(), response_info, |
58 mock_handler.get(), | 59 HttpAuth::AUTH_SERVER, |
59 headers.get(), | 60 disabled_schemes, challenge_used); |
60 HttpAuth::AUTH_SERVER, | |
61 disabled_schemes, | |
62 challenge_used); | |
63 } | 61 } |
64 | 62 |
65 } // namespace | 63 } // namespace |
66 | 64 |
67 TEST(HttpAuthTest, ChooseBestChallenge) { | 65 TEST(HttpAuthTest, ChooseBestChallenge) { |
68 static const struct { | 66 static const struct { |
69 const char* headers; | 67 const char* headers; |
70 HttpAuth::Scheme challenge_scheme; | 68 HttpAuth::Scheme challenge_scheme; |
71 const char* challenge_realm; | 69 const char* challenge_realm; |
72 } tests[] = { | 70 } tests[] = { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 scoped_ptr<HostResolver> host_resolver(new MockHostResolver()); | 124 scoped_ptr<HostResolver> host_resolver(new MockHostResolver()); |
127 scoped_ptr<HttpAuthHandlerRegistryFactory> http_auth_handler_factory( | 125 scoped_ptr<HttpAuthHandlerRegistryFactory> http_auth_handler_factory( |
128 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); | 126 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); |
129 http_auth_handler_factory->SetHttpAuthPreferences(kNegotiateAuthScheme, | 127 http_auth_handler_factory->SetHttpAuthPreferences(kNegotiateAuthScheme, |
130 &http_auth_preferences); | 128 &http_auth_preferences); |
131 | 129 |
132 for (size_t i = 0; i < arraysize(tests); ++i) { | 130 for (size_t i = 0; i < arraysize(tests); ++i) { |
133 // Make a HttpResponseHeaders object. | 131 // Make a HttpResponseHeaders object. |
134 std::string headers_with_status_line("HTTP/1.1 401 Unauthorized\n"); | 132 std::string headers_with_status_line("HTTP/1.1 401 Unauthorized\n"); |
135 headers_with_status_line += tests[i].headers; | 133 headers_with_status_line += tests[i].headers; |
136 scoped_refptr<HttpResponseHeaders> headers( | 134 HttpResponseInfo response_info; |
137 HeadersFromResponseText(headers_with_status_line)); | 135 response_info.headers = HeadersFromResponseText(headers_with_status_line); |
138 | 136 |
139 scoped_ptr<HttpAuthHandler> handler; | 137 scoped_ptr<HttpAuthHandler> handler; |
140 HttpAuth::ChooseBestChallenge(http_auth_handler_factory.get(), | 138 HttpAuth::ChooseBestChallenge(http_auth_handler_factory.get(), |
141 headers.get(), | 139 response_info, HttpAuth::AUTH_SERVER, origin, |
142 HttpAuth::AUTH_SERVER, | 140 disabled_schemes, BoundNetLog(), &handler); |
143 origin, | |
144 disabled_schemes, | |
145 BoundNetLog(), | |
146 &handler); | |
147 | 141 |
148 if (handler.get()) { | 142 if (handler.get()) { |
149 EXPECT_EQ(tests[i].challenge_scheme, handler->auth_scheme()); | 143 EXPECT_EQ(tests[i].challenge_scheme, handler->auth_scheme()); |
150 EXPECT_STREQ(tests[i].challenge_realm, handler->realm().c_str()); | 144 EXPECT_STREQ(tests[i].challenge_realm, handler->realm().c_str()); |
151 } else { | 145 } else { |
152 EXPECT_EQ(HttpAuth::AUTH_SCHEME_MAX, tests[i].challenge_scheme); | 146 EXPECT_EQ(HttpAuth::AUTH_SCHEME_MAX, tests[i].challenge_scheme); |
153 EXPECT_STREQ("", tests[i].challenge_realm); | 147 EXPECT_STREQ("", tests[i].challenge_realm); |
154 } | 148 } |
155 } | 149 } |
156 } | 150 } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 std::string name; | 254 std::string name; |
261 | 255 |
262 name = HttpAuth::GetAuthorizationHeaderName(HttpAuth::AUTH_SERVER); | 256 name = HttpAuth::GetAuthorizationHeaderName(HttpAuth::AUTH_SERVER); |
263 EXPECT_STREQ("Authorization", name.c_str()); | 257 EXPECT_STREQ("Authorization", name.c_str()); |
264 | 258 |
265 name = HttpAuth::GetAuthorizationHeaderName(HttpAuth::AUTH_PROXY); | 259 name = HttpAuth::GetAuthorizationHeaderName(HttpAuth::AUTH_PROXY); |
266 EXPECT_STREQ("Proxy-Authorization", name.c_str()); | 260 EXPECT_STREQ("Proxy-Authorization", name.c_str()); |
267 } | 261 } |
268 | 262 |
269 } // namespace net | 263 } // namespace net |
OLD | NEW |