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 #ifndef CONTENT_PUBLIC_TEST_TEST_URL_FETCHER_FACTORY_H_ | |
6 #define CONTENT_PUBLIC_TEST_TEST_URL_FETCHER_FACTORY_H_ | |
7 #pragma once | |
8 | |
9 // TODO(akalin): Move this to net/. | |
10 | |
11 #include <list> | |
12 #include <map> | |
13 #include <string> | |
14 #include <utility> | |
15 | |
16 #include "base/threading/non_thread_safe.h" | |
17 #include "googleurl/src/gurl.h" | |
18 #include "net/http/http_request_headers.h" | |
19 #include "net/url_request/url_fetcher_factory.h" | |
20 #include "net/url_request/url_request_status.h" | |
21 | |
22 // Changes URLFetcher's Factory for the lifetime of the object. | |
23 // Note that this scoper cannot be nested (to make it even harder to misuse). | |
24 class ScopedURLFetcherFactory : public base::NonThreadSafe { | |
25 public: | |
26 explicit ScopedURLFetcherFactory(net::URLFetcherFactory* factory); | |
27 virtual ~ScopedURLFetcherFactory(); | |
28 | |
29 private: | |
30 DISALLOW_COPY_AND_ASSIGN(ScopedURLFetcherFactory); | |
31 }; | |
32 | |
33 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of | |
34 // URLFetcher. TestURLFetcherFactory is a URLFetcherFactory that creates | |
35 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is | |
36 // expected that you'll grab the delegate from the TestURLFetcher and invoke | |
37 // the callback method when appropriate. In this way it's easy to mock a | |
38 // URLFetcher. | |
39 // Typical usage: | |
40 // // TestURLFetcher requires a MessageLoop. | |
41 // MessageLoop message_loop; | |
42 // // And an IO thread to release URLRequestContextGetter in URLFetcher::Core. | |
43 // BrowserThreadImpl io_thread(BrowserThread::IO, &message_loop); | |
44 // // Create factory (it automatically sets itself as URLFetcher's factory). | |
45 // TestURLFetcherFactory factory; | |
46 // // Do something that triggers creation of a URLFetcher. | |
47 // ... | |
48 // TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id); | |
49 // DCHECK(fetcher); | |
50 // // Notify delegate with whatever data you want. | |
51 // fetcher->delegate()->OnURLFetchComplete(...); | |
52 // // Make sure consumer of URLFetcher does the right thing. | |
53 // ... | |
54 // | |
55 // Note: if you don't know when your request objects will be created you | |
56 // might want to use the FakeURLFetcher and FakeURLFetcherFactory classes | |
57 // below. | |
58 | |
59 class TestURLFetcher : public net::URLFetcher { | |
60 public: | |
61 TestURLFetcher(int id, | |
62 const GURL& url, | |
63 net::URLFetcherDelegate* d); | |
64 virtual ~TestURLFetcher(); | |
65 | |
66 // net::URLFetcher implementation | |
67 virtual void SetUploadData(const std::string& upload_content_type, | |
68 const std::string& upload_content) OVERRIDE; | |
69 virtual void SetChunkedUpload( | |
70 const std::string& upload_content_type) OVERRIDE; | |
71 // Overriden to cache the chunks uploaded. Caller can read back the uploaded | |
72 // chunks with the upload_chunks() accessor. | |
73 virtual void AppendChunkToUpload(const std::string& data, | |
74 bool is_last_chunk) OVERRIDE; | |
75 virtual void SetLoadFlags(int load_flags) OVERRIDE; | |
76 virtual int GetLoadFlags() const OVERRIDE; | |
77 virtual void SetReferrer(const std::string& referrer) OVERRIDE; | |
78 virtual void SetExtraRequestHeaders( | |
79 const std::string& extra_request_headers) OVERRIDE; | |
80 virtual void AddExtraRequestHeader(const std::string& header_line) OVERRIDE; | |
81 virtual void GetExtraRequestHeaders( | |
82 net::HttpRequestHeaders* headers) const OVERRIDE; | |
83 virtual void SetRequestContext( | |
84 net::URLRequestContextGetter* request_context_getter) OVERRIDE; | |
85 virtual void SetFirstPartyForCookies( | |
86 const GURL& first_party_for_cookies) OVERRIDE; | |
87 virtual void SetURLRequestUserData( | |
88 const void* key, | |
89 const CreateDataCallback& create_data_callback) OVERRIDE; | |
90 virtual void SetStopOnRedirect(bool stop_on_redirect) OVERRIDE; | |
91 virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE; | |
92 virtual void SetMaxRetries(int max_retries) OVERRIDE; | |
93 virtual int GetMaxRetries() const OVERRIDE; | |
94 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE; | |
95 virtual void SaveResponseToFileAtPath( | |
96 const FilePath& file_path, | |
97 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; | |
98 virtual void SaveResponseToTemporaryFile( | |
99 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; | |
100 virtual net::HttpResponseHeaders* GetResponseHeaders() const OVERRIDE; | |
101 virtual net::HostPortPair GetSocketAddress() const OVERRIDE; | |
102 virtual bool WasFetchedViaProxy() const OVERRIDE; | |
103 virtual void Start() OVERRIDE; | |
104 | |
105 // URL we were created with. Because of how we're using URLFetcher GetURL() | |
106 // always returns an empty URL. Chances are you'll want to use | |
107 // GetOriginalURL() in your tests. | |
108 virtual const GURL& GetOriginalURL() const OVERRIDE; | |
109 virtual const GURL& GetURL() const OVERRIDE; | |
110 virtual const net::URLRequestStatus& GetStatus() const OVERRIDE; | |
111 virtual int GetResponseCode() const OVERRIDE; | |
112 virtual const net::ResponseCookies& GetCookies() const OVERRIDE; | |
113 virtual bool FileErrorOccurred( | |
114 base::PlatformFileError* out_error_code) const OVERRIDE; | |
115 virtual void ReceivedContentWasMalformed() OVERRIDE; | |
116 // Override response access functions to return fake data. | |
117 virtual bool GetResponseAsString( | |
118 std::string* out_response_string) const OVERRIDE; | |
119 virtual bool GetResponseAsFilePath( | |
120 bool take_ownership, FilePath* out_response_path) const OVERRIDE; | |
121 | |
122 // Unique ID in our factory. | |
123 int id() const { return id_; } | |
124 | |
125 // Returns the data uploaded on this URLFetcher. | |
126 const std::string& upload_data() const { return upload_data_; } | |
127 | |
128 // Returns the chunks of data uploaded on this URLFetcher. | |
129 const std::list<std::string>& upload_chunks() const { return chunks_; } | |
130 | |
131 // Returns the delegate installed on the URLFetcher. | |
132 net::URLFetcherDelegate* delegate() const { return delegate_; } | |
133 | |
134 void set_url(const GURL& url) { fake_url_ = url; } | |
135 void set_status(const net::URLRequestStatus& status); | |
136 void set_response_code(int response_code) { | |
137 fake_response_code_ = response_code; | |
138 } | |
139 void set_cookies(const net::ResponseCookies& c) { fake_cookies_ = c; } | |
140 void set_was_fetched_via_proxy(bool flag); | |
141 void set_response_headers(scoped_refptr<net::HttpResponseHeaders> headers); | |
142 void set_backoff_delay(base::TimeDelta backoff_delay); | |
143 | |
144 // Set string data. | |
145 void SetResponseString(const std::string& response); | |
146 | |
147 // Set File data. | |
148 void SetResponseFilePath(const FilePath& path); | |
149 | |
150 private: | |
151 enum ResponseDestinationType { | |
152 STRING, // Default: In a std::string | |
153 TEMP_FILE // Write to a temp file | |
154 }; | |
155 | |
156 const int id_; | |
157 const GURL original_url_; | |
158 net::URLFetcherDelegate* delegate_; | |
159 std::string upload_data_; | |
160 std::list<std::string> chunks_; | |
161 bool did_receive_last_chunk_; | |
162 | |
163 // User can use set_* methods to provide values returned by getters. | |
164 // Setting the real values is not possible, because the real class | |
165 // has no setters. The data is a private member of a class defined | |
166 // in a .cc file, so we can't get at it with friendship. | |
167 int fake_load_flags_; | |
168 GURL fake_url_; | |
169 net::URLRequestStatus fake_status_; | |
170 int fake_response_code_; | |
171 net::ResponseCookies fake_cookies_; | |
172 ResponseDestinationType fake_response_destination_; | |
173 std::string fake_response_string_; | |
174 FilePath fake_response_file_path_; | |
175 bool fake_was_fetched_via_proxy_; | |
176 scoped_refptr<net::HttpResponseHeaders> fake_response_headers_; | |
177 net::HttpRequestHeaders fake_extra_request_headers_; | |
178 int fake_max_retries_; | |
179 base::TimeDelta fake_backoff_delay_; | |
180 | |
181 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); | |
182 }; | |
183 | |
184 // Simple URLFetcherFactory method that creates TestURLFetchers. All fetchers | |
185 // are registered in a map by the id passed to the create method. | |
186 class TestURLFetcherFactory : public net::URLFetcherFactory, | |
187 public ScopedURLFetcherFactory { | |
188 public: | |
189 TestURLFetcherFactory(); | |
190 virtual ~TestURLFetcherFactory(); | |
191 | |
192 virtual net::URLFetcher* CreateURLFetcher( | |
193 int id, | |
194 const GURL& url, | |
195 net::URLFetcher::RequestType request_type, | |
196 net::URLFetcherDelegate* d) OVERRIDE; | |
197 TestURLFetcher* GetFetcherByID(int id) const; | |
198 void RemoveFetcherFromMap(int id); | |
199 | |
200 private: | |
201 // Maps from id passed to create to the returned URLFetcher. | |
202 typedef std::map<int, TestURLFetcher*> Fetchers; | |
203 Fetchers fetchers_; | |
204 | |
205 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); | |
206 }; | |
207 | |
208 // The FakeURLFetcher and FakeURLFetcherFactory classes are similar to the | |
209 // ones above but don't require you to know when exactly the URLFetcher objects | |
210 // will be created. | |
211 // | |
212 // These classes let you set pre-baked HTTP responses for particular URLs. | |
213 // E.g., if the user requests http://a.com/ then respond with an HTTP/500. | |
214 // | |
215 // We assume that the thread that is calling Start() on the URLFetcher object | |
216 // has a message loop running. | |
217 // | |
218 // This class is not thread-safe. You should not call SetFakeResponse or | |
219 // ClearFakeResponse at the same time you call CreateURLFetcher. However, it is | |
220 // OK to start URLFetcher objects while setting or clearning fake responses | |
221 // since already created URLFetcher objects will not be affected by any changes | |
222 // made to the fake responses (once a URLFetcher object is created you cannot | |
223 // change its fake response). | |
224 // | |
225 // Example usage: | |
226 // FakeURLFetcherFactory factory; | |
227 // | |
228 // // You know that class SomeService will request url http://a.com/ and you | |
229 // // want to test the service class by returning an error. | |
230 // factory.SetFakeResponse("http://a.com/", "", false); | |
231 // // But if the service requests http://b.com/asdf you want to respond with | |
232 // // a simple html page and an HTTP/200 code. | |
233 // factory.SetFakeResponse("http://b.com/asdf", | |
234 // "<html><body>hello world</body></html>", | |
235 // true); | |
236 // | |
237 // SomeService service; | |
238 // service.Run(); // Will eventually request these two URLs. | |
239 | |
240 class FakeURLFetcherFactory : public net::URLFetcherFactory, | |
241 public ScopedURLFetcherFactory { | |
242 public: | |
243 FakeURLFetcherFactory(); | |
244 // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown | |
245 // url to the given factory. | |
246 explicit FakeURLFetcherFactory(net::URLFetcherFactory* default_factory); | |
247 virtual ~FakeURLFetcherFactory(); | |
248 | |
249 // If no fake response is set for the given URL this method will delegate the | |
250 // call to |default_factory_| if it is not NULL, or return NULL if it is | |
251 // NULL. | |
252 // Otherwise, it will return a URLFetcher object which will respond with the | |
253 // pre-baked response that the client has set by calling SetFakeResponse(). | |
254 virtual net::URLFetcher* CreateURLFetcher( | |
255 int id, | |
256 const GURL& url, | |
257 net::URLFetcher::RequestType request_type, | |
258 net::URLFetcherDelegate* d) OVERRIDE; | |
259 | |
260 // Sets the fake response for a given URL. If success is true we will serve | |
261 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. | |
262 void SetFakeResponse(const std::string& url, | |
263 const std::string& response_data, | |
264 bool success); | |
265 | |
266 // Clear all the fake responses that were previously set via | |
267 // SetFakeResponse(). | |
268 void ClearFakeResponses(); | |
269 | |
270 private: | |
271 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; | |
272 FakeResponseMap fake_responses_; | |
273 net::URLFetcherFactory* default_factory_; | |
274 | |
275 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); | |
276 }; | |
277 | |
278 // This is an implementation of URLFetcherFactory that will create a | |
279 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in | |
280 // integration tests to control the behavior of some requests but execute | |
281 // all the other ones. | |
282 class URLFetcherImplFactory : public net::URLFetcherFactory { | |
283 public: | |
284 URLFetcherImplFactory(); | |
285 virtual ~URLFetcherImplFactory(); | |
286 | |
287 // This method will create a real URLFetcher. | |
288 virtual net::URLFetcher* CreateURLFetcher( | |
289 int id, | |
290 const GURL& url, | |
291 net::URLFetcher::RequestType request_type, | |
292 net::URLFetcherDelegate* d) OVERRIDE; | |
293 | |
294 }; | |
295 | |
296 #endif // CONTENT_PUBLIC_TEST_TEST_URL_FETCHER_FACTORY_H_ | |
OLD | NEW |