Commit 1b21ab6e authored by Matt Falkenhagen's avatar Matt Falkenhagen Committed by Commit Bot

Dedupe code between FakeNetworkURLLoaderFactory and FakeNetwork.

Bug: 1001435
Change-Id: I46ad1707a235b6d87f811fe89c943bbcbe1664d7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1981416
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#727410}
parent 3ba2070f
......@@ -5,6 +5,7 @@
#include "content/test/fake_network_url_loader_factory.h"
#include "base/strings/string_util.h"
#include "content/public/common/child_process_host.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/http/http_util.h"
#include "services/network/public/mojom/url_loader.mojom.h"
......@@ -12,79 +13,17 @@
namespace content {
namespace {
const char kDefaultHeader[] = "HTTP/1.1 200 OK\n\n";
const char kDefaultBody[] = "this body came from the network";
const char kDefaultHeaderForJS[] =
"HTTP/1.1 200 OK\n"
"Content-Type: application/javascript\n\n";
const char kDefaultBodyForJS[] = "/*this body came from the network*/";
} // namespace
FakeNetworkURLLoaderFactory::ResponseInfo::ResponseInfo(
const std::string& headers,
const std::string& body,
bool network_accessed,
net::Error error_code)
: headers(headers),
body(body),
network_accessed(network_accessed),
error_code(error_code) {}
FakeNetworkURLLoaderFactory::ResponseInfo::ResponseInfo() = default;
FakeNetworkURLLoaderFactory::ResponseInfo::ResponseInfo(
FakeNetworkURLLoaderFactory::ResponseInfo&& info) = default;
FakeNetworkURLLoaderFactory::ResponseInfo::~ResponseInfo() = default;
FakeNetworkURLLoaderFactory::ResponseInfo&
FakeNetworkURLLoaderFactory::ResponseInfo::operator=(
FakeNetworkURLLoaderFactory::ResponseInfo&& info) = default;
FakeNetworkURLLoaderFactory::FakeNetworkURLLoaderFactory() = default;
FakeNetworkURLLoaderFactory::FakeNetworkURLLoaderFactory(
const std::string& headers,
const std::string& body,
bool network_accessed,
net::Error error_code)
: user_defined_default_response_info_(
std::make_unique<FakeNetworkURLLoaderFactory::ResponseInfo>(
headers,
body,
network_accessed,
error_code)) {}
FakeNetworkURLLoaderFactory::~FakeNetworkURLLoaderFactory() = default;
void FakeNetworkURLLoaderFactory::SetResponse(const GURL& url,
const std::string& headers,
const std::string& body,
bool network_accessed,
net::Error error_code) {
response_info_map_[url] =
ResponseInfo(headers, body, network_accessed, error_code);
net::Error error_code) {
fake_network_.SetDefaultResponse(headers, body, network_accessed, error_code);
}
const FakeNetworkURLLoaderFactory::ResponseInfo&
FakeNetworkURLLoaderFactory::FindResponseInfo(const GURL& url) const {
auto it = response_info_map_.find(url);
if (it != response_info_map_.end())
return it->second;
if (user_defined_default_response_info_)
return *user_defined_default_response_info_;
static const base::NoDestructor<ResponseInfo> kDefaultResponseInfo(
kDefaultHeader, kDefaultBody, /*network_accessed=*/true, net::OK);
static const base::NoDestructor<ResponseInfo> kDefaultJsResponseInfo(
kDefaultHeaderForJS, kDefaultBodyForJS, /*network_accessed=*/true,
net::OK);
bool is_js =
base::EndsWith(url.path(), ".js", base::CompareCase::INSENSITIVE_ASCII);
return is_js ? *kDefaultJsResponseInfo : *kDefaultResponseInfo;
}
FakeNetworkURLLoaderFactory::~FakeNetworkURLLoaderFactory() = default;
void FakeNetworkURLLoaderFactory::CreateLoaderAndStart(
mojo::PendingReceiver<network::mojom::URLLoader> receiver,
......@@ -94,31 +33,16 @@ void FakeNetworkURLLoaderFactory::CreateLoaderAndStart(
const network::ResourceRequest& url_request,
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) {
const ResponseInfo& response_info = FindResponseInfo(url_request.url);
mojo::Remote<network::mojom::URLLoaderClient> client_remote(
std::move(client));
net::HttpResponseInfo info;
info.headers = base::MakeRefCounted<net::HttpResponseHeaders>(
net::HttpUtil::AssembleRawHeaders(response_info.headers));
auto response = network::mojom::URLResponseHead::New();
response->headers = info.headers;
response->headers->GetMimeType(&response->mime_type);
response->network_accessed = response_info.network_accessed;
client_remote->OnReceiveResponse(std::move(response));
uint32_t bytes_written = response_info.body.size();
mojo::ScopedDataPipeProducerHandle producer_handle;
mojo::ScopedDataPipeConsumerHandle consumer_handle;
CHECK_EQ(MOJO_RESULT_OK,
mojo::CreateDataPipe(nullptr, &producer_handle, &consumer_handle));
producer_handle->WriteData(response_info.body.data(), &bytes_written,
MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
client_remote->OnStartLoadingResponseBody(std::move(consumer_handle));
network::URLLoaderCompletionStatus status;
status.error_code = response_info.error_code;
client_remote->OnComplete(status);
URLLoaderInterceptor::RequestParams params;
params.process_id = ChildProcessHost::kInvalidUniqueID; // unused
params.routing_id = routing_id;
params.request_id = request_id;
params.options = options;
params.url_request = url_request;
params.client.Bind(std::move(client));
params.traffic_annotation = traffic_annotation;
fake_network_.HandleRequest(&params);
}
void FakeNetworkURLLoaderFactory::Clone(
......
......@@ -5,6 +5,7 @@
#ifndef CONTENT_TEST_FAKE_NETWORK_URL_LOADER_FACTORY_H_
#define CONTENT_TEST_FAKE_NETWORK_URL_LOADER_FACTORY_H_
#include "content/test/fake_network.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
......@@ -12,30 +13,15 @@
namespace content {
// A configurable URLLoaderFactory:
// 1. By default, it returns 200 OK with a simple body to any request:
// If request path ends in ".js", body is:
// "/*this body came from the network*/". Otherwise, body is:
// "this body came from the network".
// 2. The default response can be overridden through the non-default
// constructor.
// 3. Call SetResponse() to set specific response for a url.
//
// TODO(falken): Simplify/refactor this to be based on FakeNetwork as
// they currently share a lot of code. The idea is that tests that want to
// customize network activity should use URLLoaderInterceptor with FakeNetwork
// instead.
// A URLLoaderFactory backed by FakeNetwork, see the documentation there
// for its behavior.
class FakeNetworkURLLoaderFactory final
: public network::mojom::URLLoaderFactory {
public:
// If this constructor is used:
// A default response is used for any url request that is not configured
// through SetResponse().
FakeNetworkURLLoaderFactory();
// If this constructor is used:
// The provided response is used for any url request that is not configured
// through SetResponse().
// If this constructor is used, the provided response is used for any url
// request.
FakeNetworkURLLoaderFactory(const std::string& headers,
const std::string& body,
bool network_accessed,
......@@ -56,46 +42,8 @@ class FakeNetworkURLLoaderFactory final
void Clone(mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver)
override;
// Sets the response for a specific url. CreateLoaderAndStart() uses this
// response instead of the default.
void SetResponse(const GURL& url,
const std::string& headers,
const std::string& body,
bool network_accessed,
net::Error error_code);
private:
class ResponseInfo {
public:
ResponseInfo();
ResponseInfo(ResponseInfo&& info);
ResponseInfo(const std::string& headers,
const std::string& body,
bool network_accessed,
net::Error error_code);
~ResponseInfo();
ResponseInfo& operator=(ResponseInfo&& info);
GURL url;
std::string headers;
std::string body;
bool network_accessed = true;
net::Error error_code = net::OK;
};
// Returns the ResponseInfo for the |url|, it follows the order:
// 1. Returns the matching entry in |response_info_map_| if exists.
// 2. Returns |user_defined_default_response_info_| if it's set.
// 3. Returns default response info (defined inside this method).
const ResponseInfo& FindResponseInfo(const GURL& url) const;
// This is user-defined default response info, it overrides the default
// response info.
std::unique_ptr<ResponseInfo> user_defined_default_response_info_;
// User-defined URL => ResponseInfo map.
base::flat_map<GURL, ResponseInfo> response_info_map_;
FakeNetwork fake_network_;
mojo::ReceiverSet<network::mojom::URLLoaderFactory> receivers_;
DISALLOW_COPY_AND_ASSIGN(FakeNetworkURLLoaderFactory);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment