blob: b5a3da23b39e195b46f2a5f431bc1b2357ee0edc [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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)03b57e02014-08-28 12:05:23 +01007#include "base/memory/scoped_ptr.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#include "build/build_config.h"
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +00009#include "net/base/request_priority.h"
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +010010#include "net/http/http_auth_handler.h"
11#include "net/http/http_auth_handler_factory.h"
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +010012#include "net/test/spawned_test_server/spawned_test_server.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013#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
23namespace net {
24
25namespace {
26
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010027// 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)58218062012-11-14 11:43:16 +000029// http://crbug.com/114369
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010030class LocalHttpTestServer : public SpawnedTestServer {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000032 explicit LocalHttpTestServer(const base::FilePath& document_root)
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010033 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
34 ScopedCustomUrlRequestTestHttpHost::value(),
35 document_root) {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +000036 LocalHttpTestServer()
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010037 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
38 ScopedCustomUrlRequestTestHttpHost::value(),
39 base::FilePath()) {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +000040};
41
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +010042class 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)58218062012-11-14 11:43:16 +000063class URLRequestContextBuilderTest : public PlatformTest {
64 protected:
65 URLRequestContextBuilderTest()
66 : test_server_(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000067 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068#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
78TEST_F(URLRequestContextBuilderTest, DefaultSettings) {
79 ASSERT_TRUE(test_server_.Start());
80
81 scoped_ptr<URLRequestContext> context(builder_.Build());
82 TestDelegate delegate;
Torne (Richard Coles)03b57e02014-08-28 12:05:23 +010083 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)90dce4d2013-05-29 14:40:03 +010091 base::MessageLoop::current()->Run();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000092 EXPECT_EQ("Bar", delegate.data_received());
93}
94
95TEST_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)03b57e02014-08-28 12:05:23 +0100101 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)90dce4d2013-05-29 14:40:03 +0100108 base::MessageLoop::current()->Run();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000109 EXPECT_EQ("Bar", delegate.data_received());
110}
111
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +0100112TEST_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)58218062012-11-14 11:43:16 +0000134} // namespace
135
136} // namespace net