OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/url_request_test_util.h" | 5 #include "net/url_request/url_request_test_util.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "base/threading/worker_pool.h" | 11 #include "base/threading/worker_pool.h" |
12 #include "net/base/cert_verifier.h" | 12 #include "net/base/cert_verifier.h" |
13 #include "net/base/default_server_bound_cert_store.h" | 13 #include "net/base/default_server_bound_cert_store.h" |
14 #include "net/base/host_port_pair.h" | 14 #include "net/base/host_port_pair.h" |
| 15 #include "net/base/mock_host_resolver.h" |
15 #include "net/base/server_bound_cert_service.h" | 16 #include "net/base/server_bound_cert_service.h" |
16 #include "net/http/http_network_session.h" | 17 #include "net/http/http_network_session.h" |
17 #include "net/http/http_server_properties_impl.h" | 18 #include "net/http/http_server_properties_impl.h" |
18 #include "net/url_request/url_request_job_factory.h" | 19 #include "net/url_request/url_request_job_factory.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // These constants put the net::NetworkDelegate events of TestNetworkDelegate | 24 // These constants put the net::NetworkDelegate events of TestNetworkDelegate |
24 // into an order. They are used in conjunction with | 25 // into an order. They are used in conjunction with |
25 // |TestNetworkDelegate::next_states_| to check that we do not send | 26 // |TestNetworkDelegate::next_states_| to check that we do not send |
26 // events in the wrong order. | 27 // events in the wrong order. |
27 const int kStageBeforeURLRequest = 1 << 0; | 28 const int kStageBeforeURLRequest = 1 << 0; |
28 const int kStageBeforeSendHeaders = 1 << 1; | 29 const int kStageBeforeSendHeaders = 1 << 1; |
29 const int kStageSendHeaders = 1 << 2; | 30 const int kStageSendHeaders = 1 << 2; |
30 const int kStageHeadersReceived = 1 << 3; | 31 const int kStageHeadersReceived = 1 << 3; |
31 const int kStageAuthRequired = 1 << 4; | 32 const int kStageAuthRequired = 1 << 4; |
32 const int kStageBeforeRedirect = 1 << 5; | 33 const int kStageBeforeRedirect = 1 << 5; |
33 const int kStageResponseStarted = 1 << 6; | 34 const int kStageResponseStarted = 1 << 6; |
34 const int kStageCompletedSuccess = 1 << 7; | 35 const int kStageCompletedSuccess = 1 << 7; |
35 const int kStageCompletedError = 1 << 8; | 36 const int kStageCompletedError = 1 << 8; |
36 const int kStageURLRequestDestroyed = 1 << 9; | 37 const int kStageURLRequestDestroyed = 1 << 9; |
37 const int kStageDestruction = 1 << 10; | 38 const int kStageDestruction = 1 << 10; |
38 | 39 |
39 } // namespace | 40 } // namespace |
40 | 41 |
41 TestURLRequestContext::TestURLRequestContext() | 42 TestURLRequestContext::TestURLRequestContext() |
42 : initialized_(false), | 43 : initialized_(false), |
43 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { | 44 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { |
44 context_storage_.set_host_resolver( | |
45 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, | |
46 net::HostResolver::kDefaultRetryAttempts, | |
47 NULL)); | |
48 SetProxyDirect(); | |
49 Init(); | 45 Init(); |
50 } | 46 } |
51 | 47 |
52 TestURLRequestContext::TestURLRequestContext(bool delay_initialization) | 48 TestURLRequestContext::TestURLRequestContext(bool delay_initialization) |
53 : initialized_(false), | 49 : initialized_(false), |
54 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { | 50 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { |
55 context_storage_.set_host_resolver( | |
56 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, | |
57 net::HostResolver::kDefaultRetryAttempts, | |
58 NULL)); | |
59 SetProxyDirect(); | |
60 if (!delay_initialization) | 51 if (!delay_initialization) |
61 Init(); | 52 Init(); |
62 } | 53 } |
63 | 54 |
64 TestURLRequestContext::TestURLRequestContext(const std::string& proxy) | |
65 : initialized_(false), | |
66 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { | |
67 context_storage_.set_host_resolver( | |
68 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, | |
69 net::HostResolver::kDefaultRetryAttempts, | |
70 NULL)); | |
71 SetProxyFromString(proxy); | |
72 Init(); | |
73 } | |
74 | |
75 TestURLRequestContext::TestURLRequestContext(const char* proxy) | |
76 : initialized_(false), | |
77 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { | |
78 context_storage_.set_host_resolver( | |
79 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, | |
80 net::HostResolver::kDefaultRetryAttempts, | |
81 NULL)); | |
82 SetProxyFromString(proxy); | |
83 Init(); | |
84 } | |
85 | |
86 TestURLRequestContext::TestURLRequestContext(const std::string& proxy, | |
87 net::HostResolver* host_resolver) | |
88 : initialized_(false), | |
89 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { | |
90 context_storage_.set_host_resolver(host_resolver); | |
91 SetProxyFromString(proxy); | |
92 Init(); | |
93 } | |
94 | |
95 void TestURLRequestContext::SetProxyFromString(const std::string& proxy) { | |
96 DCHECK(!initialized_); | |
97 net::ProxyConfig proxy_config; | |
98 proxy_config.proxy_rules().ParseFromString(proxy); | |
99 context_storage_.set_proxy_service( | |
100 net::ProxyService::CreateFixed(proxy_config)); | |
101 } | |
102 | |
103 void TestURLRequestContext::SetProxyDirect() { | |
104 DCHECK(!initialized_); | |
105 context_storage_.set_proxy_service(net::ProxyService::CreateDirect()); | |
106 } | |
107 | |
108 TestURLRequestContext::~TestURLRequestContext() { | 55 TestURLRequestContext::~TestURLRequestContext() { |
109 DCHECK(initialized_); | 56 DCHECK(initialized_); |
110 } | 57 } |
111 | 58 |
112 void TestURLRequestContext::Init() { | 59 void TestURLRequestContext::Init() { |
113 DCHECK(!initialized_); | 60 DCHECK(!initialized_); |
114 initialized_ = true; | 61 initialized_ = true; |
| 62 |
| 63 if (!host_resolver()) |
| 64 context_storage_.set_host_resolver(new net::MockCachingHostResolver()); |
| 65 if (!proxy_service()) |
| 66 context_storage_.set_proxy_service(net::ProxyService::CreateDirect()); |
115 if (!cert_verifier()) | 67 if (!cert_verifier()) |
116 context_storage_.set_cert_verifier(net::CertVerifier::CreateDefault()); | 68 context_storage_.set_cert_verifier(net::CertVerifier::CreateDefault()); |
117 if (!ftp_transaction_factory()) { | 69 if (!ftp_transaction_factory()) { |
118 context_storage_.set_ftp_transaction_factory( | 70 context_storage_.set_ftp_transaction_factory( |
119 new net::FtpNetworkLayer(host_resolver())); | 71 new net::FtpNetworkLayer(host_resolver())); |
120 } | 72 } |
121 if (!ssl_config_service()) | 73 if (!ssl_config_service()) |
122 context_storage_.set_ssl_config_service(new net::SSLConfigServiceDefaults); | 74 context_storage_.set_ssl_config_service(new net::SSLConfigServiceDefaults); |
123 if (!http_auth_handler_factory()) { | 75 if (!http_auth_handler_factory()) { |
124 context_storage_.set_http_auth_handler_factory( | 76 context_storage_.set_http_auth_handler_factory( |
(...skipping 28 matching lines...) Expand all Loading... |
153 base::WorkerPool::GetTaskRunner(true))); | 105 base::WorkerPool::GetTaskRunner(true))); |
154 } | 106 } |
155 if (accept_language().empty()) | 107 if (accept_language().empty()) |
156 set_accept_language("en-us,fr"); | 108 set_accept_language("en-us,fr"); |
157 if (accept_charset().empty()) | 109 if (accept_charset().empty()) |
158 set_accept_charset("iso-8859-1,*,utf-8"); | 110 set_accept_charset("iso-8859-1,*,utf-8"); |
159 if (!job_factory()) | 111 if (!job_factory()) |
160 context_storage_.set_job_factory(new net::URLRequestJobFactory); | 112 context_storage_.set_job_factory(new net::URLRequestJobFactory); |
161 } | 113 } |
162 | 114 |
163 | |
164 TestURLRequest::TestURLRequest(const GURL& url, Delegate* delegate) | 115 TestURLRequest::TestURLRequest(const GURL& url, Delegate* delegate) |
165 : net::URLRequest(url, delegate), | 116 : net::URLRequest(url, delegate), |
166 context_(new TestURLRequestContext) { | 117 context_(new TestURLRequestContext) { |
167 set_context(context_.get()); | 118 set_context(context_.get()); |
168 } | 119 } |
169 | 120 |
170 TestURLRequest::~TestURLRequest() { | 121 TestURLRequest::~TestURLRequest() { |
171 set_context(NULL); | 122 set_context(NULL); |
172 } | 123 } |
173 | 124 |
174 TestURLRequestContextGetter::TestURLRequestContextGetter( | 125 TestURLRequestContextGetter::TestURLRequestContextGetter( |
175 const scoped_refptr<base::MessageLoopProxy>& io_message_loop_proxy) | 126 const scoped_refptr<base::MessageLoopProxy>& io_message_loop_proxy) |
176 : io_message_loop_proxy_(io_message_loop_proxy) { | 127 : io_message_loop_proxy_(io_message_loop_proxy) { |
177 DCHECK(io_message_loop_proxy.get()); | 128 DCHECK(io_message_loop_proxy.get()); |
178 } | 129 } |
179 | 130 |
| 131 TestURLRequestContextGetter::TestURLRequestContextGetter( |
| 132 const scoped_refptr<base::MessageLoopProxy>& io_message_loop_proxy, |
| 133 scoped_ptr<TestURLRequestContext> context) |
| 134 : io_message_loop_proxy_(io_message_loop_proxy), context_(context.Pass()) { |
| 135 DCHECK(io_message_loop_proxy.get()); |
| 136 } |
| 137 |
180 TestURLRequestContextGetter::~TestURLRequestContextGetter() {} | 138 TestURLRequestContextGetter::~TestURLRequestContextGetter() {} |
181 | 139 |
182 TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() { | 140 TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() { |
183 if (!context_.get()) | 141 if (!context_.get()) |
184 context_.reset(new TestURLRequestContext); | 142 context_.reset(new TestURLRequestContext); |
185 return context_.get(); | 143 return context_.get(); |
186 } | 144 } |
187 | 145 |
188 scoped_refptr<base::MessageLoopProxy> | 146 scoped_refptr<base::MessageLoopProxy> |
189 TestURLRequestContextGetter::GetIOMessageLoopProxy() const { | 147 TestURLRequestContextGetter::GetIOMessageLoopProxy() const { |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 | 505 |
548 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() { | 506 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() { |
549 DCHECK_EQ(value_, new_value_); | 507 DCHECK_EQ(value_, new_value_); |
550 value_ = old_value_; | 508 value_ = old_value_; |
551 } | 509 } |
552 | 510 |
553 // static | 511 // static |
554 const std::string& ScopedCustomUrlRequestTestHttpHost::value() { | 512 const std::string& ScopedCustomUrlRequestTestHttpHost::value() { |
555 return value_; | 513 return value_; |
556 } | 514 } |
OLD | NEW |