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
|
// Copyright (c) 2012 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.
#ifndef NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_
#define NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_
#pragma once
#include <stdlib.h>
#include <map>
#include <string>
#include "base/compiler_specific.h"
#include "base/path_service.h"
#include "base/process_util.h"
#include "base/string_util.h"
#include "base/string16.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "googleurl/src/url_util.h"
#include "net/base/cert_verifier.h"
#include "net/base/cookie_monster.h"
#include "net/base/host_resolver.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/network_delegate.h"
#include "net/base/ssl_config_service_defaults.h"
#include "net/disk_cache/disk_cache.h"
#include "net/ftp/ftp_network_layer.h"
#include "net/http/http_auth_handler_factory.h"
#include "net/http/http_cache.h"
#include "net/http/http_network_layer.h"
#include "net/proxy/proxy_service.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_storage.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::TimeDelta;
//-----------------------------------------------------------------------------
class TestURLRequestContext : public net::URLRequestContext {
public:
TestURLRequestContext();
// Default constructor like TestURLRequestContext() but does not call
// Init() in case |delay_initialization| is true. This allows modifying the
// URLRequestContext before it is constructed completely. If
// |delay_initialization| is true, Init() needs be be called manually.
explicit TestURLRequestContext(bool delay_initialization);
// We need this constructor because TestURLRequestContext("foo") actually
// calls the boolean constructor rather than the std::string constructor.
explicit TestURLRequestContext(const char* proxy);
explicit TestURLRequestContext(const std::string& proxy);
TestURLRequestContext(const std::string& proxy,
net::HostResolver* host_resolver);
// Configures the proxy server, must not be called after Init().
void SetProxyFromString(const std::string& proxy);
void SetProxyDirect();
void Init();
protected:
virtual ~TestURLRequestContext();
private:
bool initialized_;
net::URLRequestContextStorage context_storage_;
};
//-----------------------------------------------------------------------------
class TestURLRequest : public net::URLRequest {
public:
TestURLRequest(const GURL& url, Delegate* delegate);
virtual ~TestURLRequest();
};
//-----------------------------------------------------------------------------
class TestDelegate : public net::URLRequest::Delegate {
public:
enum Options {
NO_GET_COOKIES = 1 << 0,
NO_SET_COOKIE = 1 << 1,
FORCE_SESSION = 1 << 2,
};
TestDelegate();
virtual ~TestDelegate();
void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; }
void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; }
void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; }
void set_cancel_in_received_data_pending(bool val) {
cancel_in_rd_pending_ = val;
}
void set_quit_on_complete(bool val) { quit_on_complete_ = val; }
void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; }
void set_allow_certificate_errors(bool val) {
allow_certificate_errors_ = val;
}
void set_cookie_options(int o) {cookie_options_bit_mask_ = o; }
void set_credentials(const net::AuthCredentials& credentials) {
credentials_ = credentials;
}
// query state
const std::string& data_received() const { return data_received_; }
int bytes_received() const { return static_cast<int>(data_received_.size()); }
int response_started_count() const { return response_started_count_; }
int received_redirect_count() const { return received_redirect_count_; }
int blocked_get_cookies_count() const { return blocked_get_cookies_count_; }
int blocked_set_cookie_count() const { return blocked_set_cookie_count_; }
int set_cookie_count() const { return set_cookie_count_; }
bool received_data_before_response() const {
return received_data_before_response_;
}
bool request_failed() const { return request_failed_; }
bool have_certificate_errors() const { return have_certificate_errors_; }
bool certificate_errors_are_fatal() const {
return certificate_errors_are_fatal_;
}
bool auth_required_called() const { return auth_required_; }
// net::URLRequest::Delegate:
virtual void OnReceivedRedirect(net::URLRequest* request, const GURL& new_url,
bool* defer_redirect) OVERRIDE;
virtual void OnAuthRequired(net::URLRequest* request,
net::AuthChallengeInfo* auth_info) OVERRIDE;
// NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true.
// (Unit tests use this as a post-condition.) But for policy, this method
// consults |allow_certificate_errors_|.
virtual void OnSSLCertificateError(net::URLRequest* request,
const net::SSLInfo& ssl_info,
bool fatal) OVERRIDE;
virtual bool CanGetCookies(const net::URLRequest* request,
const net::CookieList& cookie_list) const OVERRIDE;
virtual bool CanSetCookie(const net::URLRequest* request,
const std::string& cookie_line,
net::CookieOptions* options) const OVERRIDE;
virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
virtual void OnReadCompleted(net::URLRequest* request,
int bytes_read) OVERRIDE;
private:
static const int kBufferSize = 4096;
virtual void OnResponseCompleted(net::URLRequest* request);
// options for controlling behavior
bool cancel_in_rr_;
bool cancel_in_rs_;
bool cancel_in_rd_;
bool cancel_in_rd_pending_;
bool quit_on_complete_;
bool quit_on_redirect_;
bool allow_certificate_errors_;
int cookie_options_bit_mask_;
net::AuthCredentials credentials_;
// tracks status of callbacks
int response_started_count_;
int received_bytes_count_;
int received_redirect_count_;
mutable int blocked_get_cookies_count_;
mutable int blocked_set_cookie_count_;
mutable int set_cookie_count_;
bool received_data_before_response_;
bool request_failed_;
bool have_certificate_errors_;
bool certificate_errors_are_fatal_;
bool auth_required_;
std::string data_received_;
// our read buffer
scoped_refptr<net::IOBuffer> buf_;
};
//-----------------------------------------------------------------------------
class TestNetworkDelegate : public net::NetworkDelegate {
public:
TestNetworkDelegate();
virtual ~TestNetworkDelegate();
int last_error() const { return last_error_; }
int error_count() const { return error_count_; }
int created_requests() const { return created_requests_; }
int destroyed_requests() const { return destroyed_requests_; }
int completed_requests() const { return completed_requests_; }
protected:
// net::NetworkDelegate:
virtual int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) OVERRIDE;
virtual int OnBeforeSendHeaders(net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) OVERRIDE;
virtual void OnSendHeaders(net::URLRequest* request,
const net::HttpRequestHeaders& headers) OVERRIDE;
virtual int OnHeadersReceived(
net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers)
OVERRIDE;
virtual void OnBeforeRedirect(net::URLRequest* request,
const GURL& new_location) OVERRIDE;
virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
virtual void OnRawBytesRead(const net::URLRequest& request,
int bytes_read) OVERRIDE;
virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE;
virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE;
virtual void OnPACScriptError(int line_number,
const string16& error) OVERRIDE;
virtual net::NetworkDelegate::AuthRequiredResponse OnAuthRequired(
net::URLRequest* request,
const net::AuthChallengeInfo& auth_info,
const AuthCallback& callback,
net::AuthCredentials* credentials) OVERRIDE;
void InitRequestStatesIfNew(int request_id);
int last_error_;
int error_count_;
int created_requests_;
int destroyed_requests_;
int completed_requests_;
// net::NetworkDelegate callbacks happen in a particular order (e.g.
// OnBeforeURLRequest is always called before OnBeforeSendHeaders).
// This bit-set indicates for each request id (key) what events may be sent
// next.
std::map<int, int> next_states_;
// A log that records for each request id (key) the order in which On...
// functions were called.
std::map<int, std::string> event_order_;
};
#endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_
|