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.
5 #include "net/url_request/url_request_context_builder.h"
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"
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)
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
{
31 explicit LocalHttpTestServer(const base::FilePath
& document_root
)
32 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP
,
33 ScopedCustomUrlRequestTestHttpHost::value(),
36 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP
,
37 ScopedCustomUrlRequestTestHttpHost::value(),
41 class MockHttpAuthHandlerFactory
: public HttpAuthHandlerFactory
{
43 explicit MockHttpAuthHandlerFactory(int return_code
) :
44 return_code_(return_code
) {}
45 virtual ~MockHttpAuthHandlerFactory() {}
47 virtual int CreateAuthHandler(HttpAuthChallengeTokenizer
* challenge
,
48 HttpAuth::Target target
,
52 const BoundNetLog
& net_log
,
53 scoped_ptr
<HttpAuthHandler
>* handler
) OVERRIDE
{
62 class URLRequestContextBuilderTest
: public PlatformTest
{
64 URLRequestContextBuilderTest()
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)
73 LocalHttpTestServer test_server_
;
74 URLRequestContextBuilder builder_
;
77 TEST_F(URLRequestContextBuilderTest
, DefaultSettings
) {
78 ASSERT_TRUE(test_server_
.Start());
80 scoped_ptr
<URLRequestContext
> context(builder_
.Build());
81 TestDelegate delegate
;
82 URLRequest
request(test_server_
.GetURL("echoheader?Foo"),
86 request
.set_method("GET");
87 request
.SetExtraRequestHeaderByName("Foo", "Bar", false);
89 base::MessageLoop::current()->Run();
90 EXPECT_EQ("Bar", delegate
.data_received());
93 TEST_F(URLRequestContextBuilderTest
, UserAgent
) {
94 ASSERT_TRUE(test_server_
.Start());
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"),
103 request
.set_method("GET");
105 base::MessageLoop::current()->Run();
106 EXPECT_EQ("Bar", delegate
.data_received());
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(
121 HttpAuth::AUTH_SERVER
,
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
));