Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: net/url_request/test_url_request_interceptor.cc

Issue 508473002: Componentize component_updater: Move URLRequestPrepackagedInterceptor from content/ to net/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "net/url_request/test_url_request_interceptor.h"
6
7 #include "base/file_util.h"
8 #include "base/threading/sequenced_worker_pool.h"
9 #include "base/threading/thread_restrictions.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 "net/url_request/url_request_interceptor.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace net {
17
18 namespace {
19
20 class TestURLRequestJob : public net::URLRequestFileJob {
21 public:
22 TestURLRequestJob(net::URLRequest* request,
23 net::NetworkDelegate* network_delegate,
24 const base::FilePath& file_path,
25 const scoped_refptr<base::TaskRunner>& worker_task_runner)
26 : net::URLRequestFileJob(request,
27 network_delegate,
28 file_path,
29 worker_task_runner) {}
30
31 virtual int GetResponseCode() const OVERRIDE { return 200; }
32
33 private:
34 virtual ~TestURLRequestJob() {}
35
36 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJob);
37 };
38
39 } // namespace
40
41 class TestURLRequestInterceptor::Delegate : public net::URLRequestInterceptor {
42 public:
43 Delegate(const std::string& scheme,
44 const std::string& hostname,
45 const scoped_refptr<base::TaskRunner>& io_task_runner,
46 const scoped_refptr<base::TaskRunner>& worker_task_runner)
47 : scheme_(scheme),
48 hostname_(hostname),
49 io_task_runner_(io_task_runner),
50 worker_task_runner_(worker_task_runner),
51 hit_count_(0) {}
52 virtual ~Delegate() {}
53
54 void Register() {
55 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
56 scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this));
57 }
58
59 static void Unregister(const std::string& scheme,
60 const std::string& hostname) {
61 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme,
62 hostname);
63 }
64
65 // When requests for |url| arrive, respond with the contents of |path|. The
66 // hostname and scheme of |url| must match the corresponding parameters
67 // passed as constructor arguments.
68 void SetResponse(const GURL& url,
69 const base::FilePath& path,
70 bool ignore_query) {
71 DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
72 // It's ok to do a blocking disk access on this thread; this class
73 // is just used for tests.
74 base::ThreadRestrictions::ScopedAllowIO allow_io;
75 EXPECT_TRUE(base::PathExists(path));
76 if (ignore_query) {
77 ignore_query_responses_[url] = path;
78 } else {
79 responses_[url] = path;
80 }
81 }
82
83 // Returns how many requests have been issued that have a stored reply.
84 int GetHitCount() const {
85 base::AutoLock auto_lock(hit_count_lock_);
86 return hit_count_;
87 }
88
89 private:
90 typedef std::map<GURL, base::FilePath> ResponseMap;
91
92 // When computing matches, this ignores the query parameters of the url.
93 virtual net::URLRequestJob* MaybeInterceptRequest(
94 net::URLRequest* request,
95 net::NetworkDelegate* network_delegate) const OVERRIDE {
96 DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
97 if (request->url().scheme() != scheme_ ||
98 request->url().host() != hostname_) {
99 return NULL;
100 }
101
102 ResponseMap::const_iterator it = responses_.find(request->url());
103 if (it == responses_.end()) {
104 // Search for this request's url, ignoring any query parameters.
105 GURL url = request->url();
106 if (url.has_query()) {
107 GURL::Replacements replacements;
108 replacements.ClearQuery();
109 url = url.ReplaceComponents(replacements);
110 }
111 it = ignore_query_responses_.find(url);
112 if (it == ignore_query_responses_.end())
113 return NULL;
114 }
115 {
116 base::AutoLock auto_lock(hit_count_lock_);
117 ++hit_count_;
118 }
119
120 return new TestURLRequestJob(
121 request, network_delegate, it->second, worker_task_runner_);
122 }
123
124 const std::string scheme_;
125 const std::string hostname_;
126
127 const scoped_refptr<base::TaskRunner> io_task_runner_;
128 const scoped_refptr<base::TaskRunner> worker_task_runner_;
129
130 ResponseMap responses_;
131 ResponseMap ignore_query_responses_;
132
133 mutable base::Lock hit_count_lock_;
134 mutable int hit_count_;
135
136 DISALLOW_COPY_AND_ASSIGN(Delegate);
137 };
138
139 TestURLRequestInterceptor::TestURLRequestInterceptor(
140 const std::string& scheme,
141 const std::string& hostname,
142 const scoped_refptr<base::TaskRunner>& io_task_runner,
143 const scoped_refptr<base::TaskRunner>& worker_task_runner)
144 : scheme_(scheme),
145 hostname_(hostname),
146 io_task_runner_(io_task_runner),
147 delegate_(
148 new Delegate(scheme, hostname, io_task_runner_, worker_task_runner)) {
149 io_task_runner_->PostTask(
150 FROM_HERE, base::Bind(&Delegate::Register, base::Unretained(delegate_)));
151 }
152
153 TestURLRequestInterceptor::~TestURLRequestInterceptor() {
154 io_task_runner_->PostTask(
155 FROM_HERE, base::Bind(&Delegate::Unregister, scheme_, hostname_));
156 }
157
158 void TestURLRequestInterceptor::SetResponse(const GURL& url,
159 const base::FilePath& path) {
160 CHECK_EQ(scheme_, url.scheme());
161 CHECK_EQ(hostname_, url.host());
162 io_task_runner_->PostTask(FROM_HERE,
163 base::Bind(&Delegate::SetResponse,
164 base::Unretained(delegate_),
165 url,
166 path,
167 false));
168 }
169
170 void TestURLRequestInterceptor::SetResponseIgnoreQuery(
171 const GURL& url,
172 const base::FilePath& path) {
173 CHECK_EQ(scheme_, url.scheme());
174 CHECK_EQ(hostname_, url.host());
175 io_task_runner_->PostTask(FROM_HERE,
176 base::Bind(&Delegate::SetResponse,
177 base::Unretained(delegate_),
178 url,
179 path,
180 true));
181 }
182
183 int TestURLRequestInterceptor::GetHitCount() {
184 return delegate_->GetHitCount();
185 }
186
187 LocalHostTestURLRequestInterceptor::LocalHostTestURLRequestInterceptor(
188 const scoped_refptr<base::TaskRunner>& io_task_runner,
189 const scoped_refptr<base::TaskRunner>& worker_task_runner)
190 : TestURLRequestInterceptor("http",
191 "localhost",
192 io_task_runner,
193 worker_task_runner) {
194 }
195
196 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698