1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/url_request/url_request_test_util.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/threading/thread.h"
#include "net/http/http_network_session.h"
TestCookiePolicy::TestCookiePolicy(int options_bit_mask)
: options_(options_bit_mask) {
}
TestCookiePolicy::~TestCookiePolicy() {}
int TestCookiePolicy::CanGetCookies(const GURL& url,
const GURL& first_party) const {
if (options_ & NO_GET_COOKIES)
return net::ERR_ACCESS_DENIED;
return net::OK;
}
int TestCookiePolicy::CanSetCookie(const GURL& url,
const GURL& first_party,
const std::string& cookie_line) const {
if (options_ & NO_SET_COOKIE)
return net::ERR_ACCESS_DENIED;
if (options_ & FORCE_SESSION)
return net::OK_FOR_SESSION_ONLY;
return net::OK;
}
TestURLRequestContext::TestURLRequestContext()
: ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) {
context_storage_.set_host_resolver(
net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism,
NULL, NULL));
context_storage_.set_proxy_service(net::ProxyService::CreateDirect());
Init();
}
TestURLRequestContext::TestURLRequestContext(const std::string& proxy)
: ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) {
context_storage_.set_host_resolver(
net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism,
NULL, NULL));
net::ProxyConfig proxy_config;
proxy_config.proxy_rules().ParseFromString(proxy);
context_storage_.set_proxy_service(
net::ProxyService::CreateFixed(proxy_config));
Init();
}
TestURLRequestContext::TestURLRequestContext(const std::string& proxy,
net::HostResolver* host_resolver)
: ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) {
context_storage_.set_host_resolver(host_resolver);
net::ProxyConfig proxy_config;
proxy_config.proxy_rules().ParseFromString(proxy);
context_storage_.set_proxy_service(
net::ProxyService::CreateFixed(proxy_config));
Init();
}
TestURLRequestContext::~TestURLRequestContext() {}
void TestURLRequestContext::Init() {
context_storage_.set_cert_verifier(new net::CertVerifier);
context_storage_.set_ftp_transaction_factory(
new net::FtpNetworkLayer(host_resolver()));
context_storage_.set_ssl_config_service(new net::SSLConfigServiceDefaults);
context_storage_.set_http_auth_handler_factory(
net::HttpAuthHandlerFactory::CreateDefault(host_resolver()));
net::HttpNetworkSession::Params params;
params.host_resolver = host_resolver();
params.cert_verifier = cert_verifier();
params.proxy_service = proxy_service();
params.ssl_config_service = ssl_config_service();
params.http_auth_handler_factory = http_auth_handler_factory();
params.network_delegate = network_delegate();
context_storage_.set_http_transaction_factory(new net::HttpCache(
new net::HttpNetworkSession(params),
net::HttpCache::DefaultBackend::InMemory(0)));
// In-memory cookie store.
context_storage_.set_cookie_store(new net::CookieMonster(NULL, NULL));
set_accept_language("en-us,fr");
set_accept_charset("iso-8859-1,*,utf-8");
}
TestURLRequest::TestURLRequest(const GURL& url, Delegate* delegate)
: net::URLRequest(url, delegate) {
set_context(new TestURLRequestContext());
}
TestURLRequest::~TestURLRequest() {}
TestDelegate::TestDelegate()
: cancel_in_rr_(false),
cancel_in_rs_(false),
cancel_in_rd_(false),
cancel_in_rd_pending_(false),
cancel_in_getcookiesblocked_(false),
cancel_in_setcookieblocked_(false),
quit_on_complete_(true),
quit_on_redirect_(false),
allow_certificate_errors_(false),
response_started_count_(0),
received_bytes_count_(0),
received_redirect_count_(0),
blocked_get_cookies_count_(0),
blocked_set_cookie_count_(0),
set_cookie_count_(0),
received_data_before_response_(false),
request_failed_(false),
have_certificate_errors_(false),
buf_(new net::IOBuffer(kBufferSize)) {
}
TestDelegate::~TestDelegate() {}
void TestDelegate::OnReceivedRedirect(net::URLRequest* request,
const GURL& new_url,
bool* defer_redirect) {
received_redirect_count_++;
if (quit_on_redirect_) {
*defer_redirect = true;
MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
} else if (cancel_in_rr_) {
request->Cancel();
}
}
void TestDelegate::OnAuthRequired(net::URLRequest* request,
net::AuthChallengeInfo* auth_info) {
if (!username_.empty() || !password_.empty()) {
request->SetAuth(username_, password_);
} else {
request->CancelAuth();
}
}
void TestDelegate::OnSSLCertificateError(net::URLRequest* request,
int cert_error,
net::X509Certificate* cert) {
// The caller can control whether it needs all SSL requests to go through,
// independent of any possible errors, or whether it wants SSL errors to
// cancel the request.
have_certificate_errors_ = true;
if (allow_certificate_errors_)
request->ContinueDespiteLastError();
else
request->Cancel();
}
void TestDelegate::OnGetCookies(net::URLRequest* request,
bool blocked_by_policy) {
if (blocked_by_policy) {
blocked_get_cookies_count_++;
if (cancel_in_getcookiesblocked_)
request->Cancel();
}
}
void TestDelegate::OnSetCookie(net::URLRequest* request,
const std::string& cookie_line,
const net::CookieOptions& options,
bool blocked_by_policy) {
if (blocked_by_policy) {
blocked_set_cookie_count_++;
if (cancel_in_setcookieblocked_)
request->Cancel();
} else {
set_cookie_count_++;
}
}
void TestDelegate::OnResponseStarted(net::URLRequest* request) {
// It doesn't make sense for the request to have IO pending at this point.
DCHECK(!request->status().is_io_pending());
response_started_count_++;
if (cancel_in_rs_) {
request->Cancel();
OnResponseCompleted(request);
} else if (!request->status().is_success()) {
DCHECK(request->status().status() == net::URLRequestStatus::FAILED ||
request->status().status() == net::URLRequestStatus::CANCELED);
request_failed_ = true;
OnResponseCompleted(request);
} else {
// Initiate the first read.
int bytes_read = 0;
if (request->Read(buf_, kBufferSize, &bytes_read))
OnReadCompleted(request, bytes_read);
else if (!request->status().is_io_pending())
OnResponseCompleted(request);
}
}
void TestDelegate::OnReadCompleted(net::URLRequest* request, int bytes_read) {
// It doesn't make sense for the request to have IO pending at this point.
DCHECK(!request->status().is_io_pending());
if (response_started_count_ == 0)
received_data_before_response_ = true;
if (cancel_in_rd_)
request->Cancel();
if (bytes_read >= 0) {
// There is data to read.
received_bytes_count_ += bytes_read;
// consume the data
data_received_.append(buf_->data(), bytes_read);
}
// If it was not end of stream, request to read more.
if (request->status().is_success() && bytes_read > 0) {
bytes_read = 0;
while (request->Read(buf_, kBufferSize, &bytes_read)) {
if (bytes_read > 0) {
data_received_.append(buf_->data(), bytes_read);
received_bytes_count_ += bytes_read;
} else {
break;
}
}
}
if (!request->status().is_io_pending())
OnResponseCompleted(request);
else if (cancel_in_rd_pending_)
request->Cancel();
}
void TestDelegate::OnResponseCompleted(net::URLRequest* request) {
if (quit_on_complete_)
MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
}
TestNetworkDelegate::TestNetworkDelegate()
: last_os_error_(0),
error_count_(0) {
}
TestNetworkDelegate::~TestNetworkDelegate() {}
int TestNetworkDelegate::OnBeforeURLRequest(
net::URLRequest* request, net::CompletionCallback* callback) {
return net::OK;
}
int TestNetworkDelegate::OnBeforeSendHeaders(
uint64 request_id,
net::HttpRequestHeaders* headers,
net::CompletionCallback* callback) {
return net::OK;
}
void TestNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
if (request->status().status() == net::URLRequestStatus::FAILED) {
error_count_++;
last_os_error_ = request->status().os_error();
}
}
void TestNetworkDelegate::OnReadCompleted(net::URLRequest* request,
int bytes_read) {
if (request->status().status() == net::URLRequestStatus::FAILED) {
error_count_++;
last_os_error_ = request->status().os_error();
}
}
void TestNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
}
net::URLRequestJob* TestNetworkDelegate::OnMaybeCreateURLRequestJob(
net::URLRequest* request) {
return NULL;
}
|