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

Side by Side Diff: components/update_client/test/url_request_post_interceptor.cc

Issue 808773005: Move most of the component updater artifacts to update_client. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #include "components/component_updater/test/url_request_post_interceptor.h" 5 #include "components/update_client/test/url_request_post_interceptor.h"
6 6
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "components/component_updater/test/test_configurator.h" 11 #include "components/update_client/test/test_configurator.h"
12 #include "net/base/upload_bytes_element_reader.h" 12 #include "net/base/upload_bytes_element_reader.h"
13 #include "net/base/upload_data_stream.h" 13 #include "net/base/upload_data_stream.h"
14 #include "net/url_request/url_request.h" 14 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_filter.h" 15 #include "net/url_request/url_request_filter.h"
16 #include "net/url_request/url_request_interceptor.h" 16 #include "net/url_request/url_request_interceptor.h"
17 #include "net/url_request/url_request_simple_job.h" 17 #include "net/url_request/url_request_simple_job.h"
18 #include "net/url_request/url_request_test_util.h" 18 #include "net/url_request/url_request_test_util.h"
19 19
20 namespace component_updater { 20 namespace update_client {
21 21
22 // Returns a canned response. 22 // Returns a canned response.
23 class URLRequestMockJob : public net::URLRequestSimpleJob { 23 class URLRequestMockJob : public net::URLRequestSimpleJob {
24 public: 24 public:
25 URLRequestMockJob(net::URLRequest* request, 25 URLRequestMockJob(net::URLRequest* request,
26 net::NetworkDelegate* network_delegate, 26 net::NetworkDelegate* network_delegate,
27 int response_code, 27 int response_code,
28 const std::string& response_body) 28 const std::string& response_body)
29 : net::URLRequestSimpleJob(request, network_delegate), 29 : net::URLRequestSimpleJob(request, network_delegate),
30 response_code_(response_code), 30 response_code_(response_code),
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 return requests_; 116 return requests_;
117 } 117 }
118 118
119 std::string URLRequestPostInterceptor::GetRequestsAsString() const { 119 std::string URLRequestPostInterceptor::GetRequestsAsString() const {
120 std::vector<std::string> requests(GetRequests()); 120 std::vector<std::string> requests(GetRequests());
121 121
122 std::string s = "Requests are:"; 122 std::string s = "Requests are:";
123 123
124 int i = 0; 124 int i = 0;
125 for (std::vector<std::string>::const_iterator it = requests.begin(); 125 for (std::vector<std::string>::const_iterator it = requests.begin();
126 it != requests.end(); 126 it != requests.end(); ++it) {
127 ++it) {
128 s.append(base::StringPrintf("\n (%d): %s", ++i, it->c_str())); 127 s.append(base::StringPrintf("\n (%d): %s", ++i, it->c_str()));
129 } 128 }
130 129
131 return s; 130 return s;
132 } 131 }
133 132
134 void URLRequestPostInterceptor::Reset() { 133 void URLRequestPostInterceptor::Reset() {
135 base::AutoLock auto_lock(interceptor_lock_); 134 base::AutoLock auto_lock(interceptor_lock_);
136 hit_count_ = 0; 135 hit_count_ = 0;
137 requests_.clear(); 136 requests_.clear();
138 ClearExpectations(); 137 ClearExpectations();
139 } 138 }
140 139
141 class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor { 140 class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor {
142 public: 141 public:
143 Delegate(const std::string& scheme, 142 Delegate(const std::string& scheme,
144 const std::string& hostname, 143 const std::string& hostname,
145 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) 144 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
146 : scheme_(scheme), hostname_(hostname), io_task_runner_(io_task_runner) {} 145 : scheme_(scheme), hostname_(hostname), io_task_runner_(io_task_runner) {}
147 146
148 void Register() { 147 void Register() {
149 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); 148 DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
150 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( 149 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
151 scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this)); 150 scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this));
152 } 151 }
153 152
154 void Unregister() { 153 void Unregister() {
155 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); 154 DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
156 for (InterceptorMap::iterator it = interceptors_.begin(); 155 for (InterceptorMap::iterator it = interceptors_.begin();
157 it != interceptors_.end(); 156 it != interceptors_.end(); ++it)
158 ++it)
159 delete (*it).second; 157 delete (*it).second;
160 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme_, 158 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme_,
161 hostname_); 159 hostname_);
162 } 160 }
163 161
164 void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) { 162 void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) {
165 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); 163 DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
166 DCHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end()); 164 DCHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end());
167 165
168 interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor)); 166 interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 return NULL; 209 return NULL;
212 const URLRequestPostInterceptor::Expectation& expectation( 210 const URLRequestPostInterceptor::Expectation& expectation(
213 interceptor->expectations_.front()); 211 interceptor->expectations_.front());
214 if (expectation.first->Match(request_body)) { 212 if (expectation.first->Match(request_body)) {
215 const int response_code(expectation.second.response_code); 213 const int response_code(expectation.second.response_code);
216 const std::string response_body(expectation.second.response_body); 214 const std::string response_body(expectation.second.response_body);
217 delete expectation.first; 215 delete expectation.first;
218 interceptor->expectations_.pop(); 216 interceptor->expectations_.pop();
219 ++interceptor->hit_count_; 217 ++interceptor->hit_count_;
220 218
221 return new URLRequestMockJob( 219 return new URLRequestMockJob(request, network_delegate, response_code,
222 request, network_delegate, response_code, response_body); 220 response_body);
223 } 221 }
224 } 222 }
225 223
226 return NULL; 224 return NULL;
227 } 225 }
228 226
229 typedef std::map<GURL, URLRequestPostInterceptor*> InterceptorMap; 227 typedef std::map<GURL, URLRequestPostInterceptor*> InterceptorMap;
230 InterceptorMap interceptors_; 228 InterceptorMap interceptors_;
231 229
232 const std::string scheme_; 230 const std::string scheme_;
233 const std::string hostname_; 231 const std::string hostname_;
234 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; 232 scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
235 233
236 DISALLOW_COPY_AND_ASSIGN(Delegate); 234 DISALLOW_COPY_AND_ASSIGN(Delegate);
237 }; 235 };
238 236
239 URLRequestPostInterceptorFactory::URLRequestPostInterceptorFactory( 237 URLRequestPostInterceptorFactory::URLRequestPostInterceptorFactory(
240 const std::string& scheme, 238 const std::string& scheme,
241 const std::string& hostname, 239 const std::string& hostname,
242 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) 240 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
243 : scheme_(scheme), 241 : scheme_(scheme),
244 hostname_(hostname), 242 hostname_(hostname),
245 io_task_runner_(io_task_runner), 243 io_task_runner_(io_task_runner),
246 delegate_(new URLRequestPostInterceptor::Delegate(scheme, 244 delegate_(new URLRequestPostInterceptor::Delegate(scheme,
247 hostname, 245 hostname,
248 io_task_runner)) { 246 io_task_runner)) {
249 io_task_runner_->PostTask( 247 io_task_runner_->PostTask(
250 FROM_HERE, 248 FROM_HERE, base::Bind(&URLRequestPostInterceptor::Delegate::Register,
251 base::Bind(&URLRequestPostInterceptor::Delegate::Register, 249 base::Unretained(delegate_)));
252 base::Unretained(delegate_)));
253 } 250 }
254 251
255 URLRequestPostInterceptorFactory::~URLRequestPostInterceptorFactory() { 252 URLRequestPostInterceptorFactory::~URLRequestPostInterceptorFactory() {
256 io_task_runner_->PostTask( 253 io_task_runner_->PostTask(
257 FROM_HERE, 254 FROM_HERE, base::Bind(&URLRequestPostInterceptor::Delegate::Unregister,
258 base::Bind(&URLRequestPostInterceptor::Delegate::Unregister, 255 base::Unretained(delegate_)));
259 base::Unretained(delegate_)));
260 } 256 }
261 257
262 URLRequestPostInterceptor* URLRequestPostInterceptorFactory::CreateInterceptor( 258 URLRequestPostInterceptor* URLRequestPostInterceptorFactory::CreateInterceptor(
263 const base::FilePath& filepath) { 259 const base::FilePath& filepath) {
264 const GURL base_url( 260 const GURL base_url(
265 base::StringPrintf("%s://%s", scheme_.c_str(), hostname_.c_str())); 261 base::StringPrintf("%s://%s", scheme_.c_str(), hostname_.c_str()));
266 GURL absolute_url(base_url.Resolve(filepath.MaybeAsASCII())); 262 GURL absolute_url(base_url.Resolve(filepath.MaybeAsASCII()));
267 URLRequestPostInterceptor* interceptor( 263 URLRequestPostInterceptor* interceptor(
268 new URLRequestPostInterceptor(absolute_url, io_task_runner_)); 264 new URLRequestPostInterceptor(absolute_url, io_task_runner_));
269 bool res = io_task_runner_->PostTask( 265 bool res = io_task_runner_->PostTask(
270 FROM_HERE, 266 FROM_HERE,
271 base::Bind(&URLRequestPostInterceptor::Delegate::OnCreateInterceptor, 267 base::Bind(&URLRequestPostInterceptor::Delegate::OnCreateInterceptor,
272 base::Unretained(delegate_), 268 base::Unretained(delegate_), base::Unretained(interceptor)));
273 base::Unretained(interceptor)));
274 if (!res) { 269 if (!res) {
275 delete interceptor; 270 delete interceptor;
276 return NULL; 271 return NULL;
277 } 272 }
278 273
279 return interceptor; 274 return interceptor;
280 } 275 }
281 276
282 bool PartialMatch::Match(const std::string& actual) const { 277 bool PartialMatch::Match(const std::string& actual) const {
283 return actual.find(expected_) != std::string::npos; 278 return actual.find(expected_) != std::string::npos;
(...skipping 12 matching lines...) Expand all
296 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptor() { 291 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptor() {
297 return CreateInterceptorForPath(POST_INTERCEPT_PATH); 292 return CreateInterceptorForPath(POST_INTERCEPT_PATH);
298 } 293 }
299 294
300 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptorForPath( 295 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptorForPath(
301 const char* url_path) { 296 const char* url_path) {
302 return URLRequestPostInterceptorFactory::CreateInterceptor( 297 return URLRequestPostInterceptorFactory::CreateInterceptor(
303 base::FilePath::FromUTF8Unsafe(url_path)); 298 base::FilePath::FromUTF8Unsafe(url_path));
304 } 299 }
305 300
306 } // namespace component_updater 301 } // namespace update_client
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698