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 "base/memory/scoped_ptr.h"
8 #include "build/build_config.h"
9 #include "net/base/request_priority.h"
10 #include "net/http/http_auth_handler.h"
11 #include "net/http/http_auth_handler_factory.h"
12 #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_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
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)
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:
29 // http://crbug.com/114369
30 class LocalHttpTestServer
: public SpawnedTestServer
{
32 explicit LocalHttpTestServer(const base::FilePath
& document_root
)
33 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP
,
34 ScopedCustomUrlRequestTestHttpHost::value(),
37 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP
,
38 ScopedCustomUrlRequestTestHttpHost::value(),
42 class MockHttpAuthHandlerFactory
: public HttpAuthHandlerFactory
{
44 explicit MockHttpAuthHandlerFactory(int return_code
) :
45 return_code_(return_code
) {}
46 ~MockHttpAuthHandlerFactory() override
{}
48 int CreateAuthHandler(HttpAuthChallengeTokenizer
* challenge
,
49 HttpAuth::Target target
,
53 const BoundNetLog
& net_log
,
54 scoped_ptr
<HttpAuthHandler
>* handler
) override
{
63 class URLRequestContextBuilderTest
: public PlatformTest
{
65 URLRequestContextBuilderTest()
67 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))) {
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)
74 LocalHttpTestServer test_server_
;
75 URLRequestContextBuilder builder_
;
78 TEST_F(URLRequestContextBuilderTest
, DefaultSettings
) {
79 ASSERT_TRUE(test_server_
.Start());
81 scoped_ptr
<URLRequestContext
> context(builder_
.Build());
82 TestDelegate delegate
;
83 scoped_ptr
<URLRequest
> request(context
->CreateRequest(
84 test_server_
.GetURL("echoheader?Foo"), DEFAULT_PRIORITY
, &delegate
));
85 request
->set_method("GET");
86 request
->SetExtraRequestHeaderByName("Foo", "Bar", false);
88 base::MessageLoop::current()->Run();
89 EXPECT_EQ("Bar", delegate
.data_received());
92 TEST_F(URLRequestContextBuilderTest
, UserAgent
) {
93 ASSERT_TRUE(test_server_
.Start());
95 builder_
.set_user_agent("Bar");
96 scoped_ptr
<URLRequestContext
> context(builder_
.Build());
97 TestDelegate delegate
;
98 scoped_ptr
<URLRequest
> request(
99 context
->CreateRequest(test_server_
.GetURL("echoheader?User-Agent"),
100 DEFAULT_PRIORITY
, &delegate
));
101 request
->set_method("GET");
103 base::MessageLoop::current()->Run();
104 EXPECT_EQ("Bar", delegate
.data_received());
107 TEST_F(URLRequestContextBuilderTest
, ExtraHttpAuthHandlerFactory
) {
108 GURL
gurl("www.google.com");
109 const int kBasicReturnCode
= net::OK
;
110 MockHttpAuthHandlerFactory
* mock_factory_basic
=
111 new MockHttpAuthHandlerFactory(kBasicReturnCode
);
112 scoped_ptr
<HttpAuthHandler
> handler
;
113 builder_
.add_http_auth_handler_factory("ExtraScheme", mock_factory_basic
);
114 scoped_ptr
<URLRequestContext
> context(builder_
.Build());
115 // Verify that a handler is returned for and added scheme.
116 EXPECT_EQ(kBasicReturnCode
,
117 context
->http_auth_handler_factory()->CreateAuthHandlerFromString(
119 HttpAuth::AUTH_SERVER
,
123 // Verify that a handler isn't returned for a bogus scheme.
124 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME
,
125 context
->http_auth_handler_factory()->CreateAuthHandlerFromString(
126 "Bogus", HttpAuth::AUTH_SERVER
, gurl
, BoundNetLog(), &handler
));