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/test/spawned_test_server/spawned_test_server.h"
9#include "net/url_request/url_request.h"
10#include "net/url_request/url_request_test_util.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "testing/platform_test.h"
13
14#if defined(OS_LINUX) || defined(OS_ANDROID)
15#include "net/proxy/proxy_config.h"
16#include "net/proxy/proxy_config_service_fixed.h"
17#endif  // defined(OS_LINUX) || defined(OS_ANDROID)
18
19namespace net {
20
21namespace {
22
23// A subclass of SpawnedTestServer that uses a statically-configured hostname.
24// This is to work around mysterious failures in chrome_frame_net_tests. See:
25// http://crbug.com/114369
26class LocalHttpTestServer : public SpawnedTestServer {
27 public:
28  explicit LocalHttpTestServer(const base::FilePath& document_root)
29      : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
30                          ScopedCustomUrlRequestTestHttpHost::value(),
31                          document_root) {}
32  LocalHttpTestServer()
33      : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
34                          ScopedCustomUrlRequestTestHttpHost::value(),
35                          base::FilePath()) {}
36};
37
38class URLRequestContextBuilderTest : public PlatformTest {
39 protected:
40  URLRequestContextBuilderTest()
41      : test_server_(
42          base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))) {
43#if defined(OS_LINUX) || defined(OS_ANDROID)
44    builder_.set_proxy_config_service(
45        new ProxyConfigServiceFixed(ProxyConfig::CreateDirect()));
46#endif  // defined(OS_LINUX) || defined(OS_ANDROID)
47  }
48
49  LocalHttpTestServer test_server_;
50  URLRequestContextBuilder builder_;
51};
52
53TEST_F(URLRequestContextBuilderTest, DefaultSettings) {
54  ASSERT_TRUE(test_server_.Start());
55
56  scoped_ptr<URLRequestContext> context(builder_.Build());
57  TestDelegate delegate;
58  URLRequest request(
59      test_server_.GetURL("echoheader?Foo"), &delegate, context.get());
60  request.set_method("GET");
61  request.SetExtraRequestHeaderByName("Foo", "Bar", false);
62  request.Start();
63  base::MessageLoop::current()->Run();
64  EXPECT_EQ("Bar", delegate.data_received());
65}
66
67TEST_F(URLRequestContextBuilderTest, UserAgent) {
68  ASSERT_TRUE(test_server_.Start());
69
70  builder_.set_user_agent("Bar");
71  scoped_ptr<URLRequestContext> context(builder_.Build());
72  TestDelegate delegate;
73  URLRequest request(
74      test_server_.GetURL("echoheader?User-Agent"), &delegate, context.get());
75  request.set_method("GET");
76  request.Start();
77  base::MessageLoop::current()->Run();
78  EXPECT_EQ("Bar", delegate.data_received());
79}
80
81}  // namespace
82
83}  // namespace net
84