OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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_COMMON_TEST_URL_FETCHER_FACTORY_H_ | |
6 #define CONTENT_COMMON_TEST_URL_FETCHER_FACTORY_H_ | |
7 #pragma once | |
8 | |
9 #include <list> | |
10 #include <map> | |
11 #include <string> | |
12 #include <utility> | |
13 | |
14 #include "content/common/url_fetcher.h" | |
15 #include "net/url_request/url_request_status.h" | |
16 #include "googleurl/src/gurl.h" | |
17 | |
18 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of | |
19 // URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates | |
20 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is | |
21 // expected that you'll grab the delegate from the TestURLFetcher and invoke | |
22 // the callback method when appropriate. In this way it's easy to mock a | |
23 // URLFetcher. | |
24 // Typical usage: | |
25 // // TestURLFetcher requires a MessageLoop: | |
26 // MessageLoopForUI message_loop; | |
27 // // And io_thread to release URLRequestContextGetter in URLFetcher::Core. | |
28 // BrowserThread io_thread(BrowserThread::IO, &message_loop); | |
29 // // Create and register factory. | |
30 // TestURLFetcherFactory factory; | |
31 // URLFetcher::set_factory(&factory); | |
32 // // Do something that triggers creation of a URLFetcher. | |
33 // TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id); | |
34 // DCHECK(fetcher); | |
35 // // Notify delegate with whatever data you want. | |
36 // fetcher->delegate()->OnURLFetchComplete(...); | |
37 // // Make sure consumer of URLFetcher does the right thing. | |
38 // ... | |
39 // // Reset factory. | |
40 // URLFetcher::set_factory(NULL); | |
41 // | |
42 // Note: if you don't know when your request objects will be created you | |
43 // might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes | |
44 // below. | |
45 | |
46 class TestURLFetcher : public URLFetcher { | |
47 public: | |
48 TestURLFetcher(int id, | |
49 const GURL& url, | |
50 RequestType request_type, | |
51 Delegate* d); | |
52 virtual ~TestURLFetcher(); | |
53 | |
54 // Overriden to do nothing. It is assumed the caller will notify the delegate. | |
55 virtual void Start() {} | |
56 | |
57 // Overriden to cache the chunks uploaded. Caller can read back the uploaded | |
58 // chunks with the upload_data() accessor. | |
59 virtual void AppendChunkToUpload(const std::string& data, bool is_last_chunk); | |
60 | |
61 // Unique ID in our factory. | |
62 int id() const { return id_; } | |
63 | |
64 // URL we were created with. Because of how we're using URLFetcher url() | |
65 // always returns an empty URL. Chances are you'll want to use original_url() | |
66 // in your tests. | |
67 const GURL& original_url() const { return original_url_; } | |
68 | |
69 // Returns the data uploaded on this URLFetcher. | |
70 const std::string& upload_data() const { return URLFetcher::upload_data(); } | |
71 | |
72 // Returns the chunks of data uploaded on this URLFetcher. | |
73 const std::list<std::string>& upload_chunks() const { return chunks_; } | |
74 | |
75 // Returns the delegate installed on the URLFetcher. | |
76 Delegate* delegate() const { return URLFetcher::delegate(); } | |
77 | |
78 void set_url(const GURL& url) { fake_url_ = url; } | |
79 virtual const GURL& url() const; | |
80 | |
81 void set_status(const net::URLRequestStatus& status); | |
82 virtual const net::URLRequestStatus& status() const; | |
83 | |
84 void set_response_code(int response_code) { | |
85 fake_response_code_ = response_code; | |
86 } | |
87 virtual int response_code() const; | |
88 | |
89 // Set string data. | |
90 void SetResponseString(const std::string& response); | |
91 | |
92 // Set File data. | |
93 void SetResponseFilePath(const FilePath& path); | |
94 | |
95 // Override response access functions to return fake data. | |
96 virtual bool GetResponseAsString(std::string* out_response_string) const; | |
97 virtual bool GetResponseAsFilePath(bool take_ownership, | |
98 FilePath* out_response_path) const; | |
99 | |
100 private: | |
101 const int id_; | |
102 const GURL original_url_; | |
103 std::list<std::string> chunks_; | |
104 bool did_receive_last_chunk_; | |
105 | |
106 // User can use set_* methods to provide values returned by getters. | |
107 // Setting the real values is not possible, because the real class | |
108 // has no setters. The data is a private member of a class defined | |
109 // in a .cc file, so we can't get at it with friendship. | |
110 GURL fake_url_; | |
111 net::URLRequestStatus fake_status_; | |
112 int fake_response_code_; | |
113 std::string fake_response_string_; | |
114 FilePath fake_response_file_path_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); | |
117 }; | |
118 | |
119 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers | |
120 // are registered in a map by the id passed to the create method. | |
121 class TestURLFetcherFactory : public URLFetcher::Factory { | |
122 public: | |
123 TestURLFetcherFactory(); | |
124 virtual ~TestURLFetcherFactory(); | |
125 | |
126 virtual URLFetcher* CreateURLFetcher(int id, | |
127 const GURL& url, | |
128 URLFetcher::RequestType request_type, | |
129 URLFetcher::Delegate* d); | |
130 TestURLFetcher* GetFetcherByID(int id) const; | |
131 void RemoveFetcherFromMap(int id); | |
132 | |
133 private: | |
134 // Maps from id passed to create to the returned URLFetcher. | |
135 typedef std::map<int, TestURLFetcher*> Fetchers; | |
136 Fetchers fetchers_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); | |
139 }; | |
140 | |
141 // The FakeUrlFetcher and FakeUrlFetcherFactory classes are similar to the | |
142 // ones above but don't require you to know when exactly the URLFetcher objects | |
143 // will be created. | |
144 // | |
145 // These classes let you set pre-baked HTTP responses for particular URLs. | |
146 // E.g., if the user requests http://a.com/ then respond with an HTTP/500. | |
147 // | |
148 // We assume that the thread that is calling Start() on the URLFetcher object | |
149 // has a message loop running. | |
150 // | |
151 // This class is not thread-safe. You should not call SetFakeResponse or | |
152 // ClearFakeResponse at the same time you call CreateURLFetcher. However, it is | |
153 // OK to start URLFetcher objects while setting or clearning fake responses | |
154 // since already created URLFetcher objects will not be affected by any changes | |
155 // made to the fake responses (once a URLFetcher object is created you cannot | |
156 // change its fake response). | |
157 // | |
158 // Example usage: | |
159 // FakeURLFetcherFactory factory; | |
160 // URLFetcher::set_factory(&factory); | |
161 // | |
162 // // You know that class SomeService will request url http://a.com/ and you | |
163 // // want to test the service class by returning an error. | |
164 // factory.SetFakeResponse("http://a.com/", "", false); | |
165 // // But if the service requests http://b.com/asdf you want to respond with | |
166 // // a simple html page and an HTTP/200 code. | |
167 // factory.SetFakeResponse("http://b.com/asdf", | |
168 // "<html><body>hello world</body></html>", | |
169 // true); | |
170 // | |
171 // SomeService service; | |
172 // service.Run(); // Will eventually request these two URLs. | |
173 | |
174 class FakeURLFetcherFactory : public URLFetcher::Factory { | |
175 public: | |
176 FakeURLFetcherFactory(); | |
177 // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown | |
178 // url to the given factory. | |
179 explicit FakeURLFetcherFactory(URLFetcher::Factory* default_factory); | |
180 virtual ~FakeURLFetcherFactory(); | |
181 | |
182 // If no fake response is set for the given URL this method will delegate the | |
183 // call to |default_factory_| if it is not NULL, or return NULL if it is | |
184 // NULL. | |
185 // Otherwise, it will return a URLFetcher object which will respond with the | |
186 // pre-baked response that the client has set by calling SetFakeResponse(). | |
187 virtual URLFetcher* CreateURLFetcher(int id, | |
188 const GURL& url, | |
189 URLFetcher::RequestType request_type, | |
190 URLFetcher::Delegate* d); | |
191 | |
192 // Sets the fake response for a given URL. If success is true we will serve | |
193 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. | |
194 void SetFakeResponse(const std::string& url, | |
195 const std::string& response_data, | |
196 bool success); | |
197 | |
198 // Clear all the fake responses that were previously set via | |
199 // SetFakeResponse(). | |
200 void ClearFakeReponses(); | |
201 | |
202 private: | |
203 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; | |
204 FakeResponseMap fake_responses_; | |
205 URLFetcher::Factory* default_factory_; | |
206 | |
207 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); | |
208 }; | |
209 | |
210 // This is an implementation of URLFetcher::Factory that will create a real | |
211 // URLFetcher. It can be use in conjunction with a FakeURLFetcherFactory in | |
212 // integration tests to control the behavior of some requests but execute | |
213 // all the other ones. | |
214 class URLFetcherFactory : public URLFetcher::Factory { | |
215 public: | |
216 URLFetcherFactory(); | |
217 virtual ~URLFetcherFactory(); | |
218 | |
219 // This method will create a real URLFetcher. | |
220 virtual URLFetcher* CreateURLFetcher(int id, | |
221 const GURL& url, | |
222 URLFetcher::RequestType request_type, | |
223 URLFetcher::Delegate* d); | |
224 | |
225 }; | |
226 | |
227 #endif // CONTENT_COMMON_TEST_URL_FETCHER_FACTORY_H_ | |
OLD | NEW |