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