Index: net/url_request/test_url_request_interceptor.h |
diff --git a/net/url_request/test_url_request_interceptor.h b/net/url_request/test_url_request_interceptor.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..26f5c9ededcd43569c1fc197f1ecd2535b30e14a |
--- /dev/null |
+++ b/net/url_request/test_url_request_interceptor.h |
@@ -0,0 +1,81 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_ |
+#define NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_ |
+ |
+#include <string> |
+ |
+#include "base/basictypes.h" |
+#include "base/memory/ref_counted.h" |
+ |
+class GURL; |
+ |
+namespace base { |
+class FilePath; |
+class TaskRunner; |
+} |
+ |
+namespace net { |
+ |
+// Intercepts HTTP requests and gives pre-defined responses to specified URLs. |
+// The pre-defined responses are loaded from files on disk. The interception |
+// occurs while the TestURLRequestInterceptor is alive. This class may be |
+// instantiated on any thread. This is unlike most other classes in /net, |
+// which may only be instantiated on the |network_task_runner| thread. We allow |
+// instantiation on any thread for convenient usage on test threads. |
Ryan Sleevi
2014/09/03 00:23:31
1) Don't use pronouns in comments ("We" allow)
2)
tommycli
2014/09/05 15:03:08
Done.
|
+class TestURLRequestInterceptor { |
+ public: |
+ // Registers an interceptor for urls using |scheme| and |hostname|. Urls |
Ryan Sleevi
2014/09/03 00:23:31
s/urls/URLs/
s/Urls/URLs/
tommycli
2014/09/05 15:03:08
Done.
|
+ // passed to "SetResponse" are required to use |scheme| and |hostname|. |
+ TestURLRequestInterceptor( |
+ const std::string& scheme, |
+ const std::string& hostname, |
+ const scoped_refptr<base::TaskRunner>& network_task_runner, |
+ const scoped_refptr<base::TaskRunner>& worker_task_runner); |
Ryan Sleevi
2014/09/03 00:23:31
Document both |network_task_runner| and |worker_ta
tommycli
2014/09/05 15:03:08
Done.
|
+ virtual ~TestURLRequestInterceptor(); |
+ |
+ // When requests for |url| arrive, respond with the contents of |path|. The |
+ // hostname and scheme of |url| must match the corresponding parameters |
+ // passed as constructor arguments. |
+ void SetResponse(const GURL& url, const base::FilePath& path); |
+ |
+ // Identical to SetResponse except that query parameters are ignored on |
Ryan Sleevi
2014/09/03 00:23:31
s/SetResponse/SetResponse,/
tommycli
2014/09/05 15:03:08
Done.
|
+ // incoming URLs when comparing against |url|. |
+ void SetResponseIgnoreQuery(const GURL& url, const base::FilePath& path); |
+ |
+ // Returns how many requests have been issued that have a stored reply. |
+ int GetHitCount(); |
+ |
+ private: |
+ class Delegate; |
+ |
+ const std::string scheme_; |
+ const std::string hostname_; |
+ |
+ scoped_refptr<base::TaskRunner> network_task_runner_; |
+ |
+ // After creation, |delegate_| lives on the thread of the |
+ // |network_task_runner_|, and a task to delete it is posted from |
+ // ~TestURLRequestInterceptor(). |
+ Delegate* delegate_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestURLRequestInterceptor); |
+}; |
+ |
+// Specialization of TestURLRequestInterceptor where scheme is "http" and |
+// hostname is "localhost". |
+class LocalHostTestURLRequestInterceptor : public TestURLRequestInterceptor { |
+ public: |
+ LocalHostTestURLRequestInterceptor( |
+ const scoped_refptr<base::TaskRunner>& network_task_runner, |
+ const scoped_refptr<base::TaskRunner>& worker_task_runner); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(LocalHostTestURLRequestInterceptor); |
+}; |
+ |
+} // namespace net |
+ |
+#endif // NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_ |