| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/url_request/url_request_context_builder.h" | 5 #include "net/url_request/url_request_context_builder.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "net/base/request_priority.h" | 9 #include "net/base/request_priority.h" |
| 10 #include "net/http/http_auth_challenge_tokenizer.h" |
| 10 #include "net/http/http_auth_handler.h" | 11 #include "net/http/http_auth_handler.h" |
| 11 #include "net/http/http_auth_handler_factory.h" | 12 #include "net/http/http_auth_handler_factory.h" |
| 12 #include "net/test/embedded_test_server/embedded_test_server.h" | 13 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 13 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
| 14 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
| 17 | 18 |
| 18 #if defined(OS_LINUX) || defined(OS_ANDROID) | 19 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 19 #include "net/proxy/proxy_config.h" | 20 #include "net/proxy/proxy_config.h" |
| 20 #include "net/proxy/proxy_config_service_fixed.h" | 21 #include "net/proxy/proxy_config_service_fixed.h" |
| 21 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 22 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 22 | 23 |
| 23 namespace net { | 24 namespace net { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 class MockHttpAuthHandlerFactory : public HttpAuthHandlerFactory { | 28 class MockHttpAuthHandlerFactory : public HttpAuthHandlerFactory { |
| 28 public: | 29 public: |
| 29 explicit MockHttpAuthHandlerFactory(int return_code) : | 30 MockHttpAuthHandlerFactory(std::string supported_scheme, int return_code) |
| 30 return_code_(return_code) {} | 31 : return_code_(return_code), supported_scheme_(supported_scheme) {} |
| 31 ~MockHttpAuthHandlerFactory() override {} | 32 ~MockHttpAuthHandlerFactory() override {} |
| 32 | 33 |
| 33 int CreateAuthHandler(HttpAuthChallengeTokenizer* challenge, | 34 int CreateAuthHandler(HttpAuthChallengeTokenizer* challenge, |
| 34 HttpAuth::Target target, | 35 HttpAuth::Target target, |
| 35 const GURL& origin, | 36 const GURL& origin, |
| 36 CreateReason reason, | 37 CreateReason reason, |
| 37 int nonce_count, | 38 int nonce_count, |
| 38 const BoundNetLog& net_log, | 39 const BoundNetLog& net_log, |
| 39 scoped_ptr<HttpAuthHandler>* handler) override { | 40 scoped_ptr<HttpAuthHandler>* handler) override { |
| 40 handler->reset(); | 41 handler->reset(); |
| 41 return return_code_; | 42 |
| 43 return challenge->scheme() == supported_scheme_ |
| 44 ? return_code_ |
| 45 : ERR_UNSUPPORTED_AUTH_SCHEME; |
| 42 } | 46 } |
| 43 | 47 |
| 44 private: | 48 private: |
| 45 int return_code_; | 49 int return_code_; |
| 50 std::string supported_scheme_; |
| 46 }; | 51 }; |
| 47 | 52 |
| 48 class URLRequestContextBuilderTest : public PlatformTest { | 53 class URLRequestContextBuilderTest : public PlatformTest { |
| 49 protected: | 54 protected: |
| 50 URLRequestContextBuilderTest() { | 55 URLRequestContextBuilderTest() { |
| 51 test_server_.AddDefaultHandlers( | 56 test_server_.AddDefaultHandlers( |
| 52 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 57 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 53 #if defined(OS_LINUX) || defined(OS_ANDROID) | 58 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 54 builder_.set_proxy_config_service(make_scoped_ptr( | 59 builder_.set_proxy_config_service(make_scoped_ptr( |
| 55 new ProxyConfigServiceFixed(ProxyConfig::CreateDirect()))); | 60 new ProxyConfigServiceFixed(ProxyConfig::CreateDirect()))); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 82 TestDelegate delegate; | 87 TestDelegate delegate; |
| 83 scoped_ptr<URLRequest> request( | 88 scoped_ptr<URLRequest> request( |
| 84 context->CreateRequest(test_server_.GetURL("/echoheader?User-Agent"), | 89 context->CreateRequest(test_server_.GetURL("/echoheader?User-Agent"), |
| 85 DEFAULT_PRIORITY, &delegate)); | 90 DEFAULT_PRIORITY, &delegate)); |
| 86 request->set_method("GET"); | 91 request->set_method("GET"); |
| 87 request->Start(); | 92 request->Start(); |
| 88 base::MessageLoop::current()->Run(); | 93 base::MessageLoop::current()->Run(); |
| 89 EXPECT_EQ("Bar", delegate.data_received()); | 94 EXPECT_EQ("Bar", delegate.data_received()); |
| 90 } | 95 } |
| 91 | 96 |
| 92 TEST_F(URLRequestContextBuilderTest, ExtraHttpAuthHandlerFactory) { | 97 TEST_F(URLRequestContextBuilderTest, DefaultHttpAuthHandlerFactory) { |
| 98 GURL gurl("www.google.com"); |
| 99 scoped_ptr<HttpAuthHandler> handler; |
| 100 scoped_ptr<URLRequestContext> context(builder_.Build()); |
| 101 |
| 102 // Verify that the default basic handler is present |
| 103 EXPECT_EQ(OK, |
| 104 context->http_auth_handler_factory()->CreateAuthHandlerFromString( |
| 105 "basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); |
| 106 } |
| 107 |
| 108 TEST_F(URLRequestContextBuilderTest, CustomHttpAuthHandlerFactory) { |
| 93 GURL gurl("www.google.com"); | 109 GURL gurl("www.google.com"); |
| 94 const int kBasicReturnCode = OK; | 110 const int kBasicReturnCode = OK; |
| 95 MockHttpAuthHandlerFactory* mock_factory_basic = | |
| 96 new MockHttpAuthHandlerFactory(kBasicReturnCode); | |
| 97 scoped_ptr<HttpAuthHandler> handler; | 111 scoped_ptr<HttpAuthHandler> handler; |
| 98 builder_.add_http_auth_handler_factory("ExtraScheme", mock_factory_basic); | 112 builder_.SetHttpAuthHandlerFactory(make_scoped_ptr( |
| 113 new MockHttpAuthHandlerFactory("ExtraScheme", kBasicReturnCode))); |
| 99 scoped_ptr<URLRequestContext> context(builder_.Build()); | 114 scoped_ptr<URLRequestContext> context(builder_.Build()); |
| 100 // Verify that a handler is returned for and added scheme. | 115 // Verify that a handler is returned for a custom scheme. |
| 101 EXPECT_EQ(kBasicReturnCode, | 116 EXPECT_EQ(kBasicReturnCode, |
| 102 context->http_auth_handler_factory()->CreateAuthHandlerFromString( | 117 context->http_auth_handler_factory()->CreateAuthHandlerFromString( |
| 103 "ExtraScheme", | 118 "ExtraScheme", |
| 104 HttpAuth::AUTH_SERVER, | 119 HttpAuth::AUTH_SERVER, |
| 105 gurl, | 120 gurl, |
| 106 BoundNetLog(), | 121 BoundNetLog(), |
| 107 &handler)); | 122 &handler)); |
| 123 |
| 124 // Verify that the default basic handler isn't present |
| 125 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, |
| 126 context->http_auth_handler_factory()->CreateAuthHandlerFromString( |
| 127 "basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); |
| 128 |
| 108 // Verify that a handler isn't returned for a bogus scheme. | 129 // Verify that a handler isn't returned for a bogus scheme. |
| 109 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, | 130 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, |
| 110 context->http_auth_handler_factory()->CreateAuthHandlerFromString( | 131 context->http_auth_handler_factory()->CreateAuthHandlerFromString( |
| 111 "Bogus", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); | 132 "Bogus", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); |
| 112 } | 133 } |
| 113 | 134 |
| 114 } // namespace | 135 } // namespace |
| 115 | 136 |
| 116 } // namespace net | 137 } // namespace net |
| OLD | NEW |