Index: net/http/http_auth_handler_factory_unittest.cc |
diff --git a/net/http/http_auth_handler_factory_unittest.cc b/net/http/http_auth_handler_factory_unittest.cc |
index 6a6d282b15cc2e40f0ce038f6ca1e03c65556e67..76eb3a3c026014137b0f576bfcccbefe5aba8380 100644 |
--- a/net/http/http_auth_handler_factory_unittest.cc |
+++ b/net/http/http_auth_handler_factory_unittest.cc |
@@ -3,10 +3,13 @@ |
// found in the LICENSE file. |
#include "base/memory/scoped_ptr.h" |
+#include "base/strings/string_util.h" |
#include "net/base/net_errors.h" |
#include "net/dns/mock_host_resolver.h" |
+#include "net/http/http_auth_challenge_tokenizer.h" |
#include "net/http/http_auth_handler.h" |
#include "net/http/http_auth_handler_factory.h" |
+#include "net/http/http_auth_handler_mock.h" |
#include "net/http/mock_allow_url_security_manager.h" |
#include "net/http/url_security_manager.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -17,23 +20,27 @@ namespace { |
class MockHttpAuthHandlerFactory : public HttpAuthHandlerFactory { |
public: |
- explicit MockHttpAuthHandlerFactory(int return_code) : |
- return_code_(return_code) {} |
+ explicit MockHttpAuthHandlerFactory(const char* scheme) |
+ : scheme_(base::ToLowerASCII(scheme)) {} |
~MockHttpAuthHandlerFactory() override {} |
- int CreateAuthHandler(const HttpAuthChallengeTokenizer& challenge, |
- HttpAuth::Target target, |
- const GURL& origin, |
- CreateReason reason, |
- int nonce_count, |
- const BoundNetLog& net_log, |
- scoped_ptr<HttpAuthHandler>* handler) override { |
- handler->reset(); |
- return return_code_; |
+ scoped_ptr<HttpAuthHandler> CreateAuthHandlerForScheme( |
+ const std::string& scheme) override { |
+ EXPECT_EQ(scheme, scheme_) << scheme << " vs. " << scheme_; |
+ return make_scoped_ptr(new HttpAuthHandlerMock()); |
+ } |
+ scoped_ptr<HttpAuthHandler> CreateAndInitPreemptiveAuthHandler( |
+ HttpAuthCache::Entry* cache_entry, |
+ const HttpAuthChallengeTokenizer& tokenizer, |
+ HttpAuth::Target target, |
+ const BoundNetLog& net_log) override { |
+ EXPECT_EQ(cache_entry->scheme(), scheme_) << cache_entry->scheme() |
+ << " vs. " << scheme_; |
+ return make_scoped_ptr(new HttpAuthHandlerMock()); |
} |
private: |
- int return_code_; |
+ std::string scheme_; |
}; |
} // namespace |
@@ -41,59 +48,40 @@ class MockHttpAuthHandlerFactory : public HttpAuthHandlerFactory { |
TEST(HttpAuthHandlerFactoryTest, RegistryFactory) { |
HttpAuthHandlerRegistryFactory registry_factory; |
GURL gurl("www.google.com"); |
- const int kBasicReturnCode = ERR_INVALID_SPDY_STREAM; |
- MockHttpAuthHandlerFactory* mock_factory_basic = |
- new MockHttpAuthHandlerFactory(kBasicReturnCode); |
- |
- const int kDigestReturnCode = ERR_PAC_SCRIPT_FAILED; |
- MockHttpAuthHandlerFactory* mock_factory_digest = |
- new MockHttpAuthHandlerFactory(kDigestReturnCode); |
- |
- const int kDigestReturnCodeReplace = ERR_SYN_REPLY_NOT_RECEIVED; |
- MockHttpAuthHandlerFactory* mock_factory_digest_replace = |
- new MockHttpAuthHandlerFactory(kDigestReturnCodeReplace); |
scoped_ptr<HttpAuthHandler> handler; |
// No schemes should be supported in the beginning. |
- EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, |
- registry_factory.CreateAuthHandlerFromString( |
- "Basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); |
+ handler = registry_factory.CreateAuthHandlerForScheme("basic"); |
+ EXPECT_FALSE(handler); |
// Test what happens with a single scheme. |
- registry_factory.RegisterSchemeFactory("basic", mock_factory_basic); |
- EXPECT_EQ(kBasicReturnCode, |
- registry_factory.CreateAuthHandlerFromString( |
- "Basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); |
- EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, |
- registry_factory.CreateAuthHandlerFromString( |
- "Digest", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), |
- &handler)); |
+ registry_factory.RegisterSchemeFactory( |
+ "basic", new MockHttpAuthHandlerFactory("basic")); |
+ handler = registry_factory.CreateAuthHandlerForScheme("basic"); |
+ EXPECT_TRUE(handler); |
+ handler = registry_factory.CreateAuthHandlerForScheme("digest"); |
+ EXPECT_FALSE(handler); |
// Test multiple schemes |
- registry_factory.RegisterSchemeFactory("digest", mock_factory_digest); |
- EXPECT_EQ(kBasicReturnCode, |
- registry_factory.CreateAuthHandlerFromString( |
- "Basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); |
- EXPECT_EQ(kDigestReturnCode, |
- registry_factory.CreateAuthHandlerFromString( |
- "Digest", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), |
- &handler)); |
- |
- // Test case-insensitivity |
- EXPECT_EQ(kBasicReturnCode, |
- registry_factory.CreateAuthHandlerFromString( |
- "basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); |
- |
- // Test replacement of existing auth scheme |
- registry_factory.RegisterSchemeFactory("digest", mock_factory_digest_replace); |
- EXPECT_EQ(kBasicReturnCode, |
- registry_factory.CreateAuthHandlerFromString( |
- "Basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); |
- EXPECT_EQ(kDigestReturnCodeReplace, |
- registry_factory.CreateAuthHandlerFromString( |
- "Digest", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), |
- &handler)); |
+ registry_factory.RegisterSchemeFactory( |
+ "digest", new MockHttpAuthHandlerFactory("digest")); |
+ handler = registry_factory.CreateAuthHandlerForScheme("basic"); |
+ EXPECT_TRUE(handler); |
+ handler = registry_factory.CreateAuthHandlerForScheme("digest"); |
+ EXPECT_TRUE(handler); |
+ handler = registry_factory.CreateAuthHandlerForScheme("bogus"); |
+ EXPECT_FALSE(handler); |
+ |
+ // Test replacement of existing schemes. |
+ registry_factory.RegisterSchemeFactory("digest", |
+ new HttpAuthHandlerMock::Factory()); |
+ handler = registry_factory.CreateAuthHandlerForScheme("digest"); |
+ EXPECT_FALSE(handler); |
+ registry_factory.RegisterSchemeFactory( |
+ "digest", new MockHttpAuthHandlerFactory("digest")); |
+ handler = registry_factory.CreateAuthHandlerForScheme("digest"); |
+ EXPECT_TRUE(handler); |
} |
TEST(HttpAuthHandlerFactoryTest, DefaultFactory) { |
@@ -106,13 +94,14 @@ TEST(HttpAuthHandlerFactoryTest, DefaultFactory) { |
GURL server_origin("http://www.example.com"); |
GURL proxy_origin("http://cache.example.com:3128"); |
{ |
- scoped_ptr<HttpAuthHandler> handler; |
- int rv = http_auth_handler_factory->CreateAuthHandlerFromString( |
- "Basic realm=\"FooBar\"", |
- HttpAuth::AUTH_SERVER, |
- server_origin, |
- BoundNetLog(), |
- &handler); |
+ std::string challenge = "Basic realm=\"FooBar\""; |
+ HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
+ scoped_ptr<HttpAuthHandler> handler = |
+ http_auth_handler_factory->CreateAuthHandlerForScheme( |
+ tokenizer.NormalizedScheme()); |
+ ASSERT_TRUE(handler); |
+ int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, |
+ server_origin, BoundNetLog()); |
EXPECT_EQ(OK, rv); |
ASSERT_FALSE(handler.get() == NULL); |
EXPECT_EQ("basic", handler->auth_scheme()); |
@@ -120,24 +109,19 @@ TEST(HttpAuthHandlerFactoryTest, DefaultFactory) { |
EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); |
} |
{ |
- scoped_ptr<HttpAuthHandler> handler; |
- int rv = http_auth_handler_factory->CreateAuthHandlerFromString( |
- "UNSUPPORTED realm=\"FooBar\"", |
- HttpAuth::AUTH_SERVER, |
- server_origin, |
- BoundNetLog(), |
- &handler); |
- EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, rv); |
- EXPECT_TRUE(handler.get() == NULL); |
+ scoped_ptr<HttpAuthHandler> handler = |
+ http_auth_handler_factory->CreateAuthHandlerForScheme("UNSUPPORTED"); |
+ EXPECT_FALSE(handler); |
} |
{ |
- scoped_ptr<HttpAuthHandler> handler; |
- int rv = http_auth_handler_factory->CreateAuthHandlerFromString( |
- "Digest realm=\"FooBar\", nonce=\"xyz\"", |
- HttpAuth::AUTH_PROXY, |
- proxy_origin, |
- BoundNetLog(), |
- &handler); |
+ std::string challenge = "Digest realm=\"FooBar\", nonce=\"xyz\""; |
+ HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
+ scoped_ptr<HttpAuthHandler> handler = |
+ http_auth_handler_factory->CreateAuthHandlerForScheme( |
+ tokenizer.NormalizedScheme()); |
+ ASSERT_TRUE(handler); |
+ int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_PROXY, |
+ proxy_origin, BoundNetLog()); |
EXPECT_EQ(OK, rv); |
ASSERT_FALSE(handler.get() == NULL); |
EXPECT_EQ("digest", handler->auth_scheme()); |
@@ -145,13 +129,14 @@ TEST(HttpAuthHandlerFactoryTest, DefaultFactory) { |
EXPECT_EQ(HttpAuth::AUTH_PROXY, handler->target()); |
} |
{ |
- scoped_ptr<HttpAuthHandler> handler; |
- int rv = http_auth_handler_factory->CreateAuthHandlerFromString( |
- "NTLM", |
- HttpAuth::AUTH_SERVER, |
- server_origin, |
- BoundNetLog(), |
- &handler); |
+ std::string challenge = "NTLM"; |
+ HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
+ scoped_ptr<HttpAuthHandler> handler = |
+ http_auth_handler_factory->CreateAuthHandlerForScheme( |
+ tokenizer.NormalizedScheme()); |
+ ASSERT_TRUE(handler); |
+ int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, |
+ server_origin, BoundNetLog()); |
EXPECT_EQ(OK, rv); |
ASSERT_FALSE(handler.get() == NULL); |
EXPECT_EQ("ntlm", handler->auth_scheme()); |
@@ -159,13 +144,14 @@ TEST(HttpAuthHandlerFactoryTest, DefaultFactory) { |
EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); |
} |
{ |
- scoped_ptr<HttpAuthHandler> handler; |
- int rv = http_auth_handler_factory->CreateAuthHandlerFromString( |
- "Negotiate", |
- HttpAuth::AUTH_SERVER, |
- server_origin, |
- BoundNetLog(), |
- &handler); |
+ std::string challenge = "Negotiate"; |
+ HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
+ scoped_ptr<HttpAuthHandler> handler = |
+ http_auth_handler_factory->CreateAuthHandlerForScheme( |
+ tokenizer.NormalizedScheme()); |
+ ASSERT_TRUE(handler); |
+ int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, |
+ server_origin, BoundNetLog()); |
// Note the default factory doesn't support Kerberos on Android |
#if defined(USE_KERBEROS) && !defined(OS_ANDROID) |
EXPECT_EQ(OK, rv); |