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

Side by Side Diff: net/url_request/test_url_fetcher_factory.h

Issue 10702141: Introduced TestURLFetcher::DelegateForTests interface to allow event-driven end-to-end tests of cla… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 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
« no previous file with comments | « no previous file | net/url_request/test_url_fetcher_factory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ 5 #ifndef NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_
6 #define NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ 6 #define NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_
7 #pragma once 7 #pragma once
8 8
9 #include <list> 9 #include <list>
10 #include <map> 10 #include <map>
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 // fetcher->delegate()->OnURLFetchComplete(...); 52 // fetcher->delegate()->OnURLFetchComplete(...);
53 // // Make sure consumer of URLFetcher does the right thing. 53 // // Make sure consumer of URLFetcher does the right thing.
54 // ... 54 // ...
55 // 55 //
56 // Note: if you don't know when your request objects will be created you 56 // Note: if you don't know when your request objects will be created you
57 // might want to use the FakeURLFetcher and FakeURLFetcherFactory classes 57 // might want to use the FakeURLFetcher and FakeURLFetcherFactory classes
58 // below. 58 // below.
59 59
60 class TestURLFetcher : public URLFetcher { 60 class TestURLFetcher : public URLFetcher {
61 public: 61 public:
62 // Interface for tests to intercept production code classes using URLFetcher.
63 // Allows even-driven mock server classes to analyze the correctness of
64 // requests / uploads events and forge responses back at the right moment.
65 class DelegateForTests {
66 public:
67 // Callback issued correspondingly to the call to the |Start()| method.
68 virtual void OnRequestStart(int fetcher_id) = 0;
69
70 // Callback issued correspondingly to the call to |AppendChunkToUpload|.
71 // Uploaded chunks can be retrieved with the |upload_chunks()| getter.
72 virtual void OnChunkUpload(int fetcher_id) = 0;
73
74 // Callback issued correspondingly to the destructor.
75 virtual void OnRequestEnd(int fetcher_id) = 0;
76 };
77
62 TestURLFetcher(int id, 78 TestURLFetcher(int id,
63 const GURL& url, 79 const GURL& url,
64 URLFetcherDelegate* d); 80 URLFetcherDelegate* d);
65 virtual ~TestURLFetcher(); 81 virtual ~TestURLFetcher();
66 82
67 // URLFetcher implementation 83 // URLFetcher implementation
68 virtual void SetUploadData(const std::string& upload_content_type, 84 virtual void SetUploadData(const std::string& upload_content_type,
69 const std::string& upload_content) OVERRIDE; 85 const std::string& upload_content) OVERRIDE;
70 virtual void SetChunkedUpload( 86 virtual void SetChunkedUpload(
71 const std::string& upload_content_type) OVERRIDE; 87 const std::string& upload_content_type) OVERRIDE;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 138
123 // Unique ID in our factory. 139 // Unique ID in our factory.
124 int id() const { return id_; } 140 int id() const { return id_; }
125 141
126 // Returns the data uploaded on this URLFetcher. 142 // Returns the data uploaded on this URLFetcher.
127 const std::string& upload_data() const { return upload_data_; } 143 const std::string& upload_data() const { return upload_data_; }
128 144
129 // Returns the chunks of data uploaded on this URLFetcher. 145 // Returns the chunks of data uploaded on this URLFetcher.
130 const std::list<std::string>& upload_chunks() const { return chunks_; } 146 const std::list<std::string>& upload_chunks() const { return chunks_; }
131 147
148 // Checks whether the last call to |AppendChunkToUpload(...)| was final.
149 bool did_receive_last_chunk() const { return did_receive_last_chunk_; }
150
132 // Returns the delegate installed on the URLFetcher. 151 // Returns the delegate installed on the URLFetcher.
133 URLFetcherDelegate* delegate() const { return delegate_; } 152 URLFetcherDelegate* delegate() const { return delegate_; }
134 153
135 void set_url(const GURL& url) { fake_url_ = url; } 154 void set_url(const GURL& url) { fake_url_ = url; }
136 void set_status(const URLRequestStatus& status); 155 void set_status(const URLRequestStatus& status);
137 void set_response_code(int response_code) { 156 void set_response_code(int response_code) {
138 fake_response_code_ = response_code; 157 fake_response_code_ = response_code;
139 } 158 }
140 void set_cookies(const ResponseCookies& c) { fake_cookies_ = c; } 159 void set_cookies(const ResponseCookies& c) { fake_cookies_ = c; }
141 void set_was_fetched_via_proxy(bool flag); 160 void set_was_fetched_via_proxy(bool flag);
142 void set_response_headers(scoped_refptr<HttpResponseHeaders> headers); 161 void set_response_headers(scoped_refptr<HttpResponseHeaders> headers);
143 void set_backoff_delay(base::TimeDelta backoff_delay); 162 void set_backoff_delay(base::TimeDelta backoff_delay);
163 void SetDelegateForTests(DelegateForTests* delegate_for_tests);
144 164
145 // Set string data. 165 // Set string data.
146 void SetResponseString(const std::string& response); 166 void SetResponseString(const std::string& response);
147 167
148 // Set File data. 168 // Set File data.
149 void SetResponseFilePath(const FilePath& path); 169 void SetResponseFilePath(const FilePath& path);
150 170
151 private: 171 private:
152 enum ResponseDestinationType { 172 enum ResponseDestinationType {
153 STRING, // Default: In a std::string 173 STRING, // Default: In a std::string
154 TEMP_FILE // Write to a temp file 174 TEMP_FILE // Write to a temp file
155 }; 175 };
156 176
157 const int id_; 177 const int id_;
158 const GURL original_url_; 178 const GURL original_url_;
159 URLFetcherDelegate* delegate_; 179 URLFetcherDelegate* delegate_;
180 DelegateForTests* delegate_for_tests_;
160 std::string upload_data_; 181 std::string upload_data_;
161 std::list<std::string> chunks_; 182 std::list<std::string> chunks_;
162 bool did_receive_last_chunk_; 183 bool did_receive_last_chunk_;
163 184
164 // User can use set_* methods to provide values returned by getters. 185 // User can use set_* methods to provide values returned by getters.
165 // Setting the real values is not possible, because the real class 186 // Setting the real values is not possible, because the real class
166 // has no setters. The data is a private member of a class defined 187 // has no setters. The data is a private member of a class defined
167 // in a .cc file, so we can't get at it with friendship. 188 // in a .cc file, so we can't get at it with friendship.
168 int fake_load_flags_; 189 int fake_load_flags_;
169 GURL fake_url_; 190 GURL fake_url_;
170 URLRequestStatus fake_status_; 191 URLRequestStatus fake_status_;
171 int fake_response_code_; 192 int fake_response_code_;
172 ResponseCookies fake_cookies_; 193 ResponseCookies fake_cookies_;
173 ResponseDestinationType fake_response_destination_; 194 ResponseDestinationType fake_response_destination_;
174 std::string fake_response_string_; 195 std::string fake_response_string_;
175 FilePath fake_response_file_path_; 196 FilePath fake_response_file_path_;
176 bool fake_was_fetched_via_proxy_; 197 bool fake_was_fetched_via_proxy_;
177 scoped_refptr<HttpResponseHeaders> fake_response_headers_; 198 scoped_refptr<HttpResponseHeaders> fake_response_headers_;
178 HttpRequestHeaders fake_extra_request_headers_; 199 HttpRequestHeaders fake_extra_request_headers_;
179 int fake_max_retries_; 200 int fake_max_retries_;
180 base::TimeDelta fake_backoff_delay_; 201 base::TimeDelta fake_backoff_delay_;
181 202
182 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); 203 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher);
183 }; 204 };
184 205
206 typedef TestURLFetcher::DelegateForTests TestURLFetcherDelegateForTests;
207
185 // Simple URLFetcherFactory method that creates TestURLFetchers. All fetchers 208 // Simple URLFetcherFactory method that creates TestURLFetchers. All fetchers
186 // are registered in a map by the id passed to the create method. 209 // are registered in a map by the id passed to the create method.
187 class TestURLFetcherFactory : public URLFetcherFactory, 210 class TestURLFetcherFactory : public URLFetcherFactory,
188 public ScopedURLFetcherFactory { 211 public ScopedURLFetcherFactory {
189 public: 212 public:
190 TestURLFetcherFactory(); 213 TestURLFetcherFactory();
191 virtual ~TestURLFetcherFactory(); 214 virtual ~TestURLFetcherFactory();
192 215
193 virtual URLFetcher* CreateURLFetcher( 216 virtual URLFetcher* CreateURLFetcher(
194 int id, 217 int id,
195 const GURL& url, 218 const GURL& url,
196 URLFetcher::RequestType request_type, 219 URLFetcher::RequestType request_type,
197 URLFetcherDelegate* d) OVERRIDE; 220 URLFetcherDelegate* d) OVERRIDE;
198 TestURLFetcher* GetFetcherByID(int id) const; 221 TestURLFetcher* GetFetcherByID(int id) const;
199 void RemoveFetcherFromMap(int id); 222 void RemoveFetcherFromMap(int id);
223 void SetDelegateForTests(TestURLFetcherDelegateForTests* delegate_for_tests);
200 224
201 private: 225 private:
202 // Maps from id passed to create to the returned URLFetcher. 226 // Maps from id passed to create to the returned URLFetcher.
203 typedef std::map<int, TestURLFetcher*> Fetchers; 227 typedef std::map<int, TestURLFetcher*> Fetchers;
204 Fetchers fetchers_; 228 Fetchers fetchers_;
229 TestURLFetcherDelegateForTests* delegate_for_tests_;
205 230
206 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); 231 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory);
207 }; 232 };
208 233
209 // The FakeURLFetcher and FakeURLFetcherFactory classes are similar to the 234 // The FakeURLFetcher and FakeURLFetcherFactory classes are similar to the
210 // ones above but don't require you to know when exactly the URLFetcher objects 235 // ones above but don't require you to know when exactly the URLFetcher objects
211 // will be created. 236 // will be created.
212 // 237 //
213 // These classes let you set pre-baked HTTP responses for particular URLs. 238 // These classes let you set pre-baked HTTP responses for particular URLs.
214 // E.g., if the user requests http://a.com/ then respond with an HTTP/500. 239 // E.g., if the user requests http://a.com/ then respond with an HTTP/500.
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 int id, 315 int id,
291 const GURL& url, 316 const GURL& url,
292 URLFetcher::RequestType request_type, 317 URLFetcher::RequestType request_type,
293 URLFetcherDelegate* d) OVERRIDE; 318 URLFetcherDelegate* d) OVERRIDE;
294 319
295 }; 320 };
296 321
297 } // namespace net 322 } // namespace net
298 323
299 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ 324 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_
OLDNEW
« no previous file with comments | « no previous file | net/url_request/test_url_fetcher_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698