OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/component_updater/test/url_request_post_interceptor.h" | 5 #include "chrome/browser/component_updater/test/url_request_post_interceptor.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "content/public/test/test_browser_thread.h" | 9 #include "content/public/test/test_browser_thread.h" |
10 #include "net/base/upload_bytes_element_reader.h" | 10 #include "net/base/upload_bytes_element_reader.h" |
11 #include "net/url_request/url_request.h" | 11 #include "net/url_request/url_request.h" |
12 #include "net/url_request/url_request_filter.h" | 12 #include "net/url_request/url_request_filter.h" |
13 #include "net/url_request/url_request_job_factory.h" | 13 #include "net/url_request/url_request_interceptor.h" |
14 #include "net/url_request/url_request_simple_job.h" | 14 #include "net/url_request/url_request_simple_job.h" |
15 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
16 | 16 |
17 using content::BrowserThread; | 17 using content::BrowserThread; |
18 | 18 |
19 namespace component_updater { | 19 namespace component_updater { |
20 | 20 |
21 // Returns a canned response. | 21 // Returns a canned response. |
22 class URLRequestMockJob : public net::URLRequestSimpleJob { | 22 class URLRequestMockJob : public net::URLRequestSimpleJob { |
23 public: | 23 public: |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 return s; | 114 return s; |
115 } | 115 } |
116 | 116 |
117 void URLRequestPostInterceptor::Reset() { | 117 void URLRequestPostInterceptor::Reset() { |
118 base::AutoLock auto_lock(interceptor_lock_); | 118 base::AutoLock auto_lock(interceptor_lock_); |
119 hit_count_ = 0; | 119 hit_count_ = 0; |
120 requests_.clear(); | 120 requests_.clear(); |
121 ClearExpectations(); | 121 ClearExpectations(); |
122 } | 122 } |
123 | 123 |
124 class URLRequestPostInterceptor::Delegate | 124 class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor { |
125 : public net::URLRequestJobFactory::ProtocolHandler { | |
126 public: | 125 public: |
127 Delegate(const std::string& scheme, const std::string& hostname) | 126 Delegate(const std::string& scheme, const std::string& hostname) |
128 : scheme_(scheme), hostname_(hostname) {} | 127 : scheme_(scheme), hostname_(hostname) {} |
129 | 128 |
130 void Register() { | 129 void Register() { |
131 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 130 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
132 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( | 131 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
133 scheme_, | 132 scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this)); |
134 hostname_, | |
135 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(this)); | |
136 } | 133 } |
137 | 134 |
138 void Unregister() { | 135 void Unregister() { |
139 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 136 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
140 for (InterceptorMap::iterator it = interceptors_.begin(); | 137 for (InterceptorMap::iterator it = interceptors_.begin(); |
141 it != interceptors_.end(); | 138 it != interceptors_.end(); |
142 ++it) | 139 ++it) |
143 delete (*it).second; | 140 delete (*it).second; |
144 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme_, | 141 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme_, |
145 hostname_); | 142 hostname_); |
146 } | 143 } |
147 | 144 |
148 void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) { | 145 void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) { |
149 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 146 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
150 CHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end()); | 147 CHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end()); |
151 | 148 |
152 interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor)); | 149 interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor)); |
153 } | 150 } |
154 | 151 |
155 private: | 152 private: |
156 virtual ~Delegate() {} | 153 virtual ~Delegate() {} |
157 | 154 |
158 virtual net::URLRequestJob* MaybeCreateJob( | 155 virtual net::URLRequestJob* MaybeInterceptRequest( |
159 net::URLRequest* request, | 156 net::URLRequest* request, |
160 net::NetworkDelegate* network_delegate) const OVERRIDE { | 157 net::NetworkDelegate* network_delegate) const OVERRIDE { |
161 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 158 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
162 | 159 |
163 // Only intercepts POST. | 160 // Only intercepts POST. |
164 if (!request->has_upload()) | 161 if (!request->has_upload()) |
165 return NULL; | 162 return NULL; |
166 | 163 |
167 GURL url = request->url(); | 164 GURL url = request->url(); |
168 if (url.has_query()) { | 165 if (url.has_query()) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 base::Unretained(interceptor))); | 250 base::Unretained(interceptor))); |
254 if (!res) { | 251 if (!res) { |
255 delete interceptor; | 252 delete interceptor; |
256 return NULL; | 253 return NULL; |
257 } | 254 } |
258 | 255 |
259 return interceptor; | 256 return interceptor; |
260 } | 257 } |
261 | 258 |
262 } // namespace component_updater | 259 } // namespace component_updater |
OLD | NEW |