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_handler.h" | 10 #include "net/http/http_auth_handler.h" |
11 #include "net/http/http_auth_handler_factory.h" | 11 #include "net/http/http_auth_handler_factory.h" |
| 12 #include "net/http/http_auth_handler_mock.h" |
12 #include "net/test/spawned_test_server/spawned_test_server.h" | 13 #include "net/test/spawned_test_server/spawned_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 public: | |
29 explicit MockHttpAuthHandlerFactory(int return_code) : | |
30 return_code_(return_code) {} | |
31 ~MockHttpAuthHandlerFactory() override {} | |
32 | |
33 int CreateAuthHandler(const HttpAuthChallengeTokenizer& challenge, | |
34 HttpAuth::Target target, | |
35 const GURL& origin, | |
36 CreateReason reason, | |
37 int nonce_count, | |
38 const BoundNetLog& net_log, | |
39 scoped_ptr<HttpAuthHandler>* handler) override { | |
40 handler->reset(); | |
41 return return_code_; | |
42 } | |
43 | |
44 private: | |
45 int return_code_; | |
46 }; | |
47 | |
48 class URLRequestContextBuilderTest : public PlatformTest { | 28 class URLRequestContextBuilderTest : public PlatformTest { |
49 protected: | 29 protected: |
50 URLRequestContextBuilderTest() | 30 URLRequestContextBuilderTest() |
51 : test_server_(SpawnedTestServer::TYPE_HTTP, | 31 : test_server_(SpawnedTestServer::TYPE_HTTP, |
52 SpawnedTestServer::kLocalhost, | 32 SpawnedTestServer::kLocalhost, |
53 base::FilePath( | 33 base::FilePath( |
54 FILE_PATH_LITERAL("net/data/url_request_unittest"))) { | 34 FILE_PATH_LITERAL("net/data/url_request_unittest"))) { |
55 #if defined(OS_LINUX) || defined(OS_ANDROID) | 35 #if defined(OS_LINUX) || defined(OS_ANDROID) |
56 builder_.set_proxy_config_service(make_scoped_ptr( | 36 builder_.set_proxy_config_service(make_scoped_ptr( |
57 new ProxyConfigServiceFixed(ProxyConfig::CreateDirect()))); | 37 new ProxyConfigServiceFixed(ProxyConfig::CreateDirect()))); |
(...skipping 27 matching lines...) Expand all Loading... |
85 scoped_ptr<URLRequest> request( | 65 scoped_ptr<URLRequest> request( |
86 context->CreateRequest(test_server_.GetURL("echoheader?User-Agent"), | 66 context->CreateRequest(test_server_.GetURL("echoheader?User-Agent"), |
87 DEFAULT_PRIORITY, &delegate)); | 67 DEFAULT_PRIORITY, &delegate)); |
88 request->set_method("GET"); | 68 request->set_method("GET"); |
89 request->Start(); | 69 request->Start(); |
90 base::MessageLoop::current()->Run(); | 70 base::MessageLoop::current()->Run(); |
91 EXPECT_EQ("Bar", delegate.data_received()); | 71 EXPECT_EQ("Bar", delegate.data_received()); |
92 } | 72 } |
93 | 73 |
94 TEST_F(URLRequestContextBuilderTest, ExtraHttpAuthHandlerFactory) { | 74 TEST_F(URLRequestContextBuilderTest, ExtraHttpAuthHandlerFactory) { |
| 75 const char kExtraScheme[] = "extrascheme"; |
95 GURL gurl("www.google.com"); | 76 GURL gurl("www.google.com"); |
96 const int kBasicReturnCode = OK; | 77 scoped_ptr<HttpAuthHandlerMock::Factory> mock_auth_factory( |
97 MockHttpAuthHandlerFactory* mock_factory_basic = | 78 new HttpAuthHandlerMock::Factory()); |
98 new MockHttpAuthHandlerFactory(kBasicReturnCode); | 79 scoped_ptr<HttpAuthHandlerMock> mock_handler(new HttpAuthHandlerMock()); |
| 80 mock_handler->set_expected_auth_scheme(kExtraScheme); |
| 81 mock_auth_factory->AddMockHandler(mock_handler.Pass(), |
| 82 HttpAuthHandlerCreateReason::CHALLENGE); |
| 83 |
99 scoped_ptr<HttpAuthHandler> handler; | 84 scoped_ptr<HttpAuthHandler> handler; |
100 builder_.add_http_auth_handler_factory("extrascheme", mock_factory_basic); | 85 builder_.add_http_auth_handler_factory(kExtraScheme, |
| 86 mock_auth_factory.release()); |
101 scoped_ptr<URLRequestContext> context(builder_.Build()); | 87 scoped_ptr<URLRequestContext> context(builder_.Build()); |
102 // Verify that a handler is returned for and added scheme. | 88 // Verify that a handler is returned for a known scheme. |
103 EXPECT_EQ(kBasicReturnCode, | 89 scoped_ptr<HttpAuthHandler> auth_handler = |
104 context->http_auth_handler_factory()->CreateAuthHandlerFromString( | 90 context->http_auth_handler_factory()->CreateAuthHandlerForScheme( |
105 "ExtraScheme", | 91 kExtraScheme); |
106 HttpAuth::AUTH_SERVER, | 92 EXPECT_TRUE(auth_handler); |
107 gurl, | 93 |
108 BoundNetLog(), | 94 // Verify that a handler is not returned for an unknown scheme. |
109 &handler)); | 95 auth_handler = |
110 // Verify that a handler isn't returned for a bogus scheme. | 96 context->http_auth_handler_factory()->CreateAuthHandlerForScheme("bogus"); |
111 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, | 97 EXPECT_FALSE(auth_handler); |
112 context->http_auth_handler_factory()->CreateAuthHandlerFromString( | |
113 "Bogus", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); | |
114 } | 98 } |
115 | 99 |
116 } // namespace | 100 } // namespace |
117 | 101 |
118 } // namespace net | 102 } // namespace net |
OLD | NEW |