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 class MockHttpAuthHandlerFactory
: public HttpAuthHandlerFactory
{
29 explicit MockHttpAuthHandlerFactory(int return_code
) :
30 return_code_(return_code
) {}
31 ~MockHttpAuthHandlerFactory() override
{}
33 int CreateAuthHandler(HttpAuthChallengeTokenizer
* challenge
,
34 HttpAuth::Target target
,
38 const BoundNetLog
& net_log
,
39 scoped_ptr
<HttpAuthHandler
>* handler
) override
{
48 class URLRequestContextBuilderTest
: public PlatformTest
{
50 URLRequestContextBuilderTest()
51 : test_server_(SpawnedTestServer::TYPE_HTTP
,
52 SpawnedTestServer::kLocalhost
,
54 FILE_PATH_LITERAL("net/data/url_request_unittest"))) {
55 #if defined(OS_LINUX) || defined(OS_ANDROID)
56 builder_
.set_proxy_config_service(
57 new ProxyConfigServiceFixed(ProxyConfig::CreateDirect()));
58 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
61 SpawnedTestServer test_server_
;
62 URLRequestContextBuilder builder_
;
65 TEST_F(URLRequestContextBuilderTest
, DefaultSettings
) {
66 ASSERT_TRUE(test_server_
.Start());
68 scoped_ptr
<URLRequestContext
> context(builder_
.Build());
69 TestDelegate delegate
;
70 scoped_ptr
<URLRequest
> request(context
->CreateRequest(
71 test_server_
.GetURL("echoheader?Foo"), DEFAULT_PRIORITY
, &delegate
));
72 request
->set_method("GET");
73 request
->SetExtraRequestHeaderByName("Foo", "Bar", false);
75 base::MessageLoop::current()->Run();
76 EXPECT_EQ("Bar", delegate
.data_received());
79 TEST_F(URLRequestContextBuilderTest
, UserAgent
) {
80 ASSERT_TRUE(test_server_
.Start());
82 builder_
.set_user_agent("Bar");
83 scoped_ptr
<URLRequestContext
> context(builder_
.Build());
84 TestDelegate delegate
;
85 scoped_ptr
<URLRequest
> request(
86 context
->CreateRequest(test_server_
.GetURL("echoheader?User-Agent"),
87 DEFAULT_PRIORITY
, &delegate
));
88 request
->set_method("GET");
90 base::MessageLoop::current()->Run();
91 EXPECT_EQ("Bar", delegate
.data_received());
94 TEST_F(URLRequestContextBuilderTest
, ExtraHttpAuthHandlerFactory
) {
95 GURL
gurl("www.google.com");
96 const int kBasicReturnCode
= OK
;
97 MockHttpAuthHandlerFactory
* mock_factory_basic
=
98 new MockHttpAuthHandlerFactory(kBasicReturnCode
);
99 scoped_ptr
<HttpAuthHandler
> handler
;
100 builder_
.add_http_auth_handler_factory("ExtraScheme", mock_factory_basic
);
101 scoped_ptr
<URLRequestContext
> context(builder_
.Build());
102 // Verify that a handler is returned for and added scheme.
103 EXPECT_EQ(kBasicReturnCode
,
104 context
->http_auth_handler_factory()->CreateAuthHandlerFromString(
106 HttpAuth::AUTH_SERVER
,
110 // Verify that a handler isn't returned for a bogus scheme.
111 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME
,
112 context
->http_auth_handler_factory()->CreateAuthHandlerFromString(
113 "Bogus", HttpAuth::AUTH_SERVER
, gurl
, BoundNetLog(), &handler
));