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.
5 #ifndef NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_
6 #define NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
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
{
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();
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
);
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
);
51 // Returns how many requests have been issued that have a stored reply.
57 const std::string scheme_
;
58 const std::string hostname_
;
60 scoped_refptr
<base::TaskRunner
> network_task_runner_
;
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().
67 DISALLOW_COPY_AND_ASSIGN(TestURLRequestInterceptor
);
70 // Specialization of TestURLRequestInterceptor where scheme is "http" and
71 // hostname is "localhost".
72 class LocalHostTestURLRequestInterceptor
: public TestURLRequestInterceptor
{
74 LocalHostTestURLRequestInterceptor(
75 const scoped_refptr
<base::TaskRunner
>& network_task_runner
,
76 const scoped_refptr
<base::TaskRunner
>& worker_task_runner
);
79 DISALLOW_COPY_AND_ASSIGN(LocalHostTestURLRequestInterceptor
);
84 #endif // NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_