| OLD | NEW |
| 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 #include "net/url_request/test_url_request_interceptor.h" | 5 #include "net/url_request/test_url_request_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/threading/sequenced_worker_pool.h" | 9 #include "base/threading/sequenced_worker_pool.h" |
| 10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 const std::string& hostname) { | 70 const std::string& hostname) { |
| 71 URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme, hostname); | 71 URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme, hostname); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // When requests for |url| arrive, respond with the contents of |path|. The | 74 // When requests for |url| arrive, respond with the contents of |path|. The |
| 75 // hostname and scheme of |url| must match the corresponding parameters | 75 // hostname and scheme of |url| must match the corresponding parameters |
| 76 // passed as constructor arguments. | 76 // passed as constructor arguments. |
| 77 void SetResponse(const GURL& url, | 77 void SetResponse(const GURL& url, |
| 78 const base::FilePath& path, | 78 const base::FilePath& path, |
| 79 bool ignore_query) { | 79 bool ignore_query) { |
| 80 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 80 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); |
| 81 if (ignore_query) { | 81 if (ignore_query) { |
| 82 ignore_query_responses_[url] = path; | 82 ignore_query_responses_[url] = path; |
| 83 } else { | 83 } else { |
| 84 responses_[url] = path; | 84 responses_[url] = path; |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 // Returns how many requests have been issued that have a stored reply. | 88 // Returns how many requests have been issued that have a stored reply. |
| 89 int GetHitCount() const { | 89 int GetHitCount() const { |
| 90 base::AutoLock auto_lock(hit_count_lock_); | 90 base::AutoLock auto_lock(hit_count_lock_); |
| 91 return hit_count_; | 91 return hit_count_; |
| 92 } | 92 } |
| 93 | 93 |
| 94 private: | 94 private: |
| 95 typedef std::map<GURL, base::FilePath> ResponseMap; | 95 typedef std::map<GURL, base::FilePath> ResponseMap; |
| 96 | 96 |
| 97 // When computing matches, this ignores the query parameters of the url. | 97 // When computing matches, this ignores the query parameters of the url. |
| 98 URLRequestJob* MaybeInterceptRequest( | 98 URLRequestJob* MaybeInterceptRequest( |
| 99 URLRequest* request, | 99 URLRequest* request, |
| 100 NetworkDelegate* network_delegate) const override { | 100 NetworkDelegate* network_delegate) const override { |
| 101 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 101 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); |
| 102 if (request->url().scheme() != scheme_ || | 102 if (request->url().scheme() != scheme_ || |
| 103 request->url().host() != hostname_) { | 103 request->url().host() != hostname_) { |
| 104 return NULL; | 104 return NULL; |
| 105 } | 105 } |
| 106 | 106 |
| 107 ResponseMap::const_iterator it = responses_.find(request->url()); | 107 ResponseMap::const_iterator it = responses_.find(request->url()); |
| 108 if (it == responses_.end()) { | 108 if (it == responses_.end()) { |
| 109 // Search for this request's url, ignoring any query parameters. | 109 // Search for this request's url, ignoring any query parameters. |
| 110 GURL url = request->url(); | 110 GURL url = request->url(); |
| 111 if (url.has_query()) { | 111 if (url.has_query()) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 LocalHostTestURLRequestInterceptor::LocalHostTestURLRequestInterceptor( | 194 LocalHostTestURLRequestInterceptor::LocalHostTestURLRequestInterceptor( |
| 195 const scoped_refptr<base::TaskRunner>& network_task_runner, | 195 const scoped_refptr<base::TaskRunner>& network_task_runner, |
| 196 const scoped_refptr<base::TaskRunner>& worker_task_runner) | 196 const scoped_refptr<base::TaskRunner>& worker_task_runner) |
| 197 : TestURLRequestInterceptor("http", | 197 : TestURLRequestInterceptor("http", |
| 198 "localhost", | 198 "localhost", |
| 199 network_task_runner, | 199 network_task_runner, |
| 200 worker_task_runner) { | 200 worker_task_runner) { |
| 201 } | 201 } |
| 202 | 202 |
| 203 } // namespace net | 203 } // namespace net |
| OLD | NEW |