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 #include "content/test/net/url_request_prepackaged_interceptor.h"
7 #include "base/file_util.h"
8 #include "base/threading/sequenced_worker_pool.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "net/url_request/url_request.h"
12 #include "net/url_request/url_request_file_job.h"
13 #include "net/url_request/url_request_filter.h"
14 #include "net/url_request/url_request_interceptor.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using content::BrowserThread
;
23 class URLRequestPrepackagedJob
: public net::URLRequestFileJob
{
25 URLRequestPrepackagedJob(net::URLRequest
* request
,
26 net::NetworkDelegate
* network_delegate
,
27 const base::FilePath
& file_path
)
28 : net::URLRequestFileJob(
29 request
, network_delegate
, file_path
,
30 content::BrowserThread::GetBlockingPool()->
31 GetTaskRunnerWithShutdownBehavior(
32 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
)) {}
34 virtual int GetResponseCode() const OVERRIDE
{ return 200; }
37 virtual ~URLRequestPrepackagedJob() {}
39 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob
);
44 class URLRequestPrepackagedInterceptor::Delegate
45 : public net::URLRequestInterceptor
{
47 Delegate(const std::string
& scheme
, const std::string
& hostname
)
48 : scheme_(scheme
), hostname_(hostname
), hit_count_(0) {}
49 virtual ~Delegate() {}
52 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
54 scoped_ptr
<net::URLRequestInterceptor
>(this));
57 static void Unregister(
58 const std::string
& scheme
,
59 const std::string
& hostname
) {
60 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme
,
64 // When requests for |url| arrive, respond with the contents of |path|. The
65 // hostname and scheme of |url| must match the corresponding parameters
66 // passed as constructor arguments.
67 void SetResponse(const GURL
& url
,
68 const base::FilePath
& path
,
70 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
71 // It's ok to do a blocking disk access on this thread; this class
72 // is just used for tests.
73 base::ThreadRestrictions::ScopedAllowIO allow_io
;
74 EXPECT_TRUE(base::PathExists(path
));
76 ignore_query_responses_
[url
] = path
;
78 responses_
[url
] = path
;
82 // Returns how many requests have been issued that have a stored reply.
83 int GetHitCount() const {
84 base::AutoLock
auto_lock(hit_count_lock_
);
89 typedef std::map
<GURL
, base::FilePath
> ResponseMap
;
91 // When computing matches, this ignores the query parameters of the url.
92 virtual net::URLRequestJob
* MaybeInterceptRequest(
93 net::URLRequest
* request
,
94 net::NetworkDelegate
* network_delegate
) const OVERRIDE
{
95 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
96 if (request
->url().scheme() != scheme_
||
97 request
->url().host() != hostname_
) {
101 ResponseMap::const_iterator it
= responses_
.find(request
->url());
102 if (it
== responses_
.end()) {
103 // Search for this request's url, ignoring any query parameters.
104 GURL url
= request
->url();
105 if (url
.has_query()) {
106 GURL::Replacements replacements
;
107 replacements
.ClearQuery();
108 url
= url
.ReplaceComponents(replacements
);
110 it
= ignore_query_responses_
.find(url
);
111 if (it
== ignore_query_responses_
.end())
115 base::AutoLock
auto_lock(hit_count_lock_
);
119 return new URLRequestPrepackagedJob(request
,
124 const std::string scheme_
;
125 const std::string hostname_
;
127 ResponseMap responses_
;
128 ResponseMap ignore_query_responses_
;
130 mutable base::Lock hit_count_lock_
;
131 mutable int hit_count_
;
133 DISALLOW_COPY_AND_ASSIGN(Delegate
);
137 URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor(
138 const std::string
& scheme
,
139 const std::string
& hostname
)
142 delegate_(new Delegate(scheme
, hostname
)) {
143 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
144 base::Bind(&Delegate::Register
,
145 base::Unretained(delegate_
)));
148 URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() {
149 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
150 base::Bind(&Delegate::Unregister
,
155 void URLRequestPrepackagedInterceptor::SetResponse(
157 const base::FilePath
& path
) {
158 CHECK_EQ(scheme_
, url
.scheme());
159 CHECK_EQ(hostname_
, url
.host());
160 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
161 base::Bind(&Delegate::SetResponse
,
162 base::Unretained(delegate_
), url
, path
,
166 void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery(
168 const base::FilePath
& path
) {
169 CHECK_EQ(scheme_
, url
.scheme());
170 CHECK_EQ(hostname_
, url
.host());
171 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
172 base::Bind(&Delegate::SetResponse
,
173 base::Unretained(delegate_
), url
, path
,
177 int URLRequestPrepackagedInterceptor::GetHitCount() {
178 return delegate_
->GetHitCount();
182 URLLocalHostRequestPrepackagedInterceptor
183 ::URLLocalHostRequestPrepackagedInterceptor()
184 : URLRequestPrepackagedInterceptor("http", "localhost") {
187 } // namespace content