OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/public/test/test_url_fetcher_factory.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/compiler_specific.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/message_loop.h" | |
13 #include "net/base/host_port_pair.h" | |
14 #include "net/http/http_response_headers.h" | |
15 #include "net/url_request/url_fetcher_delegate.h" | |
16 #include "net/url_request/url_fetcher_impl.h" | |
17 #include "net/url_request/url_request_status.h" | |
18 | |
19 ScopedURLFetcherFactory::ScopedURLFetcherFactory( | |
20 net::URLFetcherFactory* factory) { | |
21 DCHECK(!net::URLFetcherImpl::factory()); | |
22 net::URLFetcherImpl::set_factory(factory); | |
23 } | |
24 | |
25 ScopedURLFetcherFactory::~ScopedURLFetcherFactory() { | |
26 DCHECK(net::URLFetcherImpl::factory()); | |
27 net::URLFetcherImpl::set_factory(NULL); | |
28 } | |
29 | |
30 TestURLFetcher::TestURLFetcher(int id, | |
31 const GURL& url, | |
32 net::URLFetcherDelegate* d) | |
33 : id_(id), | |
34 original_url_(url), | |
35 delegate_(d), | |
36 did_receive_last_chunk_(false), | |
37 fake_load_flags_(0), | |
38 fake_response_code_(-1), | |
39 fake_response_destination_(STRING), | |
40 fake_was_fetched_via_proxy_(false), | |
41 fake_max_retries_(0) { | |
42 } | |
43 | |
44 TestURLFetcher::~TestURLFetcher() { | |
45 } | |
46 | |
47 void TestURLFetcher::SetUploadData(const std::string& upload_content_type, | |
48 const std::string& upload_content) { | |
49 upload_data_ = upload_content; | |
50 } | |
51 | |
52 void TestURLFetcher::SetChunkedUpload(const std::string& upload_content_type) { | |
53 } | |
54 | |
55 void TestURLFetcher::AppendChunkToUpload(const std::string& data, | |
56 bool is_last_chunk) { | |
57 DCHECK(!did_receive_last_chunk_); | |
58 did_receive_last_chunk_ = is_last_chunk; | |
59 chunks_.push_back(data); | |
60 } | |
61 | |
62 void TestURLFetcher::SetLoadFlags(int load_flags) { | |
63 fake_load_flags_= load_flags; | |
64 } | |
65 | |
66 int TestURLFetcher::GetLoadFlags() const { | |
67 return fake_load_flags_; | |
68 } | |
69 | |
70 void TestURLFetcher::SetReferrer(const std::string& referrer) { | |
71 } | |
72 | |
73 void TestURLFetcher::SetExtraRequestHeaders( | |
74 const std::string& extra_request_headers) { | |
75 fake_extra_request_headers_.Clear(); | |
76 fake_extra_request_headers_.AddHeadersFromString(extra_request_headers); | |
77 } | |
78 | |
79 void TestURLFetcher::AddExtraRequestHeader(const std::string& header_line) { | |
80 fake_extra_request_headers_.AddHeaderFromString(header_line); | |
81 } | |
82 | |
83 void TestURLFetcher::GetExtraRequestHeaders( | |
84 net::HttpRequestHeaders* headers) const { | |
85 *headers = fake_extra_request_headers_; | |
86 } | |
87 | |
88 void TestURLFetcher::SetRequestContext( | |
89 net::URLRequestContextGetter* request_context_getter) { | |
90 } | |
91 | |
92 void TestURLFetcher::SetFirstPartyForCookies( | |
93 const GURL& first_party_for_cookies) { | |
94 } | |
95 | |
96 void TestURLFetcher::SetURLRequestUserData( | |
97 const void* key, | |
98 const CreateDataCallback& create_data_callback) { | |
99 } | |
100 | |
101 void TestURLFetcher::SetStopOnRedirect(bool stop_on_redirect) { | |
102 } | |
103 | |
104 void TestURLFetcher::SetAutomaticallyRetryOn5xx(bool retry) { | |
105 } | |
106 | |
107 void TestURLFetcher::SetMaxRetries(int max_retries) { | |
108 fake_max_retries_ = max_retries; | |
109 } | |
110 | |
111 int TestURLFetcher::GetMaxRetries() const { | |
112 return fake_max_retries_; | |
113 } | |
114 | |
115 base::TimeDelta TestURLFetcher::GetBackoffDelay() const { | |
116 return fake_backoff_delay_; | |
117 } | |
118 | |
119 void TestURLFetcher::SaveResponseToFileAtPath( | |
120 const FilePath& file_path, | |
121 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { | |
122 } | |
123 | |
124 void TestURLFetcher::SaveResponseToTemporaryFile( | |
125 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { | |
126 } | |
127 | |
128 net::HttpResponseHeaders* TestURLFetcher::GetResponseHeaders() const { | |
129 return fake_response_headers_; | |
130 } | |
131 | |
132 net::HostPortPair TestURLFetcher::GetSocketAddress() const { | |
133 NOTIMPLEMENTED(); | |
134 return net::HostPortPair(); | |
135 } | |
136 | |
137 bool TestURLFetcher::WasFetchedViaProxy() const { | |
138 return fake_was_fetched_via_proxy_; | |
139 } | |
140 | |
141 void TestURLFetcher::Start() { | |
142 // Overriden to do nothing. It is assumed the caller will notify the delegate. | |
143 } | |
144 | |
145 const GURL& TestURLFetcher::GetOriginalURL() const { | |
146 return original_url_; | |
147 } | |
148 | |
149 const GURL& TestURLFetcher::GetURL() const { | |
150 return fake_url_; | |
151 } | |
152 | |
153 const net::URLRequestStatus& TestURLFetcher::GetStatus() const { | |
154 return fake_status_; | |
155 } | |
156 | |
157 int TestURLFetcher::GetResponseCode() const { | |
158 return fake_response_code_; | |
159 } | |
160 | |
161 const net::ResponseCookies& TestURLFetcher::GetCookies() const { | |
162 return fake_cookies_; | |
163 } | |
164 | |
165 bool TestURLFetcher::FileErrorOccurred( | |
166 base::PlatformFileError* out_error_code) const { | |
167 NOTIMPLEMENTED(); | |
168 return false; | |
169 } | |
170 | |
171 void TestURLFetcher::ReceivedContentWasMalformed() { | |
172 } | |
173 | |
174 bool TestURLFetcher::GetResponseAsString( | |
175 std::string* out_response_string) const { | |
176 if (fake_response_destination_ != STRING) | |
177 return false; | |
178 | |
179 *out_response_string = fake_response_string_; | |
180 return true; | |
181 } | |
182 | |
183 bool TestURLFetcher::GetResponseAsFilePath( | |
184 bool take_ownership, FilePath* out_response_path) const { | |
185 if (fake_response_destination_ != TEMP_FILE) | |
186 return false; | |
187 | |
188 *out_response_path = fake_response_file_path_; | |
189 return true; | |
190 } | |
191 | |
192 void TestURLFetcher::set_status(const net::URLRequestStatus& status) { | |
193 fake_status_ = status; | |
194 } | |
195 | |
196 void TestURLFetcher::set_was_fetched_via_proxy(bool flag) { | |
197 fake_was_fetched_via_proxy_ = flag; | |
198 } | |
199 | |
200 void TestURLFetcher::set_response_headers( | |
201 scoped_refptr<net::HttpResponseHeaders> headers) { | |
202 fake_response_headers_ = headers; | |
203 } | |
204 | |
205 void TestURLFetcher::set_backoff_delay(base::TimeDelta backoff_delay) { | |
206 fake_backoff_delay_ = backoff_delay; | |
207 } | |
208 | |
209 void TestURLFetcher::SetResponseString(const std::string& response) { | |
210 fake_response_destination_ = STRING; | |
211 fake_response_string_ = response; | |
212 } | |
213 | |
214 void TestURLFetcher::SetResponseFilePath(const FilePath& path) { | |
215 fake_response_destination_ = TEMP_FILE; | |
216 fake_response_file_path_ = path; | |
217 } | |
218 | |
219 TestURLFetcherFactory::TestURLFetcherFactory() | |
220 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
221 } | |
222 | |
223 TestURLFetcherFactory::~TestURLFetcherFactory() {} | |
224 | |
225 net::URLFetcher* TestURLFetcherFactory::CreateURLFetcher( | |
226 int id, | |
227 const GURL& url, | |
228 net::URLFetcher::RequestType request_type, | |
229 net::URLFetcherDelegate* d) { | |
230 TestURLFetcher* fetcher = new TestURLFetcher(id, url, d); | |
231 fetchers_[id] = fetcher; | |
232 return fetcher; | |
233 } | |
234 | |
235 TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const { | |
236 Fetchers::const_iterator i = fetchers_.find(id); | |
237 return i == fetchers_.end() ? NULL : i->second; | |
238 } | |
239 | |
240 void TestURLFetcherFactory::RemoveFetcherFromMap(int id) { | |
241 Fetchers::iterator i = fetchers_.find(id); | |
242 DCHECK(i != fetchers_.end()); | |
243 fetchers_.erase(i); | |
244 } | |
245 | |
246 // This class is used by the FakeURLFetcherFactory below. | |
247 class FakeURLFetcher : public TestURLFetcher { | |
248 public: | |
249 // Normal URL fetcher constructor but also takes in a pre-baked response. | |
250 FakeURLFetcher(const GURL& url, | |
251 net::URLFetcherDelegate* d, | |
252 const std::string& response_data, bool success) | |
253 : TestURLFetcher(0, url, d), | |
254 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
255 set_status(net::URLRequestStatus( | |
256 success ? net::URLRequestStatus::SUCCESS : | |
257 net::URLRequestStatus::FAILED, | |
258 0)); | |
259 set_response_code(success ? 200 : 500); | |
260 SetResponseString(response_data); | |
261 } | |
262 | |
263 // Start the request. This will call the given delegate asynchronously | |
264 // with the pre-baked response as parameter. | |
265 virtual void Start() OVERRIDE { | |
266 MessageLoop::current()->PostTask( | |
267 FROM_HERE, | |
268 base::Bind(&FakeURLFetcher::RunDelegate, weak_factory_.GetWeakPtr())); | |
269 } | |
270 | |
271 virtual const GURL& GetURL() const OVERRIDE { | |
272 return TestURLFetcher::GetOriginalURL(); | |
273 } | |
274 | |
275 private: | |
276 virtual ~FakeURLFetcher() { | |
277 } | |
278 | |
279 // This is the method which actually calls the delegate that is passed in the | |
280 // constructor. | |
281 void RunDelegate() { | |
282 delegate()->OnURLFetchComplete(this); | |
283 } | |
284 | |
285 base::WeakPtrFactory<FakeURLFetcher> weak_factory_; | |
286 | |
287 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); | |
288 }; | |
289 | |
290 FakeURLFetcherFactory::FakeURLFetcherFactory() | |
291 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
292 default_factory_(NULL) { | |
293 } | |
294 | |
295 FakeURLFetcherFactory::FakeURLFetcherFactory( | |
296 net::URLFetcherFactory* default_factory) | |
297 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
298 default_factory_(default_factory) { | |
299 } | |
300 | |
301 FakeURLFetcherFactory::~FakeURLFetcherFactory() {} | |
302 | |
303 net::URLFetcher* FakeURLFetcherFactory::CreateURLFetcher( | |
304 int id, | |
305 const GURL& url, | |
306 net::URLFetcher::RequestType request_type, | |
307 net::URLFetcherDelegate* d) { | |
308 FakeResponseMap::const_iterator it = fake_responses_.find(url); | |
309 if (it == fake_responses_.end()) { | |
310 if (default_factory_ == NULL) { | |
311 // If we don't have a baked response for that URL we return NULL. | |
312 DLOG(ERROR) << "No baked response for URL: " << url.spec(); | |
313 return NULL; | |
314 } else { | |
315 return default_factory_->CreateURLFetcher(id, url, request_type, d); | |
316 } | |
317 } | |
318 return new FakeURLFetcher(url, d, it->second.first, it->second.second); | |
319 } | |
320 | |
321 void FakeURLFetcherFactory::SetFakeResponse(const std::string& url, | |
322 const std::string& response_data, | |
323 bool success) { | |
324 // Overwrite existing URL if it already exists. | |
325 fake_responses_[GURL(url)] = std::make_pair(response_data, success); | |
326 } | |
327 | |
328 void FakeURLFetcherFactory::ClearFakeResponses() { | |
329 fake_responses_.clear(); | |
330 } | |
331 | |
332 URLFetcherImplFactory::URLFetcherImplFactory() {} | |
333 | |
334 URLFetcherImplFactory::~URLFetcherImplFactory() {} | |
335 | |
336 net::URLFetcher* URLFetcherImplFactory::CreateURLFetcher( | |
337 int id, | |
338 const GURL& url, | |
339 net::URLFetcher::RequestType request_type, | |
340 net::URLFetcherDelegate* d) { | |
341 return new net::URLFetcherImpl(url, request_type, d); | |
342 } | |
OLD | NEW |