Fix build break
[chromium-blink-merge.git] / content / test / net / url_request_prepackaged_interceptor.cc
blobf8603ef5de0f4e56b49a8e8710b5b52f8255fea2
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/thread_restrictions.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "net/url_request/url_request.h"
11 #include "net/url_request/url_request_file_job.h"
12 #include "net/url_request/url_request_filter.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using content::BrowserThread;
17 namespace content {
19 namespace {
21 class URLRequestPrepackagedJob : public net::URLRequestFileJob {
22 public:
23 URLRequestPrepackagedJob(net::URLRequest* request,
24 net::NetworkDelegate* network_delegate,
25 const base::FilePath& file_path)
26 : net::URLRequestFileJob(request, network_delegate, file_path) {}
28 virtual int GetResponseCode() const OVERRIDE { return 200; }
30 private:
31 virtual ~URLRequestPrepackagedJob() {}
33 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob);
36 } // namespace
38 class URLRequestPrepackagedInterceptor::Delegate
39 : public net::URLRequestJobFactory::ProtocolHandler {
40 public:
41 Delegate() : hit_count_(0) {}
42 virtual ~Delegate() {}
44 void Register() {
45 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler(
46 "http", "localhost",
47 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(this));
50 static void Unregister() {
51 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http",
52 "localhost");
55 // When requests for |url| arrive, respond with the contents of |path|. The
56 // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme
57 // must be "http".
58 void SetResponse(const GURL& url,
59 const base::FilePath& path,
60 bool ignore_query) {
61 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
62 // It's ok to do a blocking disk access on this thread; this class
63 // is just used for tests.
64 base::ThreadRestrictions::ScopedAllowIO allow_io;
65 EXPECT_TRUE(file_util::PathExists(path));
66 if (ignore_query) {
67 ignore_query_responses_[url] = path;
68 } else {
69 responses_[url] = path;
73 // Returns how many requests have been issued that have a stored reply.
74 int GetHitCount() const {
75 base::AutoLock auto_lock(hit_count_lock_);
76 return hit_count_;
79 private:
80 typedef std::map<GURL, base::FilePath> ResponseMap;
82 // When computing matches, this ignores the query parameters of the url.
83 virtual net::URLRequestJob* MaybeCreateJob(
84 net::URLRequest* request,
85 net::NetworkDelegate* network_delegate) const OVERRIDE {
86 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
87 if (request->url().scheme() != "http" ||
88 request->url().host() != "localhost") {
89 return NULL;
92 ResponseMap::const_iterator it = responses_.find(request->url());
93 if (it == responses_.end()) {
94 // Search for this request's url, ignoring any query parameters.
95 GURL url = request->url();
96 if (url.has_query()) {
97 GURL::Replacements replacements;
98 replacements.ClearQuery();
99 url = url.ReplaceComponents(replacements);
101 it = ignore_query_responses_.find(url);
102 if (it == ignore_query_responses_.end())
103 return NULL;
106 base::AutoLock auto_lock(hit_count_lock_);
107 ++hit_count_;
110 return new URLRequestPrepackagedJob(request,
111 network_delegate,
112 it->second);
115 ResponseMap responses_;
116 ResponseMap ignore_query_responses_;
118 mutable base::Lock hit_count_lock_;
119 mutable int hit_count_;
121 DISALLOW_COPY_AND_ASSIGN(Delegate);
125 URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor()
126 : delegate_(new Delegate) {
127 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
128 base::Bind(&Delegate::Register,
129 base::Unretained(delegate_)));
132 URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() {
133 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
134 base::Bind(&Delegate::Unregister));
137 void URLRequestPrepackagedInterceptor::SetResponse(const GURL& url,
138 const base::FilePath& path) {
139 CHECK_EQ("http", url.scheme());
140 CHECK_EQ("localhost", url.host());
141 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
142 base::Bind(&Delegate::SetResponse,
143 base::Unretained(delegate_), url, path,
144 false));
147 void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery(
148 const GURL& url,
149 const base::FilePath& path) {
150 CHECK_EQ("http", url.scheme());
151 CHECK_EQ("localhost", url.host());
152 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
153 base::Bind(&Delegate::SetResponse,
154 base::Unretained(delegate_), url, path,
155 true));
158 int URLRequestPrepackagedInterceptor::GetHitCount() {
159 return delegate_->GetHitCount();
162 } // namespace content