OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/test/net/url_request_prepackaged_interceptor.h" | 5 #include "net/url_request/test_url_request_interceptor.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/threading/sequenced_worker_pool.h" | 8 #include "base/threading/sequenced_worker_pool.h" |
9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
10 #include "content/public/browser/browser_thread.h" | |
11 #include "net/url_request/url_request.h" | 10 #include "net/url_request/url_request.h" |
12 #include "net/url_request/url_request_file_job.h" | 11 #include "net/url_request/url_request_file_job.h" |
13 #include "net/url_request/url_request_filter.h" | 12 #include "net/url_request/url_request_filter.h" |
14 #include "net/url_request/url_request_interceptor.h" | 13 #include "net/url_request/url_request_interceptor.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
16 | 15 |
17 using content::BrowserThread; | 16 namespace net { |
18 | |
19 namespace content { | |
20 | 17 |
21 namespace { | 18 namespace { |
22 | 19 |
23 class URLRequestPrepackagedJob : public net::URLRequestFileJob { | 20 class URLRequestPrepackagedJob : public net::URLRequestFileJob { |
blundell
2014/08/27 13:02:27
Should this "Prepackaged" also go away?
tommycli
2014/08/27 15:41:11
Done.
| |
24 public: | 21 public: |
25 URLRequestPrepackagedJob(net::URLRequest* request, | 22 URLRequestPrepackagedJob( |
26 net::NetworkDelegate* network_delegate, | 23 net::URLRequest* request, |
27 const base::FilePath& file_path) | 24 net::NetworkDelegate* network_delegate, |
28 : net::URLRequestFileJob( | 25 const base::FilePath& file_path, |
29 request, network_delegate, file_path, | 26 const scoped_refptr<base::TaskRunner>& worker_task_runner) |
30 content::BrowserThread::GetBlockingPool()-> | 27 : net::URLRequestFileJob(request, |
31 GetTaskRunnerWithShutdownBehavior( | 28 network_delegate, |
32 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {} | 29 file_path, |
30 worker_task_runner) {} | |
33 | 31 |
34 virtual int GetResponseCode() const OVERRIDE { return 200; } | 32 virtual int GetResponseCode() const OVERRIDE { return 200; } |
35 | 33 |
36 private: | 34 private: |
37 virtual ~URLRequestPrepackagedJob() {} | 35 virtual ~URLRequestPrepackagedJob() {} |
38 | 36 |
39 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob); | 37 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob); |
40 }; | 38 }; |
41 | 39 |
42 } // namespace | 40 } // namespace |
43 | 41 |
44 class URLRequestPrepackagedInterceptor::Delegate | 42 class TestURLRequestInterceptor::Delegate : public net::URLRequestInterceptor { |
45 : public net::URLRequestInterceptor { | |
46 public: | 43 public: |
47 Delegate(const std::string& scheme, const std::string& hostname) | 44 Delegate(const std::string& scheme, |
48 : scheme_(scheme), hostname_(hostname), hit_count_(0) {} | 45 const std::string& hostname, |
46 const scoped_refptr<base::TaskRunner>& io_task_runner, | |
47 const scoped_refptr<base::TaskRunner>& worker_task_runner) | |
48 : scheme_(scheme), | |
49 hostname_(hostname), | |
50 io_task_runner_(io_task_runner), | |
51 worker_task_runner_(worker_task_runner), | |
52 hit_count_(0) {} | |
49 virtual ~Delegate() {} | 53 virtual ~Delegate() {} |
50 | 54 |
51 void Register() { | 55 void Register() { |
52 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( | 56 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
53 scheme_, hostname_, | 57 scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this)); |
54 scoped_ptr<net::URLRequestInterceptor>(this)); | |
55 } | 58 } |
56 | 59 |
57 static void Unregister( | 60 static void Unregister(const std::string& scheme, |
58 const std::string& scheme, | 61 const std::string& hostname) { |
59 const std::string& hostname) { | |
60 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme, | 62 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme, |
61 hostname); | 63 hostname); |
62 } | 64 } |
63 | 65 |
64 // When requests for |url| arrive, respond with the contents of |path|. The | 66 // When requests for |url| arrive, respond with the contents of |path|. The |
65 // hostname and scheme of |url| must match the corresponding parameters | 67 // hostname and scheme of |url| must match the corresponding parameters |
66 // passed as constructor arguments. | 68 // passed as constructor arguments. |
67 void SetResponse(const GURL& url, | 69 void SetResponse(const GURL& url, |
68 const base::FilePath& path, | 70 const base::FilePath& path, |
69 bool ignore_query) { | 71 bool ignore_query) { |
70 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 72 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
71 // It's ok to do a blocking disk access on this thread; this class | 73 // It's ok to do a blocking disk access on this thread; this class |
72 // is just used for tests. | 74 // is just used for tests. |
73 base::ThreadRestrictions::ScopedAllowIO allow_io; | 75 base::ThreadRestrictions::ScopedAllowIO allow_io; |
74 EXPECT_TRUE(base::PathExists(path)); | 76 EXPECT_TRUE(base::PathExists(path)); |
75 if (ignore_query) { | 77 if (ignore_query) { |
76 ignore_query_responses_[url] = path; | 78 ignore_query_responses_[url] = path; |
77 } else { | 79 } else { |
78 responses_[url] = path; | 80 responses_[url] = path; |
79 } | 81 } |
80 } | 82 } |
81 | 83 |
82 // Returns how many requests have been issued that have a stored reply. | 84 // Returns how many requests have been issued that have a stored reply. |
83 int GetHitCount() const { | 85 int GetHitCount() const { |
84 base::AutoLock auto_lock(hit_count_lock_); | 86 base::AutoLock auto_lock(hit_count_lock_); |
85 return hit_count_; | 87 return hit_count_; |
86 } | 88 } |
87 | 89 |
88 private: | 90 private: |
89 typedef std::map<GURL, base::FilePath> ResponseMap; | 91 typedef std::map<GURL, base::FilePath> ResponseMap; |
90 | 92 |
91 // When computing matches, this ignores the query parameters of the url. | 93 // When computing matches, this ignores the query parameters of the url. |
92 virtual net::URLRequestJob* MaybeInterceptRequest( | 94 virtual net::URLRequestJob* MaybeInterceptRequest( |
93 net::URLRequest* request, | 95 net::URLRequest* request, |
94 net::NetworkDelegate* network_delegate) const OVERRIDE { | 96 net::NetworkDelegate* network_delegate) const OVERRIDE { |
95 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 97 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
96 if (request->url().scheme() != scheme_ || | 98 if (request->url().scheme() != scheme_ || |
97 request->url().host() != hostname_) { | 99 request->url().host() != hostname_) { |
98 return NULL; | 100 return NULL; |
99 } | 101 } |
100 | 102 |
101 ResponseMap::const_iterator it = responses_.find(request->url()); | 103 ResponseMap::const_iterator it = responses_.find(request->url()); |
102 if (it == responses_.end()) { | 104 if (it == responses_.end()) { |
103 // Search for this request's url, ignoring any query parameters. | 105 // Search for this request's url, ignoring any query parameters. |
104 GURL url = request->url(); | 106 GURL url = request->url(); |
105 if (url.has_query()) { | 107 if (url.has_query()) { |
106 GURL::Replacements replacements; | 108 GURL::Replacements replacements; |
107 replacements.ClearQuery(); | 109 replacements.ClearQuery(); |
108 url = url.ReplaceComponents(replacements); | 110 url = url.ReplaceComponents(replacements); |
109 } | 111 } |
110 it = ignore_query_responses_.find(url); | 112 it = ignore_query_responses_.find(url); |
111 if (it == ignore_query_responses_.end()) | 113 if (it == ignore_query_responses_.end()) |
112 return NULL; | 114 return NULL; |
113 } | 115 } |
114 { | 116 { |
115 base::AutoLock auto_lock(hit_count_lock_); | 117 base::AutoLock auto_lock(hit_count_lock_); |
116 ++hit_count_; | 118 ++hit_count_; |
117 } | 119 } |
118 | 120 |
119 return new URLRequestPrepackagedJob(request, | 121 return new URLRequestPrepackagedJob( |
120 network_delegate, | 122 request, network_delegate, it->second, worker_task_runner_); |
121 it->second); | |
122 } | 123 } |
123 | 124 |
124 const std::string scheme_; | 125 const std::string scheme_; |
125 const std::string hostname_; | 126 const std::string hostname_; |
126 | 127 |
128 const scoped_refptr<base::TaskRunner> io_task_runner_; | |
129 const scoped_refptr<base::TaskRunner> worker_task_runner_; | |
130 | |
127 ResponseMap responses_; | 131 ResponseMap responses_; |
128 ResponseMap ignore_query_responses_; | 132 ResponseMap ignore_query_responses_; |
129 | 133 |
130 mutable base::Lock hit_count_lock_; | 134 mutable base::Lock hit_count_lock_; |
131 mutable int hit_count_; | 135 mutable int hit_count_; |
132 | 136 |
133 DISALLOW_COPY_AND_ASSIGN(Delegate); | 137 DISALLOW_COPY_AND_ASSIGN(Delegate); |
134 }; | 138 }; |
135 | 139 |
136 | 140 TestURLRequestInterceptor::TestURLRequestInterceptor( |
137 URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor( | |
138 const std::string& scheme, | 141 const std::string& scheme, |
139 const std::string& hostname) | 142 const std::string& hostname, |
143 const scoped_refptr<base::TaskRunner>& io_task_runner, | |
144 const scoped_refptr<base::TaskRunner>& worker_task_runner) | |
140 : scheme_(scheme), | 145 : scheme_(scheme), |
141 hostname_(hostname), | 146 hostname_(hostname), |
142 delegate_(new Delegate(scheme, hostname)) { | 147 io_task_runner_(io_task_runner), |
143 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 148 delegate_( |
144 base::Bind(&Delegate::Register, | 149 new Delegate(scheme, hostname, io_task_runner_, worker_task_runner)) { |
145 base::Unretained(delegate_))); | 150 io_task_runner_->PostTask( |
151 FROM_HERE, base::Bind(&Delegate::Register, base::Unretained(delegate_))); | |
146 } | 152 } |
147 | 153 |
148 URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() { | 154 TestURLRequestInterceptor::~TestURLRequestInterceptor() { |
149 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 155 io_task_runner_->PostTask( |
150 base::Bind(&Delegate::Unregister, | 156 FROM_HERE, base::Bind(&Delegate::Unregister, scheme_, hostname_)); |
151 scheme_, | |
152 hostname_)); | |
153 } | 157 } |
154 | 158 |
155 void URLRequestPrepackagedInterceptor::SetResponse( | 159 void TestURLRequestInterceptor::SetResponse(const GURL& url, |
160 const base::FilePath& path) { | |
161 CHECK_EQ(scheme_, url.scheme()); | |
162 CHECK_EQ(hostname_, url.host()); | |
163 io_task_runner_->PostTask(FROM_HERE, | |
164 base::Bind(&Delegate::SetResponse, | |
165 base::Unretained(delegate_), | |
166 url, | |
167 path, | |
168 false)); | |
169 } | |
170 | |
171 void TestURLRequestInterceptor::SetResponseIgnoreQuery( | |
156 const GURL& url, | 172 const GURL& url, |
157 const base::FilePath& path) { | 173 const base::FilePath& path) { |
158 CHECK_EQ(scheme_, url.scheme()); | 174 CHECK_EQ(scheme_, url.scheme()); |
159 CHECK_EQ(hostname_, url.host()); | 175 CHECK_EQ(hostname_, url.host()); |
160 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 176 io_task_runner_->PostTask(FROM_HERE, |
161 base::Bind(&Delegate::SetResponse, | 177 base::Bind(&Delegate::SetResponse, |
162 base::Unretained(delegate_), url, path, | 178 base::Unretained(delegate_), |
163 false)); | 179 url, |
180 path, | |
181 true)); | |
164 } | 182 } |
165 | 183 |
166 void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery( | 184 int TestURLRequestInterceptor::GetHitCount() { |
167 const GURL& url, | |
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, | |
174 true)); | |
175 } | |
176 | |
177 int URLRequestPrepackagedInterceptor::GetHitCount() { | |
178 return delegate_->GetHitCount(); | 185 return delegate_->GetHitCount(); |
179 } | 186 } |
180 | 187 |
181 | 188 LocalHostTestURLRequestInterceptor::LocalHostTestURLRequestInterceptor( |
182 URLLocalHostRequestPrepackagedInterceptor | 189 const scoped_refptr<base::TaskRunner>& io_task_runner, |
183 ::URLLocalHostRequestPrepackagedInterceptor() | 190 const scoped_refptr<base::TaskRunner>& worker_task_runner) |
184 : URLRequestPrepackagedInterceptor("http", "localhost") { | 191 : TestURLRequestInterceptor("http", |
192 "localhost", | |
193 io_task_runner, | |
194 worker_task_runner) { | |
185 } | 195 } |
186 | 196 |
187 } // namespace content | 197 } // namespace net |
OLD | NEW |