Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/url_request/url_request_context_builder.h" |
| 6 | |
Torne (Richard Coles) | 03b57e0 | 2014-08-28 12:05:23 +0100 | [diff] [blame] | 7 | #include "base/memory/scoped_ptr.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 8 | #include "build/build_config.h" |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 9 | #include "net/base/request_priority.h" |
Torne (Richard Coles) | 010d83a | 2014-05-14 12:12:37 +0100 | [diff] [blame] | 10 | #include "net/http/http_auth_handler.h" |
| 11 | #include "net/http/http_auth_handler_factory.h" |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 12 | #include "net/test/spawned_test_server/spawned_test_server.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "net/url_request/url_request.h" |
| 14 | #include "net/url_request/url_request_test_util.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | #include "testing/platform_test.h" |
| 17 | |
| 18 | #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 19 | #include "net/proxy/proxy_config.h" |
| 20 | #include "net/proxy/proxy_config_service_fixed.h" |
| 21 | #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 22 | |
| 23 | namespace net { |
| 24 | |
| 25 | namespace { |
| 26 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 27 | // A subclass of SpawnedTestServer that uses a statically-configured hostname. |
| 28 | // This is to work around mysterious failures in chrome_frame_net_tests. See: |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 29 | // http://crbug.com/114369 |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 30 | class LocalHttpTestServer : public SpawnedTestServer { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 32 | explicit LocalHttpTestServer(const base::FilePath& document_root) |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 33 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
| 34 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 35 | document_root) {} |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 36 | LocalHttpTestServer() |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 37 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
| 38 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 39 | base::FilePath()) {} |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Torne (Richard Coles) | 010d83a | 2014-05-14 12:12:37 +0100 | [diff] [blame] | 42 | class MockHttpAuthHandlerFactory : public HttpAuthHandlerFactory { |
| 43 | public: |
| 44 | explicit MockHttpAuthHandlerFactory(int return_code) : |
| 45 | return_code_(return_code) {} |
| 46 | virtual ~MockHttpAuthHandlerFactory() {} |
| 47 | |
| 48 | virtual int CreateAuthHandler(HttpAuthChallengeTokenizer* challenge, |
| 49 | HttpAuth::Target target, |
| 50 | const GURL& origin, |
| 51 | CreateReason reason, |
| 52 | int nonce_count, |
| 53 | const BoundNetLog& net_log, |
| 54 | scoped_ptr<HttpAuthHandler>* handler) OVERRIDE { |
| 55 | handler->reset(); |
| 56 | return return_code_; |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | int return_code_; |
| 61 | }; |
| 62 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 63 | class URLRequestContextBuilderTest : public PlatformTest { |
| 64 | protected: |
| 65 | URLRequestContextBuilderTest() |
| 66 | : test_server_( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 67 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 68 | #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 69 | builder_.set_proxy_config_service( |
| 70 | new ProxyConfigServiceFixed(ProxyConfig::CreateDirect())); |
| 71 | #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 72 | } |
| 73 | |
| 74 | LocalHttpTestServer test_server_; |
| 75 | URLRequestContextBuilder builder_; |
| 76 | }; |
| 77 | |
| 78 | TEST_F(URLRequestContextBuilderTest, DefaultSettings) { |
| 79 | ASSERT_TRUE(test_server_.Start()); |
| 80 | |
| 81 | scoped_ptr<URLRequestContext> context(builder_.Build()); |
| 82 | TestDelegate delegate; |
Torne (Richard Coles) | 03b57e0 | 2014-08-28 12:05:23 +0100 | [diff] [blame] | 83 | scoped_ptr<URLRequest> request( |
| 84 | context->CreateRequest(test_server_.GetURL("echoheader?Foo"), |
| 85 | DEFAULT_PRIORITY, |
| 86 | &delegate, |
| 87 | context->cookie_store())); |
| 88 | request->set_method("GET"); |
| 89 | request->SetExtraRequestHeaderByName("Foo", "Bar", false); |
| 90 | request->Start(); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 91 | base::MessageLoop::current()->Run(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 92 | EXPECT_EQ("Bar", delegate.data_received()); |
| 93 | } |
| 94 | |
| 95 | TEST_F(URLRequestContextBuilderTest, UserAgent) { |
| 96 | ASSERT_TRUE(test_server_.Start()); |
| 97 | |
| 98 | builder_.set_user_agent("Bar"); |
| 99 | scoped_ptr<URLRequestContext> context(builder_.Build()); |
| 100 | TestDelegate delegate; |
Torne (Richard Coles) | 03b57e0 | 2014-08-28 12:05:23 +0100 | [diff] [blame] | 101 | scoped_ptr<URLRequest> request( |
| 102 | context->CreateRequest(test_server_.GetURL("echoheader?User-Agent"), |
| 103 | DEFAULT_PRIORITY, |
| 104 | &delegate, |
| 105 | NULL)); |
| 106 | request->set_method("GET"); |
| 107 | request->Start(); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 108 | base::MessageLoop::current()->Run(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 109 | EXPECT_EQ("Bar", delegate.data_received()); |
| 110 | } |
| 111 | |
Torne (Richard Coles) | 010d83a | 2014-05-14 12:12:37 +0100 | [diff] [blame] | 112 | TEST_F(URLRequestContextBuilderTest, ExtraHttpAuthHandlerFactory) { |
| 113 | GURL gurl("www.google.com"); |
| 114 | const int kBasicReturnCode = net::OK; |
| 115 | MockHttpAuthHandlerFactory* mock_factory_basic = |
| 116 | new MockHttpAuthHandlerFactory(kBasicReturnCode); |
| 117 | scoped_ptr<HttpAuthHandler> handler; |
| 118 | builder_.add_http_auth_handler_factory("ExtraScheme", mock_factory_basic); |
| 119 | scoped_ptr<URLRequestContext> context(builder_.Build()); |
| 120 | // Verify that a handler is returned for and added scheme. |
| 121 | EXPECT_EQ(kBasicReturnCode, |
| 122 | context->http_auth_handler_factory()->CreateAuthHandlerFromString( |
| 123 | "ExtraScheme", |
| 124 | HttpAuth::AUTH_SERVER, |
| 125 | gurl, |
| 126 | BoundNetLog(), |
| 127 | &handler)); |
| 128 | // Verify that a handler isn't returned for a bogus scheme. |
| 129 | EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, |
| 130 | context->http_auth_handler_factory()->CreateAuthHandlerFromString( |
| 131 | "Bogus", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); |
| 132 | } |
| 133 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 134 | } // namespace |
| 135 | |
| 136 | } // namespace net |