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.
5 #include "net/url_request/test_url_fetcher_factory.h"
10 #include "base/compiler_specific.h"
11 #include "base/files/file_util.h"
12 #include "base/location.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "net/base/host_port_pair.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/upload_data_stream.h"
21 #include "net/http/http_response_headers.h"
22 #include "net/url_request/url_fetcher_delegate.h"
23 #include "net/url_request/url_fetcher_impl.h"
24 #include "net/url_request/url_fetcher_response_writer.h"
25 #include "net/url_request/url_request_status.h"
29 ScopedURLFetcherFactory::ScopedURLFetcherFactory(
30 URLFetcherFactory
* factory
) {
31 DCHECK(!URLFetcherImpl::factory());
32 URLFetcherImpl::set_factory(factory
);
35 ScopedURLFetcherFactory::~ScopedURLFetcherFactory() {
36 DCHECK(URLFetcherImpl::factory());
37 URLFetcherImpl::set_factory(NULL
);
40 TestURLFetcher::TestURLFetcher(int id
,
42 URLFetcherDelegate
* d
)
47 delegate_for_tests_(NULL
),
48 did_receive_last_chunk_(false),
50 fake_response_code_(-1),
51 fake_response_destination_(STRING
),
52 fake_was_fetched_via_proxy_(false),
53 fake_max_retries_(0) {
54 CHECK(original_url_
.is_valid());
57 TestURLFetcher::~TestURLFetcher() {
58 if (delegate_for_tests_
)
59 delegate_for_tests_
->OnRequestEnd(id_
);
61 owner_
->RemoveFetcherFromMap(id_
);
64 void TestURLFetcher::SetUploadData(const std::string
& upload_content_type
,
65 const std::string
& upload_content
) {
66 upload_content_type_
= upload_content_type
;
67 upload_data_
= upload_content
;
70 void TestURLFetcher::SetUploadFilePath(
71 const std::string
& upload_content_type
,
72 const base::FilePath
& file_path
,
75 scoped_refptr
<base::TaskRunner
> file_task_runner
) {
76 upload_file_path_
= file_path
;
79 void TestURLFetcher::SetUploadStreamFactory(
80 const std::string
& upload_content_type
,
81 const CreateUploadStreamCallback
& factory
) {
84 void TestURLFetcher::SetChunkedUpload(const std::string
& upload_content_type
) {
87 void TestURLFetcher::AppendChunkToUpload(const std::string
& data
,
89 DCHECK(!did_receive_last_chunk_
);
90 did_receive_last_chunk_
= is_last_chunk
;
91 chunks_
.push_back(data
);
92 if (delegate_for_tests_
)
93 delegate_for_tests_
->OnChunkUpload(id_
);
96 void TestURLFetcher::SetLoadFlags(int load_flags
) {
97 fake_load_flags_
= load_flags
;
100 int TestURLFetcher::GetLoadFlags() const {
101 return fake_load_flags_
;
104 void TestURLFetcher::SetReferrer(const std::string
& referrer
) {
107 void TestURLFetcher::SetReferrerPolicy(
108 URLRequest::ReferrerPolicy referrer_policy
) {
111 void TestURLFetcher::SetExtraRequestHeaders(
112 const std::string
& extra_request_headers
) {
113 fake_extra_request_headers_
.Clear();
114 fake_extra_request_headers_
.AddHeadersFromString(extra_request_headers
);
117 void TestURLFetcher::AddExtraRequestHeader(const std::string
& header_line
) {
118 fake_extra_request_headers_
.AddHeaderFromString(header_line
);
121 void TestURLFetcher::SetRequestContext(
122 URLRequestContextGetter
* request_context_getter
) {
125 void TestURLFetcher::SetFirstPartyForCookies(
126 const GURL
& first_party_for_cookies
) {
129 void TestURLFetcher::SetURLRequestUserData(
131 const CreateDataCallback
& create_data_callback
) {
134 void TestURLFetcher::SetStopOnRedirect(bool stop_on_redirect
) {
137 void TestURLFetcher::SetAutomaticallyRetryOn5xx(bool retry
) {
140 void TestURLFetcher::SetMaxRetriesOn5xx(int max_retries
) {
141 fake_max_retries_
= max_retries
;
144 int TestURLFetcher::GetMaxRetriesOn5xx() const {
145 return fake_max_retries_
;
148 base::TimeDelta
TestURLFetcher::GetBackoffDelay() const {
149 return fake_backoff_delay_
;
152 void TestURLFetcher::SetAutomaticallyRetryOnNetworkChanges(int max_retries
) {
155 void TestURLFetcher::SaveResponseToFileAtPath(
156 const base::FilePath
& file_path
,
157 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner
) {
158 SetResponseFilePath(file_path
);
159 // Asynchronous IO is not supported, so file_task_runner is ignored.
160 base::ThreadRestrictions::ScopedAllowIO allow_io
;
161 const size_t written_bytes
= base::WriteFile(
162 file_path
, fake_response_string_
.c_str(), fake_response_string_
.size());
163 DCHECK_EQ(written_bytes
, fake_response_string_
.size());
166 void TestURLFetcher::SaveResponseToTemporaryFile(
167 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner
) {
170 void TestURLFetcher::SaveResponseWithWriter(
171 scoped_ptr
<URLFetcherResponseWriter
> response_writer
) {
172 // In class URLFetcherCore this method is called by all three:
173 // GetResponseAsString() / SaveResponseToFileAtPath() /
174 // SaveResponseToTemporaryFile(). But here (in TestURLFetcher), this method
175 // is never used by any of these three methods. So, file writing is expected
176 // to be done in SaveResponseToFileAtPath(), and this method supports only
177 // URLFetcherStringWriter (for testing of this method only).
178 if (fake_response_destination_
== STRING
) {
179 response_writer_
= response_writer
.Pass();
180 int response
= response_writer_
->Initialize(CompletionCallback());
181 // The TestURLFetcher doesn't handle asynchronous writes.
182 DCHECK_EQ(OK
, response
);
184 scoped_refptr
<IOBuffer
> buffer(new StringIOBuffer(fake_response_string_
));
185 response
= response_writer_
->Write(buffer
.get(),
186 fake_response_string_
.size(),
187 CompletionCallback());
188 DCHECK_EQ(static_cast<int>(fake_response_string_
.size()), response
);
189 response
= response_writer_
->Finish(CompletionCallback());
190 DCHECK_EQ(OK
, response
);
191 } else if (fake_response_destination_
== TEMP_FILE
) {
192 // SaveResponseToFileAtPath() should be called instead of this method to
193 // save file. Asynchronous file writing using URLFetcherFileWriter is not
201 HttpResponseHeaders
* TestURLFetcher::GetResponseHeaders() const {
202 return fake_response_headers_
.get();
205 HostPortPair
TestURLFetcher::GetSocketAddress() const {
207 return HostPortPair();
210 bool TestURLFetcher::WasFetchedViaProxy() const {
211 return fake_was_fetched_via_proxy_
;
214 void TestURLFetcher::Start() {
215 // Overriden to do nothing. It is assumed the caller will notify the delegate.
216 if (delegate_for_tests_
)
217 delegate_for_tests_
->OnRequestStart(id_
);
220 const GURL
& TestURLFetcher::GetOriginalURL() const {
221 return original_url_
;
224 const GURL
& TestURLFetcher::GetURL() const {
228 const URLRequestStatus
& TestURLFetcher::GetStatus() const {
232 int TestURLFetcher::GetResponseCode() const {
233 return fake_response_code_
;
236 const ResponseCookies
& TestURLFetcher::GetCookies() const {
237 return fake_cookies_
;
240 void TestURLFetcher::ReceivedContentWasMalformed() {
243 bool TestURLFetcher::GetResponseAsString(
244 std::string
* out_response_string
) const {
245 if (fake_response_destination_
!= STRING
)
248 *out_response_string
= fake_response_string_
;
252 bool TestURLFetcher::GetResponseAsFilePath(
253 bool take_ownership
, base::FilePath
* out_response_path
) const {
254 if (fake_response_destination_
!= TEMP_FILE
)
257 *out_response_path
= fake_response_file_path_
;
261 void TestURLFetcher::GetExtraRequestHeaders(
262 HttpRequestHeaders
* headers
) const {
263 *headers
= fake_extra_request_headers_
;
266 void TestURLFetcher::set_status(const URLRequestStatus
& status
) {
267 fake_status_
= status
;
270 void TestURLFetcher::set_was_fetched_via_proxy(bool flag
) {
271 fake_was_fetched_via_proxy_
= flag
;
274 void TestURLFetcher::set_response_headers(
275 scoped_refptr
<HttpResponseHeaders
> headers
) {
276 fake_response_headers_
= headers
;
279 void TestURLFetcher::set_backoff_delay(base::TimeDelta backoff_delay
) {
280 fake_backoff_delay_
= backoff_delay
;
283 void TestURLFetcher::SetDelegateForTests(DelegateForTests
* delegate_for_tests
) {
284 delegate_for_tests_
= delegate_for_tests
;
287 void TestURLFetcher::SetResponseString(const std::string
& response
) {
288 fake_response_destination_
= STRING
;
289 fake_response_string_
= response
;
292 void TestURLFetcher::SetResponseFilePath(const base::FilePath
& path
) {
293 fake_response_destination_
= TEMP_FILE
;
294 fake_response_file_path_
= path
;
297 TestURLFetcherFactory::TestURLFetcherFactory()
298 : ScopedURLFetcherFactory(this),
299 delegate_for_tests_(NULL
),
300 remove_fetcher_on_delete_(false) {
303 TestURLFetcherFactory::~TestURLFetcherFactory() {}
305 scoped_ptr
<URLFetcher
> TestURLFetcherFactory::CreateURLFetcher(
308 URLFetcher::RequestType request_type
,
309 URLFetcherDelegate
* d
) {
310 TestURLFetcher
* fetcher
= new TestURLFetcher(id
, url
, d
);
311 if (remove_fetcher_on_delete_
)
312 fetcher
->set_owner(this);
313 fetcher
->SetDelegateForTests(delegate_for_tests_
);
314 fetchers_
[id
] = fetcher
;
315 return scoped_ptr
<URLFetcher
>(fetcher
);
318 TestURLFetcher
* TestURLFetcherFactory::GetFetcherByID(int id
) const {
319 Fetchers::const_iterator i
= fetchers_
.find(id
);
320 return i
== fetchers_
.end() ? NULL
: i
->second
;
323 void TestURLFetcherFactory::RemoveFetcherFromMap(int id
) {
324 Fetchers::iterator i
= fetchers_
.find(id
);
325 DCHECK(i
!= fetchers_
.end());
329 void TestURLFetcherFactory::SetDelegateForTests(
330 TestURLFetcherDelegateForTests
* delegate_for_tests
) {
331 delegate_for_tests_
= delegate_for_tests
;
334 FakeURLFetcher::FakeURLFetcher(const GURL
& url
,
335 URLFetcherDelegate
* d
,
336 const std::string
& response_data
,
337 HttpStatusCode response_code
,
338 URLRequestStatus::Status status
)
339 : TestURLFetcher(0, url
, d
),
340 weak_factory_(this) {
343 case URLRequestStatus::SUCCESS
:
344 // |error| is initialized to OK.
346 case URLRequestStatus::IO_PENDING
:
347 error
= ERR_IO_PENDING
;
349 case URLRequestStatus::CANCELED
:
352 case URLRequestStatus::FAILED
:
356 set_status(URLRequestStatus(status
, error
));
357 set_response_code(response_code
);
358 SetResponseString(response_data
);
359 response_bytes_
= response_data
.size();
362 FakeURLFetcher::~FakeURLFetcher() {}
364 void FakeURLFetcher::Start() {
365 base::ThreadTaskRunnerHandle::Get()->PostTask(
367 base::Bind(&FakeURLFetcher::RunDelegate
, weak_factory_
.GetWeakPtr()));
370 void FakeURLFetcher::RunDelegate() {
371 // OnURLFetchDownloadProgress may delete this URLFetcher. We keep track of
372 // this with a weak pointer, and only call OnURLFetchComplete if this still
374 auto weak_this
= weak_factory_
.GetWeakPtr();
375 delegate()->OnURLFetchDownloadProgress(this, response_bytes_
,
378 delegate()->OnURLFetchComplete(this);
381 const GURL
& FakeURLFetcher::GetURL() const {
382 return TestURLFetcher::GetOriginalURL();
385 FakeURLFetcherFactory::FakeURLFetcherFactory(
386 URLFetcherFactory
* default_factory
)
387 : ScopedURLFetcherFactory(this),
388 creator_(base::Bind(&DefaultFakeURLFetcherCreator
)),
389 default_factory_(default_factory
) {
392 FakeURLFetcherFactory::FakeURLFetcherFactory(
393 URLFetcherFactory
* default_factory
,
394 const FakeURLFetcherCreator
& creator
)
395 : ScopedURLFetcherFactory(this),
397 default_factory_(default_factory
) {
400 scoped_ptr
<FakeURLFetcher
> FakeURLFetcherFactory::DefaultFakeURLFetcherCreator(
402 URLFetcherDelegate
* delegate
,
403 const std::string
& response_data
,
404 HttpStatusCode response_code
,
405 URLRequestStatus::Status status
) {
406 return scoped_ptr
<FakeURLFetcher
>(
407 new FakeURLFetcher(url
, delegate
, response_data
, response_code
, status
));
410 FakeURLFetcherFactory::~FakeURLFetcherFactory() {}
412 scoped_ptr
<URLFetcher
> FakeURLFetcherFactory::CreateURLFetcher(
415 URLFetcher::RequestType request_type
,
416 URLFetcherDelegate
* d
) {
417 FakeResponseMap::const_iterator it
= fake_responses_
.find(url
);
418 if (it
== fake_responses_
.end()) {
419 if (default_factory_
== NULL
) {
420 // If we don't have a baked response for that URL we return NULL.
421 DLOG(ERROR
) << "No baked response for URL: " << url
.spec();
424 return default_factory_
->CreateURLFetcher(id
, url
, request_type
, d
);
428 scoped_ptr
<URLFetcher
> fake_fetcher
=
429 creator_
.Run(url
, d
, it
->second
.response_data
, it
->second
.response_code
,
434 void FakeURLFetcherFactory::SetFakeResponse(
436 const std::string
& response_data
,
437 HttpStatusCode response_code
,
438 URLRequestStatus::Status status
) {
439 // Overwrite existing URL if it already exists.
440 FakeURLResponse response
;
441 response
.response_data
= response_data
;
442 response
.response_code
= response_code
;
443 response
.status
= status
;
444 fake_responses_
[url
] = response
;
447 void FakeURLFetcherFactory::ClearFakeResponses() {
448 fake_responses_
.clear();
451 URLFetcherImplFactory::URLFetcherImplFactory() {}
453 URLFetcherImplFactory::~URLFetcherImplFactory() {}
455 scoped_ptr
<URLFetcher
> URLFetcherImplFactory::CreateURLFetcher(
458 URLFetcher::RequestType request_type
,
459 URLFetcherDelegate
* d
) {
460 return scoped_ptr
<URLFetcher
>(new URLFetcherImpl(url
, request_type
, d
));