OLD | NEW |
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/component_updater/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/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "components/component_updater/test/test_configurator.h" | 10 #include "components/component_updater/test/test_configurator.h" |
11 #include "net/base/upload_bytes_element_reader.h" | 11 #include "net/base/upload_bytes_element_reader.h" |
12 #include "net/url_request/url_request.h" | 12 #include "net/url_request/url_request.h" |
13 #include "net/url_request/url_request_filter.h" | 13 #include "net/url_request/url_request_filter.h" |
14 #include "net/url_request/url_request_interceptor.h" | 14 #include "net/url_request/url_request_interceptor.h" |
15 #include "net/url_request/url_request_simple_job.h" | 15 #include "net/url_request/url_request_simple_job.h" |
16 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
17 | 17 |
18 namespace component_updater { | 18 namespace component_updater { |
19 | 19 |
20 // Returns a canned response. | 20 // Returns a canned response. |
21 class URLRequestMockJob : public net::URLRequestSimpleJob { | 21 class URLRequestMockJob : public net::URLRequestSimpleJob { |
22 public: | 22 public: |
23 URLRequestMockJob(net::URLRequest* request, | 23 URLRequestMockJob(net::URLRequest* request, |
24 net::NetworkDelegate* network_delegate, | 24 net::NetworkDelegate* network_delegate, |
25 const std::string& response) | 25 int response_code, |
| 26 const std::string& response_body) |
26 : net::URLRequestSimpleJob(request, network_delegate), | 27 : net::URLRequestSimpleJob(request, network_delegate), |
27 response_(response) {} | 28 response_code_(response_code), |
| 29 response_body_(response_body) {} |
28 | 30 |
29 protected: | 31 protected: |
30 virtual int GetResponseCode() const OVERRIDE { return 200; } | 32 virtual int GetResponseCode() const OVERRIDE { return response_code_; } |
31 | 33 |
32 virtual int GetData(std::string* mime_type, | 34 virtual int GetData(std::string* mime_type, |
33 std::string* charset, | 35 std::string* charset, |
34 std::string* data, | 36 std::string* data, |
35 const net::CompletionCallback& callback) const OVERRIDE { | 37 const net::CompletionCallback& callback) const OVERRIDE { |
36 mime_type->assign("text/plain"); | 38 mime_type->assign("text/plain"); |
37 charset->assign("US-ASCII"); | 39 charset->assign("US-ASCII"); |
38 data->assign(response_); | 40 data->assign(response_body_); |
39 return net::OK; | 41 return net::OK; |
40 } | 42 } |
41 | 43 |
42 private: | 44 private: |
43 virtual ~URLRequestMockJob() {} | 45 virtual ~URLRequestMockJob() {} |
44 | 46 |
45 std::string response_; | 47 int response_code_; |
| 48 std::string response_body_; |
46 DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob); | 49 DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob); |
47 }; | 50 }; |
48 | 51 |
49 URLRequestPostInterceptor::URLRequestPostInterceptor( | 52 URLRequestPostInterceptor::URLRequestPostInterceptor( |
50 const GURL& url, | 53 const GURL& url, |
51 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) | 54 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) |
52 : url_(url), io_task_runner_(io_task_runner), hit_count_(0) { | 55 : url_(url), io_task_runner_(io_task_runner), hit_count_(0) { |
53 } | 56 } |
54 | 57 |
55 URLRequestPostInterceptor::~URLRequestPostInterceptor() { | 58 URLRequestPostInterceptor::~URLRequestPostInterceptor() { |
56 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 59 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
57 ClearExpectations(); | 60 ClearExpectations(); |
58 } | 61 } |
59 | 62 |
60 void URLRequestPostInterceptor::ClearExpectations() { | 63 void URLRequestPostInterceptor::ClearExpectations() { |
61 while (!expectations_.empty()) { | 64 while (!expectations_.empty()) { |
62 Expectation expectation(expectations_.front()); | 65 Expectation expectation(expectations_.front()); |
63 delete expectation.first; | 66 delete expectation.first; |
64 expectations_.pop(); | 67 expectations_.pop(); |
65 } | 68 } |
66 } | 69 } |
67 | 70 |
68 GURL URLRequestPostInterceptor::GetUrl() const { | 71 GURL URLRequestPostInterceptor::GetUrl() const { |
69 return url_; | 72 return url_; |
70 } | 73 } |
71 | 74 |
72 bool URLRequestPostInterceptor::ExpectRequest( | 75 bool URLRequestPostInterceptor::ExpectRequest( |
73 class RequestMatcher* request_matcher) { | 76 class RequestMatcher* request_matcher) { |
74 expectations_.push(std::make_pair(request_matcher, "")); | 77 expectations_.push(std::make_pair(request_matcher, |
| 78 ExpectationResponse(kResponseCode200, ""))); |
75 return true; | 79 return true; |
76 } | 80 } |
77 | 81 |
| 82 bool URLRequestPostInterceptor::ExpectRequest( |
| 83 class RequestMatcher* request_matcher, |
| 84 int response_code) { |
| 85 expectations_.push( |
| 86 std::make_pair(request_matcher, ExpectationResponse(response_code, ""))); |
| 87 return true; |
| 88 } |
| 89 |
78 bool URLRequestPostInterceptor::ExpectRequest( | 90 bool URLRequestPostInterceptor::ExpectRequest( |
79 class RequestMatcher* request_matcher, | 91 class RequestMatcher* request_matcher, |
80 const base::FilePath& filepath) { | 92 const base::FilePath& filepath) { |
81 std::string response; | 93 std::string response; |
82 if (filepath.empty() || !base::ReadFileToString(filepath, &response)) | 94 if (filepath.empty() || !base::ReadFileToString(filepath, &response)) |
83 return false; | 95 return false; |
84 expectations_.push(std::make_pair(request_matcher, response)); | 96 |
| 97 expectations_.push(std::make_pair( |
| 98 request_matcher, ExpectationResponse(kResponseCode200, response))); |
85 return true; | 99 return true; |
86 } | 100 } |
87 | 101 |
88 int URLRequestPostInterceptor::GetHitCount() const { | 102 int URLRequestPostInterceptor::GetHitCount() const { |
89 base::AutoLock auto_lock(interceptor_lock_); | 103 base::AutoLock auto_lock(interceptor_lock_); |
90 return hit_count_; | 104 return hit_count_; |
91 } | 105 } |
92 | 106 |
93 int URLRequestPostInterceptor::GetCount() const { | 107 int URLRequestPostInterceptor::GetCount() const { |
94 base::AutoLock auto_lock(interceptor_lock_); | 108 base::AutoLock auto_lock(interceptor_lock_); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 for (InterceptorMap::iterator it = interceptors_.begin(); | 154 for (InterceptorMap::iterator it = interceptors_.begin(); |
141 it != interceptors_.end(); | 155 it != interceptors_.end(); |
142 ++it) | 156 ++it) |
143 delete (*it).second; | 157 delete (*it).second; |
144 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme_, | 158 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme_, |
145 hostname_); | 159 hostname_); |
146 } | 160 } |
147 | 161 |
148 void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) { | 162 void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) { |
149 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 163 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
150 CHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end()); | 164 DCHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end()); |
151 | 165 |
152 interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor)); | 166 interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor)); |
153 } | 167 } |
154 | 168 |
155 private: | 169 private: |
156 virtual ~Delegate() {} | 170 virtual ~Delegate() {} |
157 | 171 |
158 virtual net::URLRequestJob* MaybeInterceptRequest( | 172 virtual net::URLRequestJob* MaybeInterceptRequest( |
159 net::URLRequest* request, | 173 net::URLRequest* request, |
160 net::NetworkDelegate* network_delegate) const OVERRIDE { | 174 net::NetworkDelegate* network_delegate) const OVERRIDE { |
(...skipping 28 matching lines...) Expand all Loading... |
189 const std::string request_body(reader->bytes()); | 203 const std::string request_body(reader->bytes()); |
190 | 204 |
191 { | 205 { |
192 base::AutoLock auto_lock(interceptor->interceptor_lock_); | 206 base::AutoLock auto_lock(interceptor->interceptor_lock_); |
193 interceptor->requests_.push_back(request_body); | 207 interceptor->requests_.push_back(request_body); |
194 if (interceptor->expectations_.empty()) | 208 if (interceptor->expectations_.empty()) |
195 return NULL; | 209 return NULL; |
196 const URLRequestPostInterceptor::Expectation& expectation( | 210 const URLRequestPostInterceptor::Expectation& expectation( |
197 interceptor->expectations_.front()); | 211 interceptor->expectations_.front()); |
198 if (expectation.first->Match(request_body)) { | 212 if (expectation.first->Match(request_body)) { |
199 const std::string response(expectation.second); | 213 const int response_code(expectation.second.response_code); |
| 214 const std::string response_body(expectation.second.response_body); |
200 delete expectation.first; | 215 delete expectation.first; |
201 interceptor->expectations_.pop(); | 216 interceptor->expectations_.pop(); |
202 ++interceptor->hit_count_; | 217 ++interceptor->hit_count_; |
203 | 218 |
204 return new URLRequestMockJob(request, network_delegate, response); | 219 return new URLRequestMockJob( |
| 220 request, network_delegate, response_code, response_body); |
205 } | 221 } |
206 } | 222 } |
207 | 223 |
208 return NULL; | 224 return NULL; |
209 } | 225 } |
210 | 226 |
211 typedef std::map<GURL, URLRequestPostInterceptor*> InterceptorMap; | 227 typedef std::map<GURL, URLRequestPostInterceptor*> InterceptorMap; |
212 InterceptorMap interceptors_; | 228 InterceptorMap interceptors_; |
213 | 229 |
214 const std::string scheme_; | 230 const std::string scheme_; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) | 285 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) |
270 : URLRequestPostInterceptorFactory(POST_INTERCEPT_SCHEME, | 286 : URLRequestPostInterceptorFactory(POST_INTERCEPT_SCHEME, |
271 POST_INTERCEPT_HOSTNAME, | 287 POST_INTERCEPT_HOSTNAME, |
272 io_task_runner) { | 288 io_task_runner) { |
273 } | 289 } |
274 | 290 |
275 InterceptorFactory::~InterceptorFactory() { | 291 InterceptorFactory::~InterceptorFactory() { |
276 } | 292 } |
277 | 293 |
278 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptor() { | 294 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptor() { |
| 295 return CreateInterceptorForPath(POST_INTERCEPT_PATH); |
| 296 } |
| 297 |
| 298 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptorForPath( |
| 299 const char* url_path) { |
279 return URLRequestPostInterceptorFactory::CreateInterceptor( | 300 return URLRequestPostInterceptorFactory::CreateInterceptor( |
280 base::FilePath::FromUTF8Unsafe(POST_INTERCEPT_PATH)); | 301 base::FilePath::FromUTF8Unsafe(url_path)); |
281 } | 302 } |
282 | 303 |
283 } // namespace component_updater | 304 } // namespace component_updater |
OLD | NEW |