OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_ |
| 6 #define NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 |
| 13 class GURL; |
| 14 |
| 15 namespace base { |
| 16 class FilePath; |
| 17 class TaskRunner; |
| 18 } |
| 19 |
| 20 namespace net { |
| 21 |
| 22 // Intercepts HTTP requests and gives pre-defined responses to specified URLs. |
| 23 // The pre-defined responses are loaded from files on disk. The interception |
| 24 // occurs while the TestURLRequestInterceptor is alive. This class may be |
| 25 // instantiated on any thread. |
| 26 class TestURLRequestInterceptor { |
| 27 public: |
| 28 // Registers an interceptor for URLs using |scheme| and |hostname|. URLs |
| 29 // passed to "SetResponse" are required to use |scheme| and |hostname|. |
| 30 // |network_task_runner| is the task runner used for network activity |
| 31 // (e.g. where URL requests are processed). |
| 32 // |worker_task_runner| will be used to read the files specified by |
| 33 // either SetResponse() or SetResponseIgnoreQuery() asynchronously. It |
| 34 // must be a task runner allowed to perform disk IO. |
| 35 TestURLRequestInterceptor( |
| 36 const std::string& scheme, |
| 37 const std::string& hostname, |
| 38 const scoped_refptr<base::TaskRunner>& network_task_runner, |
| 39 const scoped_refptr<base::TaskRunner>& worker_task_runner); |
| 40 virtual ~TestURLRequestInterceptor(); |
| 41 |
| 42 // When requests for |url| arrive, respond with the contents of |path|. The |
| 43 // hostname and scheme of |url| must match the corresponding parameters |
| 44 // passed as constructor arguments. |
| 45 void SetResponse(const GURL& url, const base::FilePath& path); |
| 46 |
| 47 // Identical to SetResponse, except that query parameters are ignored on |
| 48 // incoming URLs when comparing against |url|. |
| 49 void SetResponseIgnoreQuery(const GURL& url, const base::FilePath& path); |
| 50 |
| 51 // Returns how many requests have been issued that have a stored reply. |
| 52 int GetHitCount(); |
| 53 |
| 54 private: |
| 55 class Delegate; |
| 56 |
| 57 const std::string scheme_; |
| 58 const std::string hostname_; |
| 59 |
| 60 scoped_refptr<base::TaskRunner> network_task_runner_; |
| 61 |
| 62 // After creation, |delegate_| lives on the thread of the |
| 63 // |network_task_runner_|, and a task to delete it is posted from |
| 64 // ~TestURLRequestInterceptor(). |
| 65 Delegate* delegate_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(TestURLRequestInterceptor); |
| 68 }; |
| 69 |
| 70 // Specialization of TestURLRequestInterceptor where scheme is "http" and |
| 71 // hostname is "localhost". |
| 72 class LocalHostTestURLRequestInterceptor : public TestURLRequestInterceptor { |
| 73 public: |
| 74 LocalHostTestURLRequestInterceptor( |
| 75 const scoped_refptr<base::TaskRunner>& network_task_runner, |
| 76 const scoped_refptr<base::TaskRunner>& worker_task_runner); |
| 77 |
| 78 private: |
| 79 DISALLOW_COPY_AND_ASSIGN(LocalHostTestURLRequestInterceptor); |
| 80 }; |
| 81 |
| 82 } // namespace net |
| 83 |
| 84 #endif // NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_ |
OLD | NEW |