Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: content/test/test_url_fetcher_factory.h

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

Powered by Google App Engine
This is Rietveld 408576698