1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/update_client/url_request_post_interceptor.h"
#include <memory>
#include "base/files/file_util.h"
#include "base/macros.h"
#include "base/strings/stringprintf.h"
#include "components/update_client/test_configurator.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_filter.h"
#include "net/url_request/url_request_interceptor.h"
#include "net/url_request/url_request_simple_job.h"
#include "net/url_request/url_request_test_util.h"
namespace update_client {
// Returns a canned response.
class URLRequestMockJob : public net::URLRequestSimpleJob {
public:
URLRequestMockJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
int response_code,
const std::string& response_body)
: net::URLRequestSimpleJob(request, network_delegate),
response_code_(response_code),
response_body_(response_body) {}
protected:
int GetResponseCode() const override { return response_code_; }
int GetData(std::string* mime_type,
std::string* charset,
std::string* data,
const net::CompletionCallback& callback) const override {
mime_type->assign("text/plain");
charset->assign("US-ASCII");
data->assign(response_body_);
return net::OK;
}
private:
~URLRequestMockJob() override {}
int response_code_;
std::string response_body_;
DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob);
};
URLRequestPostInterceptor::URLRequestPostInterceptor(
const GURL& url,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: url_(url), io_task_runner_(io_task_runner), hit_count_(0) {
}
URLRequestPostInterceptor::~URLRequestPostInterceptor() {
DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
ClearExpectations();
}
void URLRequestPostInterceptor::ClearExpectations() {
while (!expectations_.empty()) {
Expectation expectation(expectations_.front());
delete expectation.first;
expectations_.pop();
}
}
GURL URLRequestPostInterceptor::GetUrl() const {
return url_;
}
bool URLRequestPostInterceptor::ExpectRequest(
class RequestMatcher* request_matcher) {
expectations_.push(std::make_pair(request_matcher,
ExpectationResponse(kResponseCode200, "")));
return true;
}
bool URLRequestPostInterceptor::ExpectRequest(
class RequestMatcher* request_matcher,
int response_code) {
expectations_.push(
std::make_pair(request_matcher, ExpectationResponse(response_code, "")));
return true;
}
bool URLRequestPostInterceptor::ExpectRequest(
class RequestMatcher* request_matcher,
const base::FilePath& filepath) {
std::string response;
if (filepath.empty() || !base::ReadFileToString(filepath, &response))
return false;
expectations_.push(std::make_pair(
request_matcher, ExpectationResponse(kResponseCode200, response)));
return true;
}
int URLRequestPostInterceptor::GetHitCount() const {
base::AutoLock auto_lock(interceptor_lock_);
return hit_count_;
}
int URLRequestPostInterceptor::GetCount() const {
base::AutoLock auto_lock(interceptor_lock_);
return static_cast<int>(requests_.size());
}
std::vector<std::string> URLRequestPostInterceptor::GetRequests() const {
base::AutoLock auto_lock(interceptor_lock_);
return requests_;
}
std::string URLRequestPostInterceptor::GetRequestsAsString() const {
std::vector<std::string> requests(GetRequests());
std::string s = "Requests are:";
int i = 0;
for (std::vector<std::string>::const_iterator it = requests.begin();
it != requests.end(); ++it) {
s.append(base::StringPrintf("\n (%d): %s", ++i, it->c_str()));
}
return s;
}
void URLRequestPostInterceptor::Reset() {
base::AutoLock auto_lock(interceptor_lock_);
hit_count_ = 0;
requests_.clear();
ClearExpectations();
}
class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor {
public:
Delegate(const std::string& scheme,
const std::string& hostname,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: scheme_(scheme), hostname_(hostname), io_task_runner_(io_task_runner) {}
void Register() {
DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
scheme_, hostname_, std::unique_ptr<net::URLRequestInterceptor>(this));
}
void Unregister() {
DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
for (InterceptorMap::iterator it = interceptors_.begin();
it != interceptors_.end(); ++it)
delete (*it).second;
net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme_,
hostname_);
}
void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) {
DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
DCHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end());
interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor));
}
private:
~Delegate() override {}
net::URLRequestJob* MaybeInterceptRequest(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const override {
DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
// Only intercepts POST.
if (!request->has_upload())
return NULL;
GURL url = request->url();
if (url.has_query()) {
GURL::Replacements replacements;
replacements.ClearQuery();
url = url.ReplaceComponents(replacements);
}
InterceptorMap::const_iterator it(interceptors_.find(url));
if (it == interceptors_.end())
return NULL;
// There is an interceptor hooked up for this url. Read the request body,
// check the existing expectations, and handle the matching case by
// popping the expectation off the queue, counting the match, and
// returning a mock object to serve the canned response.
URLRequestPostInterceptor* interceptor(it->second);
const net::UploadDataStream* stream = request->get_upload();
const net::UploadBytesElementReader* reader =
(*stream->GetElementReaders())[0]->AsBytesReader();
const int size = reader->length();
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(size));
const std::string request_body(reader->bytes());
{
base::AutoLock auto_lock(interceptor->interceptor_lock_);
interceptor->requests_.push_back(request_body);
if (interceptor->expectations_.empty())
return NULL;
const URLRequestPostInterceptor::Expectation& expectation(
interceptor->expectations_.front());
if (expectation.first->Match(request_body)) {
const int response_code(expectation.second.response_code);
const std::string response_body(expectation.second.response_body);
delete expectation.first;
interceptor->expectations_.pop();
++interceptor->hit_count_;
return new URLRequestMockJob(request, network_delegate, response_code,
response_body);
}
}
return NULL;
}
typedef std::map<GURL, URLRequestPostInterceptor*> InterceptorMap;
InterceptorMap interceptors_;
const std::string scheme_;
const std::string hostname_;
scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
DISALLOW_COPY_AND_ASSIGN(Delegate);
};
URLRequestPostInterceptorFactory::URLRequestPostInterceptorFactory(
const std::string& scheme,
const std::string& hostname,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: scheme_(scheme),
hostname_(hostname),
io_task_runner_(io_task_runner),
delegate_(new URLRequestPostInterceptor::Delegate(scheme,
hostname,
io_task_runner)) {
io_task_runner_->PostTask(
FROM_HERE, base::Bind(&URLRequestPostInterceptor::Delegate::Register,
base::Unretained(delegate_)));
}
URLRequestPostInterceptorFactory::~URLRequestPostInterceptorFactory() {
io_task_runner_->PostTask(
FROM_HERE, base::Bind(&URLRequestPostInterceptor::Delegate::Unregister,
base::Unretained(delegate_)));
}
URLRequestPostInterceptor* URLRequestPostInterceptorFactory::CreateInterceptor(
const base::FilePath& filepath) {
const GURL base_url(
base::StringPrintf("%s://%s", scheme_.c_str(), hostname_.c_str()));
GURL absolute_url(base_url.Resolve(filepath.MaybeAsASCII()));
URLRequestPostInterceptor* interceptor(
new URLRequestPostInterceptor(absolute_url, io_task_runner_));
bool res = io_task_runner_->PostTask(
FROM_HERE,
base::Bind(&URLRequestPostInterceptor::Delegate::OnCreateInterceptor,
base::Unretained(delegate_), base::Unretained(interceptor)));
if (!res) {
delete interceptor;
return NULL;
}
return interceptor;
}
bool PartialMatch::Match(const std::string& actual) const {
return actual.find(expected_) != std::string::npos;
}
InterceptorFactory::InterceptorFactory(
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: URLRequestPostInterceptorFactory(POST_INTERCEPT_SCHEME,
POST_INTERCEPT_HOSTNAME,
io_task_runner) {
}
InterceptorFactory::~InterceptorFactory() {
}
URLRequestPostInterceptor* InterceptorFactory::CreateInterceptor() {
return CreateInterceptorForPath(POST_INTERCEPT_PATH);
}
URLRequestPostInterceptor* InterceptorFactory::CreateInterceptorForPath(
const char* url_path) {
return URLRequestPostInterceptorFactory::CreateInterceptor(
base::FilePath::FromUTF8Unsafe(url_path));
}
} // namespace update_client
|