OLD | NEW |
| (Empty) |
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 "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" | |
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 | |
23 namespace net { | |
24 | |
25 namespace { | |
26 | |
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 { | |
31 public: | |
32 explicit LocalHttpTestServer(const base::FilePath& document_root) | |
33 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, | |
34 ScopedCustomUrlRequestTestHttpHost::value(), | |
35 document_root) {} | |
36 LocalHttpTestServer() | |
37 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, | |
38 ScopedCustomUrlRequestTestHttpHost::value(), | |
39 base::FilePath()) {} | |
40 }; | |
41 | |
42 class MockHttpAuthHandlerFactory : public HttpAuthHandlerFactory { | |
43 public: | |
44 explicit MockHttpAuthHandlerFactory(int return_code) : | |
45 return_code_(return_code) {} | |
46 ~MockHttpAuthHandlerFactory() override {} | |
47 | |
48 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 | |
63 class URLRequestContextBuilderTest : public PlatformTest { | |
64 protected: | |
65 URLRequestContextBuilderTest() | |
66 : test_server_( | |
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) | |
72 } | |
73 | |
74 LocalHttpTestServer test_server_; | |
75 URLRequestContextBuilder builder_; | |
76 }; | |
77 | |
78 TEST_F(URLRequestContextBuilderTest, DefaultSettings) { | |
79 ASSERT_TRUE(test_server_.Start()); | |
80 | |
81 scoped_ptr<URLRequestContext> context(builder_.Build()); | |
82 TestDelegate delegate; | |
83 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(); | |
91 base::MessageLoop::current()->Run(); | |
92 EXPECT_EQ("Bar", delegate.data_received()); | |
93 } | |
94 | |
95 TEST_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; | |
101 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(); | |
108 base::MessageLoop::current()->Run(); | |
109 EXPECT_EQ("Bar", delegate.data_received()); | |
110 } | |
111 | |
112 TEST_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 | |
134 } // namespace | |
135 | |
136 } // namespace net | |
OLD | NEW |