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

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: merge 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 };
Ryan Sleevi 2014/09/03 00:23:31 Document? To be quite honest, I'm not sure why th
tommycli 2014/09/05 15:03:08 I looked into this. net::URLRequestFileJob always
38
39 } // namespace
40
41 class TestURLRequestInterceptor::Delegate : public net::URLRequestInterceptor {
Ryan Sleevi 2014/09/03 00:23:31 Briefly, document.
tommycli 2014/09/05 15:03:08 Done.
42 public:
43 Delegate(const std::string& scheme,
44 const std::string& hostname,
45 const scoped_refptr<base::TaskRunner>& network_task_runner,
46 const scoped_refptr<base::TaskRunner>& worker_task_runner)
47 : scheme_(scheme),
48 hostname_(hostname),
49 network_task_runner_(network_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);
Ryan Sleevi 2014/09/03 00:23:31 Paul should review this, as it seems there's some
pauljensen 2014/09/03 11:52:51 This is WAI; seems fine.
tommycli 2014/09/05 15:03:08 Done.
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(network_task_runner_->RunsTasksOnCurrentThread());
72 // It's ok to do a blocking disk access on this thread; this class
73 // is just used for tests.
Ryan Sleevi 2014/09/03 00:23:31 // Note: Actual loading of the file still occurs o
tommycli 2014/09/05 15:03:08 I removed the ScopedAllowIO without any apparent b
74 base::ThreadRestrictions::ScopedAllowIO allow_io;
75 EXPECT_TRUE(base::PathExists(path));
Ryan Sleevi 2014/09/03 00:23:31 This EXPECT_TRUE is (largely) meaningless and won'
tommycli 2014/09/05 15:03:08 Done.
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(network_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> network_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>& network_task_runner,
143 const scoped_refptr<base::TaskRunner>& worker_task_runner)
144 : scheme_(scheme),
145 hostname_(hostname),
146 network_task_runner_(network_task_runner),
147 delegate_(new Delegate(scheme,
148 hostname,
149 network_task_runner_,
150 worker_task_runner)) {
151 network_task_runner_->PostTask(
152 FROM_HERE, base::Bind(&Delegate::Register, base::Unretained(delegate_)));
153 }
154
155 TestURLRequestInterceptor::~TestURLRequestInterceptor() {
Ryan Sleevi 2014/09/03 00:23:31 BUG: This does not guarantee that |delegate_| will
tommycli 2014/09/05 15:03:08 I thought |delegate_| was simply never deleted her
156 network_task_runner_->PostTask(
157 FROM_HERE, base::Bind(&Delegate::Unregister, scheme_, hostname_));
158 }
159
160 void TestURLRequestInterceptor::SetResponse(const GURL& url,
161 const base::FilePath& path) {
162 CHECK_EQ(scheme_, url.scheme());
163 CHECK_EQ(hostname_, url.host());
164 network_task_runner_->PostTask(FROM_HERE,
165 base::Bind(&Delegate::SetResponse,
166 base::Unretained(delegate_),
167 url,
168 path,
169 false));
170 }
171
172 void TestURLRequestInterceptor::SetResponseIgnoreQuery(
173 const GURL& url,
174 const base::FilePath& path) {
175 CHECK_EQ(scheme_, url.scheme());
176 CHECK_EQ(hostname_, url.host());
177 network_task_runner_->PostTask(FROM_HERE,
178 base::Bind(&Delegate::SetResponse,
179 base::Unretained(delegate_),
180 url,
181 path,
182 true));
183 }
184
185 int TestURLRequestInterceptor::GetHitCount() {
186 return delegate_->GetHitCount();
187 }
188
189 LocalHostTestURLRequestInterceptor::LocalHostTestURLRequestInterceptor(
190 const scoped_refptr<base::TaskRunner>& network_task_runner,
191 const scoped_refptr<base::TaskRunner>& worker_task_runner)
192 : TestURLRequestInterceptor("http",
193 "localhost",
194 network_task_runner,
195 worker_task_runner) {
196 }
197
198 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698