This source file includes following definitions.
- map_all_requests_to_base_path_
- MaybeCreateJob
- GetOnDiskPath
- AddUrlHandler
- AddHostnameToFileHandler
- GetMockUrl
- GetMockViewSourceUrl
- CreateProtocolHandler
- CreateProtocolHandlerForSingleFile
- GetResponseInfo
- IsRedirectResponse
- GetResponseInfoConst
- GetMimeType
- GetResponseCode
- GetCharset
#include "content/test/net/url_request_mock_http_job.h"
#include "base/file_util.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/thread_restrictions.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/url_constants.h"
#include "net/base/net_util.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request_filter.h"
const char kMockHostname[] = "mock.http";
const base::FilePath::CharType kMockHeaderFileSuffix[] =
FILE_PATH_LITERAL(".mock-http-headers");
namespace content {
namespace {
class ProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
public:
explicit ProtocolHandler(const base::FilePath& base_path,
bool map_all_requests_to_base_path)
: base_path_(base_path),
map_all_requests_to_base_path_(map_all_requests_to_base_path) {}
virtual ~ProtocolHandler() {}
virtual net::URLRequestJob* MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const OVERRIDE {
return new URLRequestMockHTTPJob(request, network_delegate,
map_all_requests_to_base_path_ ? base_path_ : GetOnDiskPath(request));
}
private:
base::FilePath GetOnDiskPath(net::URLRequest* request) const {
GURL file_url(net::FilePathToFileURL(base_path_));
std::string url = file_url.spec() + request->url().path();
base::FilePath file_path;
net::FileURLToFilePath(GURL(url), &file_path);
return file_path;
}
const base::FilePath base_path_;
const bool map_all_requests_to_base_path_;
DISALLOW_COPY_AND_ASSIGN(ProtocolHandler);
};
}
void URLRequestMockHTTPJob::AddUrlHandler(const base::FilePath& base_path) {
net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
filter->AddHostnameProtocolHandler("http", kMockHostname,
CreateProtocolHandler(base_path));
}
void URLRequestMockHTTPJob::AddHostnameToFileHandler(
const std::string& hostname,
const base::FilePath& file) {
net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
filter->AddHostnameProtocolHandler(
"http", hostname, CreateProtocolHandlerForSingleFile(file));
}
GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) {
std::string url = "http://";
url.append(kMockHostname);
url.append("/");
std::string path_str = path.MaybeAsASCII();
DCHECK(!path_str.empty());
url.append(path_str);
return GURL(url);
}
GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const base::FilePath& path) {
std::string url = kViewSourceScheme;
url.append(":");
url.append(GetMockUrl(path).spec());
return GURL(url);
}
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
URLRequestMockHTTPJob::CreateProtocolHandler(const base::FilePath& base_path) {
return scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
new ProtocolHandler(base_path, false));
}
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
URLRequestMockHTTPJob::CreateProtocolHandlerForSingleFile(
const base::FilePath& file) {
return scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
new ProtocolHandler(file, true));
}
URLRequestMockHTTPJob::URLRequestMockHTTPJob(
net::URLRequest* request, net::NetworkDelegate* network_delegate,
const base::FilePath& file_path)
: net::URLRequestFileJob(
request, network_delegate, file_path,
content::BrowserThread::GetBlockingPool()->
GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {}
URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { }
void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) {
GetResponseInfoConst(info);
}
bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location,
int* http_status_code) {
return net::URLRequestJob::IsRedirectResponse(location, http_status_code);
}
void URLRequestMockHTTPJob::GetResponseInfoConst(
net::HttpResponseInfo* info) const {
base::ThreadRestrictions::ScopedAllowIO allow_io;
base::FilePath header_file =
base::FilePath(file_path_.value() + kMockHeaderFileSuffix);
std::string raw_headers;
if (!base::PathExists(header_file)) {
raw_headers = "HTTP/1.0 200 OK\n";
} else {
if (!base::ReadFileToString(header_file, &raw_headers))
return;
}
ReplaceSubstringsAfterOffset(&raw_headers, 0, "\r\n", "\n");
ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1));
info->headers = new net::HttpResponseHeaders(raw_headers);
}
bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const {
net::HttpResponseInfo info;
GetResponseInfoConst(&info);
return info.headers.get() && info.headers->GetMimeType(mime_type);
}
int URLRequestMockHTTPJob::GetResponseCode() const {
net::HttpResponseInfo info;
GetResponseInfoConst(&info);
if (info.headers.get())
return info.headers->response_code();
return net::URLRequestJob::GetResponseCode();
}
bool URLRequestMockHTTPJob::GetCharset(std::string* charset) {
net::HttpResponseInfo info;
GetResponseInfo(&info);
return info.headers.get() && info.headers->GetCharset(charset);
}
}