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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
// 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 "content/test/test_url_fetcher_factory.h"
#include <string>
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "content/common/net/url_fetcher_impl.h"
#include "content/public/common/url_fetcher_delegate.h"
#include "net/base/host_port_pair.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request_status.h"
ScopedURLFetcherFactory::ScopedURLFetcherFactory(
content::URLFetcherFactory* factory) {
DCHECK(!URLFetcherImpl::factory());
URLFetcherImpl::set_factory(factory);
}
ScopedURLFetcherFactory::~ScopedURLFetcherFactory() {
DCHECK(URLFetcherImpl::factory());
URLFetcherImpl::set_factory(NULL);
}
TestURLFetcher::TestURLFetcher(int id,
const GURL& url,
content::URLFetcherDelegate* d)
: id_(id),
original_url_(url),
delegate_(d),
did_receive_last_chunk_(false),
fake_load_flags_(0),
fake_response_code_(-1),
fake_response_destination_(STRING),
fake_was_fetched_via_proxy_(false),
fake_max_retries_(0) {
}
TestURLFetcher::~TestURLFetcher() {
}
void TestURLFetcher::SetUploadData(const std::string& upload_content_type,
const std::string& upload_content) {
upload_data_ = upload_content;
}
void TestURLFetcher::SetChunkedUpload(const std::string& upload_content_type) {
}
void TestURLFetcher::AppendChunkToUpload(const std::string& data,
bool is_last_chunk) {
DCHECK(!did_receive_last_chunk_);
did_receive_last_chunk_ = is_last_chunk;
chunks_.push_back(data);
}
void TestURLFetcher::SetLoadFlags(int load_flags) {
fake_load_flags_= load_flags;
}
int TestURLFetcher::GetLoadFlags() const {
return fake_load_flags_;
}
void TestURLFetcher::SetReferrer(const std::string& referrer) {
}
void TestURLFetcher::SetExtraRequestHeaders(
const std::string& extra_request_headers) {
fake_extra_request_headers_.Clear();
fake_extra_request_headers_.AddHeadersFromString(extra_request_headers);
}
void TestURLFetcher::GetExtraRequestHeaders(net::HttpRequestHeaders* headers) {
*headers = fake_extra_request_headers_;
}
void TestURLFetcher::SetRequestContext(
net::URLRequestContextGetter* request_context_getter) {
}
void TestURLFetcher::SetAutomaticallyRetryOn5xx(bool retry) {
}
void TestURLFetcher::SetMaxRetries(int max_retries) {
fake_max_retries_ = max_retries;
}
int TestURLFetcher::GetMaxRetries() const {
return fake_max_retries_;
}
base::TimeDelta TestURLFetcher::GetBackoffDelay() const {
return fake_backoff_delay_;
}
void TestURLFetcher::SaveResponseToTemporaryFile(
scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) {
}
net::HttpResponseHeaders* TestURLFetcher::GetResponseHeaders() const {
return fake_response_headers_;
}
net::HostPortPair TestURLFetcher::GetSocketAddress() const {
NOTIMPLEMENTED();
return net::HostPortPair();
}
bool TestURLFetcher::WasFetchedViaProxy() const {
return fake_was_fetched_via_proxy_;
}
void TestURLFetcher::Start() {
// Overriden to do nothing. It is assumed the caller will notify the delegate.
}
void TestURLFetcher::StartWithRequestContextGetter(
net::URLRequestContextGetter* request_context_getter) {
NOTIMPLEMENTED();
}
const GURL& TestURLFetcher::GetOriginalURL() const {
return original_url_;
}
const GURL& TestURLFetcher::GetURL() const {
return fake_url_;
}
const net::URLRequestStatus& TestURLFetcher::GetStatus() const {
return fake_status_;
}
int TestURLFetcher::GetResponseCode() const {
return fake_response_code_;
}
const net::ResponseCookies& TestURLFetcher::GetCookies() const {
return fake_cookies_;
}
bool TestURLFetcher::FileErrorOccurred(
base::PlatformFileError* out_error_code) const {
NOTIMPLEMENTED();
return false;
}
void TestURLFetcher::ReceivedContentWasMalformed() {
}
bool TestURLFetcher::GetResponseAsString(
std::string* out_response_string) const {
if (fake_response_destination_ != STRING)
return false;
*out_response_string = fake_response_string_;
return true;
}
bool TestURLFetcher::GetResponseAsFilePath(
bool take_ownership, FilePath* out_response_path) const {
if (fake_response_destination_ != TEMP_FILE)
return false;
*out_response_path = fake_response_file_path_;
return true;
}
void TestURLFetcher::set_status(const net::URLRequestStatus& status) {
fake_status_ = status;
}
void TestURLFetcher::set_was_fetched_via_proxy(bool flag) {
fake_was_fetched_via_proxy_ = flag;
}
void TestURLFetcher::set_response_headers(
scoped_refptr<net::HttpResponseHeaders> headers) {
fake_response_headers_ = headers;
}
void TestURLFetcher::set_backoff_delay(base::TimeDelta backoff_delay) {
fake_backoff_delay_ = backoff_delay;
}
void TestURLFetcher::SetResponseString(const std::string& response) {
fake_response_destination_ = STRING;
fake_response_string_ = response;
}
void TestURLFetcher::SetResponseFilePath(const FilePath& path) {
fake_response_destination_ = TEMP_FILE;
fake_response_file_path_ = path;
}
TestURLFetcherFactory::TestURLFetcherFactory()
: ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
}
TestURLFetcherFactory::~TestURLFetcherFactory() {}
content::URLFetcher* TestURLFetcherFactory::CreateURLFetcher(
int id,
const GURL& url,
content::URLFetcher::RequestType request_type,
content::URLFetcherDelegate* d) {
TestURLFetcher* fetcher = new TestURLFetcher(id, url, d);
fetchers_[id] = fetcher;
return fetcher;
}
TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const {
Fetchers::const_iterator i = fetchers_.find(id);
return i == fetchers_.end() ? NULL : i->second;
}
void TestURLFetcherFactory::RemoveFetcherFromMap(int id) {
Fetchers::iterator i = fetchers_.find(id);
DCHECK(i != fetchers_.end());
fetchers_.erase(i);
}
// This class is used by the FakeURLFetcherFactory below.
class FakeURLFetcher : public TestURLFetcher {
public:
// Normal URL fetcher constructor but also takes in a pre-baked response.
FakeURLFetcher(const GURL& url,
content::URLFetcherDelegate* d,
const std::string& response_data, bool success)
: TestURLFetcher(0, url, d),
ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
set_status(net::URLRequestStatus(
success ? net::URLRequestStatus::SUCCESS :
net::URLRequestStatus::FAILED,
0));
set_response_code(success ? 200 : 500);
SetResponseString(response_data);
}
// Start the request. This will call the given delegate asynchronously
// with the pre-baked response as parameter.
virtual void Start() OVERRIDE {
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&FakeURLFetcher::RunDelegate, weak_factory_.GetWeakPtr()));
}
virtual const GURL& GetURL() const OVERRIDE {
return TestURLFetcher::GetOriginalURL();
}
private:
virtual ~FakeURLFetcher() {
}
// This is the method which actually calls the delegate that is passed in the
// constructor.
void RunDelegate() {
delegate()->OnURLFetchComplete(this);
}
base::WeakPtrFactory<FakeURLFetcher> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher);
};
FakeURLFetcherFactory::FakeURLFetcherFactory()
: ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
}
FakeURLFetcherFactory::FakeURLFetcherFactory(
content::URLFetcherFactory* default_factory)
: ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
default_factory_(default_factory) {
}
FakeURLFetcherFactory::~FakeURLFetcherFactory() {}
content::URLFetcher* FakeURLFetcherFactory::CreateURLFetcher(
int id,
const GURL& url,
content::URLFetcher::RequestType request_type,
content::URLFetcherDelegate* d) {
FakeResponseMap::const_iterator it = fake_responses_.find(url);
if (it == fake_responses_.end()) {
if (default_factory_ == NULL) {
// If we don't have a baked response for that URL we return NULL.
DLOG(ERROR) << "No baked response for URL: " << url.spec();
return NULL;
} else {
return default_factory_->CreateURLFetcher(id, url, request_type, d);
}
}
return new FakeURLFetcher(url, d, it->second.first, it->second.second);
}
void FakeURLFetcherFactory::SetFakeResponse(const std::string& url,
const std::string& response_data,
bool success) {
// Overwrite existing URL if it already exists.
fake_responses_[GURL(url)] = std::make_pair(response_data, success);
}
void FakeURLFetcherFactory::ClearFakeResponses() {
fake_responses_.clear();
}
URLFetcherImplFactory::URLFetcherImplFactory() {}
URLFetcherImplFactory::~URLFetcherImplFactory() {}
content::URLFetcher* URLFetcherImplFactory::CreateURLFetcher(
int id,
const GURL& url,
content::URLFetcher::RequestType request_type,
content::URLFetcherDelegate* d) {
return new URLFetcherImpl(url, request_type, d);
}
|