| 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/file_util.h" | 7 #include "base/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 "content/public/test/test_browser_thread.h" | |
| 12 #include "net/base/upload_bytes_element_reader.h" | 11 #include "net/base/upload_bytes_element_reader.h" |
| 13 #include "net/url_request/url_request.h" | 12 #include "net/url_request/url_request.h" |
| 14 #include "net/url_request/url_request_filter.h" | 13 #include "net/url_request/url_request_filter.h" |
| 15 #include "net/url_request/url_request_interceptor.h" | 14 #include "net/url_request/url_request_interceptor.h" |
| 16 #include "net/url_request/url_request_simple_job.h" | 15 #include "net/url_request/url_request_simple_job.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 18 | 17 |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 namespace component_updater { | 18 namespace component_updater { |
| 22 | 19 |
| 23 // Returns a canned response. | 20 // Returns a canned response. |
| 24 class URLRequestMockJob : public net::URLRequestSimpleJob { | 21 class URLRequestMockJob : public net::URLRequestSimpleJob { |
| 25 public: | 22 public: |
| 26 URLRequestMockJob(net::URLRequest* request, | 23 URLRequestMockJob(net::URLRequest* request, |
| 27 net::NetworkDelegate* network_delegate, | 24 net::NetworkDelegate* network_delegate, |
| 28 const std::string& response) | 25 const std::string& response) |
| 29 : net::URLRequestSimpleJob(request, network_delegate), | 26 : net::URLRequestSimpleJob(request, network_delegate), |
| 30 response_(response) {} | 27 response_(response) {} |
| (...skipping 11 matching lines...) Expand all Loading... |
| 42 return net::OK; | 39 return net::OK; |
| 43 } | 40 } |
| 44 | 41 |
| 45 private: | 42 private: |
| 46 virtual ~URLRequestMockJob() {} | 43 virtual ~URLRequestMockJob() {} |
| 47 | 44 |
| 48 std::string response_; | 45 std::string response_; |
| 49 DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob); | 46 DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob); |
| 50 }; | 47 }; |
| 51 | 48 |
| 52 URLRequestPostInterceptor::URLRequestPostInterceptor(const GURL& url) | 49 URLRequestPostInterceptor::URLRequestPostInterceptor( |
| 53 : url_(url), hit_count_(0) { | 50 const GURL& url, |
| 51 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) |
| 52 : url_(url), io_task_runner_(io_task_runner), hit_count_(0) { |
| 54 } | 53 } |
| 55 | 54 |
| 56 URLRequestPostInterceptor::~URLRequestPostInterceptor() { | 55 URLRequestPostInterceptor::~URLRequestPostInterceptor() { |
| 57 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 56 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 58 ClearExpectations(); | 57 ClearExpectations(); |
| 59 } | 58 } |
| 60 | 59 |
| 61 void URLRequestPostInterceptor::ClearExpectations() { | 60 void URLRequestPostInterceptor::ClearExpectations() { |
| 62 while (!expectations_.empty()) { | 61 while (!expectations_.empty()) { |
| 63 Expectation expectation(expectations_.front()); | 62 Expectation expectation(expectations_.front()); |
| 64 delete expectation.first; | 63 delete expectation.first; |
| 65 expectations_.pop(); | 64 expectations_.pop(); |
| 66 } | 65 } |
| 67 } | 66 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 | 117 |
| 119 void URLRequestPostInterceptor::Reset() { | 118 void URLRequestPostInterceptor::Reset() { |
| 120 base::AutoLock auto_lock(interceptor_lock_); | 119 base::AutoLock auto_lock(interceptor_lock_); |
| 121 hit_count_ = 0; | 120 hit_count_ = 0; |
| 122 requests_.clear(); | 121 requests_.clear(); |
| 123 ClearExpectations(); | 122 ClearExpectations(); |
| 124 } | 123 } |
| 125 | 124 |
| 126 class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor { | 125 class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor { |
| 127 public: | 126 public: |
| 128 Delegate(const std::string& scheme, const std::string& hostname) | 127 Delegate(const std::string& scheme, |
| 129 : scheme_(scheme), hostname_(hostname) {} | 128 const std::string& hostname, |
| 129 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) |
| 130 : scheme_(scheme), hostname_(hostname), io_task_runner_(io_task_runner) {} |
| 130 | 131 |
| 131 void Register() { | 132 void Register() { |
| 132 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 133 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 133 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( | 134 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
| 134 scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this)); | 135 scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this)); |
| 135 } | 136 } |
| 136 | 137 |
| 137 void Unregister() { | 138 void Unregister() { |
| 138 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 139 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 139 for (InterceptorMap::iterator it = interceptors_.begin(); | 140 for (InterceptorMap::iterator it = interceptors_.begin(); |
| 140 it != interceptors_.end(); | 141 it != interceptors_.end(); |
| 141 ++it) | 142 ++it) |
| 142 delete (*it).second; | 143 delete (*it).second; |
| 143 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme_, | 144 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme_, |
| 144 hostname_); | 145 hostname_); |
| 145 } | 146 } |
| 146 | 147 |
| 147 void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) { | 148 void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) { |
| 148 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 149 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 149 CHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end()); | 150 CHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end()); |
| 150 | 151 |
| 151 interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor)); | 152 interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor)); |
| 152 } | 153 } |
| 153 | 154 |
| 154 private: | 155 private: |
| 155 virtual ~Delegate() {} | 156 virtual ~Delegate() {} |
| 156 | 157 |
| 157 virtual net::URLRequestJob* MaybeInterceptRequest( | 158 virtual net::URLRequestJob* MaybeInterceptRequest( |
| 158 net::URLRequest* request, | 159 net::URLRequest* request, |
| 159 net::NetworkDelegate* network_delegate) const OVERRIDE { | 160 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 160 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 161 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 161 | 162 |
| 162 // Only intercepts POST. | 163 // Only intercepts POST. |
| 163 if (!request->has_upload()) | 164 if (!request->has_upload()) |
| 164 return NULL; | 165 return NULL; |
| 165 | 166 |
| 166 GURL url = request->url(); | 167 GURL url = request->url(); |
| 167 if (url.has_query()) { | 168 if (url.has_query()) { |
| 168 GURL::Replacements replacements; | 169 GURL::Replacements replacements; |
| 169 replacements.ClearQuery(); | 170 replacements.ClearQuery(); |
| 170 url = url.ReplaceComponents(replacements); | 171 url = url.ReplaceComponents(replacements); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 206 } |
| 206 | 207 |
| 207 return NULL; | 208 return NULL; |
| 208 } | 209 } |
| 209 | 210 |
| 210 typedef std::map<GURL, URLRequestPostInterceptor*> InterceptorMap; | 211 typedef std::map<GURL, URLRequestPostInterceptor*> InterceptorMap; |
| 211 InterceptorMap interceptors_; | 212 InterceptorMap interceptors_; |
| 212 | 213 |
| 213 const std::string scheme_; | 214 const std::string scheme_; |
| 214 const std::string hostname_; | 215 const std::string hostname_; |
| 216 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
| 215 | 217 |
| 216 DISALLOW_COPY_AND_ASSIGN(Delegate); | 218 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 217 }; | 219 }; |
| 218 | 220 |
| 219 URLRequestPostInterceptorFactory::URLRequestPostInterceptorFactory( | 221 URLRequestPostInterceptorFactory::URLRequestPostInterceptorFactory( |
| 220 const std::string& scheme, | 222 const std::string& scheme, |
| 221 const std::string& hostname) | 223 const std::string& hostname, |
| 224 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) |
| 222 : scheme_(scheme), | 225 : scheme_(scheme), |
| 223 hostname_(hostname), | 226 hostname_(hostname), |
| 224 delegate_(new URLRequestPostInterceptor::Delegate(scheme, hostname)) { | 227 io_task_runner_(io_task_runner), |
| 225 BrowserThread::PostTask( | 228 delegate_(new URLRequestPostInterceptor::Delegate(scheme, |
| 226 BrowserThread::IO, | 229 hostname, |
| 230 io_task_runner)) { |
| 231 io_task_runner_->PostTask( |
| 227 FROM_HERE, | 232 FROM_HERE, |
| 228 base::Bind(&URLRequestPostInterceptor::Delegate::Register, | 233 base::Bind(&URLRequestPostInterceptor::Delegate::Register, |
| 229 base::Unretained(delegate_))); | 234 base::Unretained(delegate_))); |
| 230 } | 235 } |
| 231 | 236 |
| 232 URLRequestPostInterceptorFactory::~URLRequestPostInterceptorFactory() { | 237 URLRequestPostInterceptorFactory::~URLRequestPostInterceptorFactory() { |
| 233 BrowserThread::PostTask( | 238 io_task_runner_->PostTask( |
| 234 BrowserThread::IO, | |
| 235 FROM_HERE, | 239 FROM_HERE, |
| 236 base::Bind(&URLRequestPostInterceptor::Delegate::Unregister, | 240 base::Bind(&URLRequestPostInterceptor::Delegate::Unregister, |
| 237 base::Unretained(delegate_))); | 241 base::Unretained(delegate_))); |
| 238 } | 242 } |
| 239 | 243 |
| 240 URLRequestPostInterceptor* URLRequestPostInterceptorFactory::CreateInterceptor( | 244 URLRequestPostInterceptor* URLRequestPostInterceptorFactory::CreateInterceptor( |
| 241 const base::FilePath& filepath) { | 245 const base::FilePath& filepath) { |
| 242 const GURL base_url( | 246 const GURL base_url( |
| 243 base::StringPrintf("%s://%s", scheme_.c_str(), hostname_.c_str())); | 247 base::StringPrintf("%s://%s", scheme_.c_str(), hostname_.c_str())); |
| 244 GURL absolute_url(base_url.Resolve(filepath.MaybeAsASCII())); | 248 GURL absolute_url(base_url.Resolve(filepath.MaybeAsASCII())); |
| 245 URLRequestPostInterceptor* interceptor( | 249 URLRequestPostInterceptor* interceptor( |
| 246 new URLRequestPostInterceptor(absolute_url)); | 250 new URLRequestPostInterceptor(absolute_url, io_task_runner_)); |
| 247 bool res = BrowserThread::PostTask( | 251 bool res = io_task_runner_->PostTask( |
| 248 BrowserThread::IO, | |
| 249 FROM_HERE, | 252 FROM_HERE, |
| 250 base::Bind(&URLRequestPostInterceptor::Delegate::OnCreateInterceptor, | 253 base::Bind(&URLRequestPostInterceptor::Delegate::OnCreateInterceptor, |
| 251 base::Unretained(delegate_), | 254 base::Unretained(delegate_), |
| 252 base::Unretained(interceptor))); | 255 base::Unretained(interceptor))); |
| 253 if (!res) { | 256 if (!res) { |
| 254 delete interceptor; | 257 delete interceptor; |
| 255 return NULL; | 258 return NULL; |
| 256 } | 259 } |
| 257 | 260 |
| 258 return interceptor; | 261 return interceptor; |
| 259 } | 262 } |
| 260 | 263 |
| 261 bool PartialMatch::Match(const std::string& actual) const { | 264 bool PartialMatch::Match(const std::string& actual) const { |
| 262 return actual.find(expected_) != std::string::npos; | 265 return actual.find(expected_) != std::string::npos; |
| 263 } | 266 } |
| 264 | 267 |
| 265 InterceptorFactory::InterceptorFactory() | 268 InterceptorFactory::InterceptorFactory( |
| 269 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) |
| 266 : URLRequestPostInterceptorFactory(POST_INTERCEPT_SCHEME, | 270 : URLRequestPostInterceptorFactory(POST_INTERCEPT_SCHEME, |
| 267 POST_INTERCEPT_HOSTNAME) { | 271 POST_INTERCEPT_HOSTNAME, |
| 272 io_task_runner) { |
| 268 } | 273 } |
| 269 | 274 |
| 270 InterceptorFactory::~InterceptorFactory() { | 275 InterceptorFactory::~InterceptorFactory() { |
| 271 } | 276 } |
| 272 | 277 |
| 273 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptor() { | 278 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptor() { |
| 274 return URLRequestPostInterceptorFactory::CreateInterceptor( | 279 return URLRequestPostInterceptorFactory::CreateInterceptor( |
| 275 base::FilePath::FromUTF8Unsafe(POST_INTERCEPT_PATH)); | 280 base::FilePath::FromUTF8Unsafe(POST_INTERCEPT_PATH)); |
| 276 } | 281 } |
| 277 | 282 |
| 278 } // namespace component_updater | 283 } // namespace component_updater |
| OLD | NEW |