summaryrefslogtreecommitdiff
path: root/chromium/net/test/embedded_test_server
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2020-10-12 14:27:29 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2020-10-13 09:35:20 +0000
commitc30a6232df03e1efbd9f3b226777b07e087a1122 (patch)
treee992f45784689f373bcc38d1b79a239ebe17ee23 /chromium/net/test/embedded_test_server
parent7b5b123ac58f58ffde0f4f6e488bcd09aa4decd3 (diff)
downloadqtwebengine-chromium-85-based.tar.gz
BASELINE: Update Chromium to 85.0.4183.14085-based
Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/net/test/embedded_test_server')
-rw-r--r--chromium/net/test/embedded_test_server/embedded_test_server.cc93
-rw-r--r--chromium/net/test/embedded_test_server/embedded_test_server.h83
-rw-r--r--chromium/net/test/embedded_test_server/request_handler_util.cc4
3 files changed, 104 insertions, 76 deletions
diff --git a/chromium/net/test/embedded_test_server/embedded_test_server.cc b/chromium/net/test/embedded_test_server/embedded_test_server.cc
index 36873f21b8a..d143a79ff94 100644
--- a/chromium/net/test/embedded_test_server/embedded_test_server.cc
+++ b/chromium/net/test/embedded_test_server/embedded_test_server.cc
@@ -219,6 +219,28 @@ bool MaybeCreateOCSPResponse(CertBuilder* target,
} // namespace
+EmbeddedTestServerHandle::EmbeddedTestServerHandle(
+ EmbeddedTestServerHandle&& other) {
+ operator=(std::move(other));
+}
+
+EmbeddedTestServerHandle& EmbeddedTestServerHandle::operator=(
+ EmbeddedTestServerHandle&& other) {
+ EmbeddedTestServerHandle temporary;
+ std::swap(other.test_server_, temporary.test_server_);
+ std::swap(temporary.test_server_, test_server_);
+ return *this;
+}
+
+EmbeddedTestServerHandle::EmbeddedTestServerHandle(
+ EmbeddedTestServer* test_server)
+ : test_server_(test_server) {}
+
+EmbeddedTestServerHandle::~EmbeddedTestServerHandle() {
+ if (test_server_)
+ CHECK(test_server_->ShutdownAndWaitUntilComplete());
+}
+
EmbeddedTestServer::OCSPConfig::OCSPConfig() = default;
EmbeddedTestServer::OCSPConfig::OCSPConfig(ResponseType response_type)
: response_type(response_type) {}
@@ -268,9 +290,8 @@ EmbeddedTestServer::EmbeddedTestServer(Type type)
EmbeddedTestServer::~EmbeddedTestServer() {
DCHECK(thread_checker_.CalledOnValidThread());
- if (Started() && !ShutdownAndWaitUntilComplete()) {
- LOG(ERROR) << "EmbeddedTestServer failed to shut down.";
- }
+ if (Started())
+ CHECK(ShutdownAndWaitUntilComplete());
{
base::ScopedAllowBaseSyncPrimitivesForTesting allow_wait_for_thread_join;
@@ -288,23 +309,21 @@ void EmbeddedTestServer::RegisterTestCerts() {
void EmbeddedTestServer::SetConnectionListener(
EmbeddedTestServerConnectionListener* listener) {
- DCHECK(!io_thread_.get())
+ DCHECK(!io_thread_)
<< "ConnectionListener must be set before starting the server.";
connection_listener_ = listener;
}
EmbeddedTestServerHandle EmbeddedTestServer::StartAndReturnHandle(int port) {
- if (!Start(port))
- return EmbeddedTestServerHandle();
- return EmbeddedTestServerHandle(this);
+ bool result = Start(port);
+ return result ? EmbeddedTestServerHandle(this) : EmbeddedTestServerHandle();
}
bool EmbeddedTestServer::Start(int port) {
bool success = InitializeAndListen(port);
- if (!success)
- return false;
- StartAcceptingConnections();
- return true;
+ if (success)
+ StartAcceptingConnections();
+ return success;
}
bool EmbeddedTestServer::InitializeAndListen(int port) {
@@ -543,7 +562,7 @@ bool EmbeddedTestServer::GenerateCertAndKey() {
// StartAcceptingConnections so that this server and the AIA server start at
// the same time. (If the test only called InitializeAndListen they expect no
// threads to be created yet.)
- if (io_thread_.get())
+ if (io_thread_)
aia_http_server_->StartAcceptingConnections();
return true;
@@ -562,10 +581,14 @@ bool EmbeddedTestServer::InitializeSSLServerContext() {
return true;
}
+EmbeddedTestServerHandle
+EmbeddedTestServer::StartAcceptingConnectionsAndReturnHandle() {
+ return EmbeddedTestServerHandle(this);
+}
+
void EmbeddedTestServer::StartAcceptingConnections() {
DCHECK(Started());
- DCHECK(!io_thread_.get())
- << "Server must not be started while server is running";
+ DCHECK(!io_thread_) << "Server must not be started while server is running";
if (aia_http_server_)
aia_http_server_->StartAcceptingConnections();
@@ -584,8 +607,18 @@ void EmbeddedTestServer::StartAcceptingConnections() {
bool EmbeddedTestServer::ShutdownAndWaitUntilComplete() {
DCHECK(thread_checker_.CalledOnValidThread());
- return PostTaskToIOThreadAndWait(base::BindOnce(
- &EmbeddedTestServer::ShutdownOnIOThread, base::Unretained(this)));
+ // Ensure that the AIA HTTP server is no longer Started().
+ bool aia_http_server_not_started = true;
+ if (aia_http_server_ && aia_http_server_->Started()) {
+ aia_http_server_not_started =
+ aia_http_server_->ShutdownAndWaitUntilComplete();
+ }
+
+ // Return false if either this or the AIA HTTP server are still Started().
+ return PostTaskToIOThreadAndWait(
+ base::BindOnce(&EmbeddedTestServer::ShutdownOnIOThread,
+ base::Unretained(this))) &&
+ aia_http_server_not_started;
}
// static
@@ -793,21 +826,21 @@ void EmbeddedTestServer::AddDefaultHandlers(const base::FilePath& directory) {
void EmbeddedTestServer::RegisterRequestHandler(
const HandleRequestCallback& callback) {
- DCHECK(!io_thread_.get())
+ DCHECK(!io_thread_)
<< "Handlers must be registered before starting the server.";
request_handlers_.push_back(callback);
}
void EmbeddedTestServer::RegisterRequestMonitor(
const MonitorRequestCallback& callback) {
- DCHECK(!io_thread_.get())
+ DCHECK(!io_thread_)
<< "Monitors must be registered before starting the server.";
request_monitors_.push_back(callback);
}
void EmbeddedTestServer::RegisterDefaultHandler(
const HandleRequestCallback& callback) {
- DCHECK(!io_thread_.get())
+ DCHECK(!io_thread_)
<< "Handlers must be registered before starting the server.";
default_request_handlers_.push_back(callback);
}
@@ -1008,27 +1041,5 @@ bool EmbeddedTestServer::PostTaskToIOThreadAndWaitWithResult(
return task_result;
}
-EmbeddedTestServerHandle::EmbeddedTestServerHandle(
- EmbeddedTestServerHandle&& other) {
- operator=(std::move(other));
-}
-
-EmbeddedTestServerHandle& EmbeddedTestServerHandle::operator=(
- EmbeddedTestServerHandle&& other) {
- EmbeddedTestServerHandle temporary;
- std::swap(other.test_server_, temporary.test_server_);
- std::swap(temporary.test_server_, test_server_);
- return *this;
-}
-
-EmbeddedTestServerHandle::EmbeddedTestServerHandle(
- EmbeddedTestServer* test_server)
- : test_server_(test_server) {}
-
-EmbeddedTestServerHandle::~EmbeddedTestServerHandle() {
- if (test_server_)
- EXPECT_TRUE(test_server_->ShutdownAndWaitUntilComplete());
-}
-
} // namespace test_server
} // namespace net
diff --git a/chromium/net/test/embedded_test_server/embedded_test_server.h b/chromium/net/test/embedded_test_server/embedded_test_server.h
index 699e64f6a4c..b296db0223f 100644
--- a/chromium/net/test/embedded_test_server/embedded_test_server.h
+++ b/chromium/net/test/embedded_test_server/embedded_test_server.h
@@ -39,11 +39,33 @@ class TCPServerSocket;
namespace test_server {
class EmbeddedTestServerConnectionListener;
-class EmbeddedTestServerHandle;
class HttpConnection;
class HttpResponse;
struct HttpRequest;
+class EmbeddedTestServer;
+
+// Returned by the Start[AcceptingConnections]WithHandle() APIs, to simplify
+// correct shutdown ordering of the EmbeddedTestServer. Shutdown() is invoked
+// on the associated test server when the handle goes out of scope. The handle
+// must therefore be destroyed before the test server.
+class EmbeddedTestServerHandle {
+ public:
+ EmbeddedTestServerHandle() = default;
+ EmbeddedTestServerHandle(EmbeddedTestServerHandle&& other);
+ EmbeddedTestServerHandle& operator=(EmbeddedTestServerHandle&& other);
+ ~EmbeddedTestServerHandle();
+
+ bool is_valid() const { return test_server_; }
+ explicit operator bool() const { return test_server_; }
+
+ private:
+ friend class EmbeddedTestServer;
+
+ explicit EmbeddedTestServerHandle(EmbeddedTestServer* test_server);
+ EmbeddedTestServer* test_server_ = nullptr;
+};
+
// Class providing an HTTP server for testing purpose. This is a basic server
// providing only an essential subset of HTTP/1.1 protocol. Especially,
// it assumes that the request syntax is correct. It *does not* support
@@ -61,7 +83,7 @@ struct HttpRequest;
// std::unique_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
// GURL absolute_url = test_server_->GetURL(request.relative_url);
// if (absolute_url.path() != "/test")
-// return std::unique_ptr<HttpResponse>();
+// return nullptr;
//
// auto http_response = std::make_unique<BasicHttpResponse>();
// http_response->set_code(net::HTTP_OK);
@@ -274,7 +296,8 @@ class EmbeddedTestServer {
typedef base::RepeatingCallback<void(const HttpRequest& request)>
MonitorRequestCallback;
- // Creates a http test server. Start() must be called to start the server.
+ // Creates a http test server. StartAndReturnHandle() must be called to start
+ // the server.
// |type| indicates the protocol type of the server (HTTP/HTTPS).
//
// When a TYPE_HTTPS server is created, EmbeddedTestServer will call
@@ -299,13 +322,14 @@ class EmbeddedTestServer {
// Initializes and waits until the server is ready to accept requests.
// This is the equivalent of calling InitializeAndListen() followed by
- // StartAcceptingConnections().
+ // StartAcceptingConnectionsAndReturnHandle().
// Returns a "handle" which will ShutdownAndWaitUntilComplete() when
// destroyed, or null if the listening socket could not be created.
EmbeddedTestServerHandle StartAndReturnHandle(int port = 0)
WARN_UNUSED_RESULT;
- // Deprecated equivalent of StartAndReturnHandle().
+ // Equivalent of StartAndReturnHandle(), but requires manual Shutdown() by
+ // the caller.
bool Start(int port = 0) WARN_UNUSED_RESULT;
// Starts listening for incoming connections but will not yet accept them.
@@ -313,9 +337,16 @@ class EmbeddedTestServer {
bool InitializeAndListen(int port = 0) WARN_UNUSED_RESULT;
// Starts the Accept IO Thread and begins accepting connections.
+ EmbeddedTestServerHandle StartAcceptingConnectionsAndReturnHandle()
+ WARN_UNUSED_RESULT;
+
+ // Equivalent of StartAcceptingConnectionsAndReturnHandle(), but requires
+ // manual Shutdown() by the caller.
void StartAcceptingConnections();
// Shuts down the http server and waits until the shutdown is complete.
+ // Prefer to use the Start*AndReturnHandle() APIs to manage shutdown, if
+ // possible.
bool ShutdownAndWaitUntilComplete() WARN_UNUSED_RESULT;
// Checks if the server has started listening for incoming connections.
@@ -386,23 +417,22 @@ class EmbeddedTestServer {
// |directory| directory, relative to DIR_SOURCE_ROOT.
void AddDefaultHandlers(const base::FilePath& directory);
- // The most general purpose method. Any request processing can be added using
- // this method. Takes ownership of the object. The |callback| is called
- // on the server's IO thread so all handlers must be registered before the
- // server is started.
+ // Adds a request handler that can perform any general-purpose processing.
+ // |callback| will be invoked on the server's IO thread. Note that:
+ // 1. All handlers must be registered before the server is Start()ed.
+ // 2. The server should be Shutdown() before any variables referred to by
+ // |callback| (e.g. via base::Unretained(&local)) are deleted. Using the
+ // Start*WithHandle() API variants is recommended for this reason.
void RegisterRequestHandler(const HandleRequestCallback& callback);
- // Adds request monitors. The |callback| is called before any handlers are
- // called, but can not respond it. This is useful to monitor requests that
- // will be handled by other request handlers. The |callback| is called
- // on the server's IO thread so all monitors must be registered before the
- // server is started.
+ // Adds a request monitor that will be called before any handlers. Monitors
+ // can be used to observe requests, but not to respond to them.
+ // See RegisterRequestHandler() for notes on usage.
void RegisterRequestMonitor(const MonitorRequestCallback& callback);
- // Adds default handlers, including those added by AddDefaultHandlers, to be
- // tried after all other user-specified handlers have been tried. The
- // |callback| is called on the server's IO thread so all handlers must be
- // registered before the server is started.
+ // Adds a default request handler, to be called if no user-specified handler
+ // handles the request.
+ // See RegisterRequestHandler() for notes on usage.
void RegisterDefaultHandler(const HandleRequestCallback& callback);
bool FlushAllSocketsAndConnectionsOnUIThread();
@@ -525,23 +555,6 @@ class EmbeddedTestServer {
DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer);
};
-class EmbeddedTestServerHandle {
- public:
- EmbeddedTestServerHandle() = default;
- EmbeddedTestServerHandle(EmbeddedTestServerHandle&& other);
- EmbeddedTestServerHandle& operator=(EmbeddedTestServerHandle&& other);
-
- ~EmbeddedTestServerHandle();
-
- explicit operator bool() const { return test_server_; }
-
- private:
- friend class EmbeddedTestServer;
-
- explicit EmbeddedTestServerHandle(EmbeddedTestServer* test_server);
- EmbeddedTestServer* test_server_ = nullptr;
-};
-
} // namespace test_server
// TODO(svaldez): Refactor EmbeddedTestServer to be in the net namespace.
diff --git a/chromium/net/test/embedded_test_server/request_handler_util.cc b/chromium/net/test/embedded_test_server/request_handler_util.cc
index 91d4275f94b..f08d3c34a0b 100644
--- a/chromium/net/test/embedded_test_server/request_handler_util.cc
+++ b/chromium/net/test/embedded_test_server/request_handler_util.cc
@@ -60,6 +60,10 @@ std::string GetContentType(const base::FilePath& path) {
return "audio/wav";
if (path.MatchesExtension(FILE_PATH_LITERAL(".webp")))
return "image/webp";
+ if (path.MatchesExtension(FILE_PATH_LITERAL(".mp4")))
+ return "video/mp4";
+ if (path.MatchesExtension(FILE_PATH_LITERAL(".webm")))
+ return "video/webm";
if (path.MatchesExtension(FILE_PATH_LITERAL(".xml")))
return "text/xml";
if (path.MatchesExtension(FILE_PATH_LITERAL(".mhtml")))