mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-01-07 07:42:21 +01:00
ceftests: Add test_server support for CefTestServer (see issue #3348)
This commit is contained in:
parent
f24dd61329
commit
a895274add
@ -5,6 +5,7 @@
|
||||
#include <sstream>
|
||||
|
||||
#include "include/base/cef_cxx17_backports.h"
|
||||
#include "include/cef_command_line.h"
|
||||
#include "include/cef_task.h"
|
||||
#include "include/cef_waitable_event.h"
|
||||
#include "include/wrapper/cef_closure_task.h"
|
||||
@ -163,6 +164,15 @@ void SignalIfDone(CefRefPtr<CefWaitableEvent> event,
|
||||
}
|
||||
}
|
||||
|
||||
void Wait(CefRefPtr<CefWaitableEvent> event) {
|
||||
if (CefCommandLine::GetGlobalCommandLine()->HasSwitch(
|
||||
"disable-test-timeout")) {
|
||||
event->Wait();
|
||||
} else {
|
||||
event->TimedWait(2000);
|
||||
}
|
||||
}
|
||||
|
||||
void RunHelperSingle(bool https_server) {
|
||||
CefRefPtr<CefWaitableEvent> event =
|
||||
CefWaitableEvent::CreateWaitableEvent(true, false);
|
||||
@ -171,7 +181,8 @@ void RunHelperSingle(bool https_server) {
|
||||
state.https_server = https_server;
|
||||
CreateObserverOnUIThread(&state, "/TestServerTest.ObserverHelperSingle",
|
||||
base::BindOnce(&CefWaitableEvent::Signal, event));
|
||||
event->TimedWait(2000);
|
||||
|
||||
Wait(event);
|
||||
|
||||
EXPECT_TRUE(state.ExpectAll());
|
||||
}
|
||||
@ -193,7 +204,7 @@ void RunHelperMultiple(bool https_server) {
|
||||
CreateObserverOnUIThread(&states[i], ss.str(), std::move(done_callback));
|
||||
}
|
||||
|
||||
event->TimedWait(2000);
|
||||
Wait(event);
|
||||
|
||||
EXPECT_EQ(size, count);
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
@ -210,3 +221,11 @@ TEST(TestServerObserverTest, HelperSingleHttp) {
|
||||
TEST(TestServerObserverTest, HelperMultipleHttp) {
|
||||
RunHelperMultiple(/*https_server=*/false);
|
||||
}
|
||||
|
||||
TEST(TestServerObserverTest, HelperSingleHttps) {
|
||||
RunHelperSingle(/*https_server=*/true);
|
||||
}
|
||||
|
||||
TEST(TestServerObserverTest, HelperMultipleHttps) {
|
||||
RunHelperMultiple(/*https_server=*/true);
|
||||
}
|
||||
|
@ -4,14 +4,145 @@
|
||||
|
||||
#include "tests/ceftests/test_server_runner.h"
|
||||
|
||||
#include "include/base/cef_logging.h"
|
||||
#include "include/test/cef_test_server.h"
|
||||
#include "include/wrapper/cef_closure_task.h"
|
||||
#include "include/wrapper/cef_helpers.h"
|
||||
#include "tests/gtest/include/gtest/gtest.h"
|
||||
|
||||
namespace test_server {
|
||||
|
||||
namespace {
|
||||
|
||||
// Created on the UI thread and called on the dedicated server thread.
|
||||
class ServerHandler : public CefTestServerHandler {
|
||||
public:
|
||||
ServerHandler(Runner::Delegate* delegate, bool https_server)
|
||||
: delegate_(delegate), https_server_(https_server) {}
|
||||
|
||||
~ServerHandler() override {
|
||||
CEF_REQUIRE_UI_THREAD();
|
||||
|
||||
EXPECT_FALSE(server_);
|
||||
delegate_->OnServerHandlerDeleted();
|
||||
}
|
||||
|
||||
void Start() {
|
||||
CEF_REQUIRE_UI_THREAD();
|
||||
|
||||
// Use any available port number for HTTPS and the legacy port number for
|
||||
// HTTP.
|
||||
server_ = CefTestServer::CreateAndStart(https_server_ ? 0 : kHttpServerPort,
|
||||
https_server_, this);
|
||||
|
||||
// Always execute asynchronously.
|
||||
CefPostTask(TID_UI, base::BindOnce(&ServerHandler::NotifyServerCreated,
|
||||
this, server_->GetOrigin()));
|
||||
}
|
||||
|
||||
void Shutdown() {
|
||||
CEF_REQUIRE_UI_THREAD();
|
||||
|
||||
server_->Stop();
|
||||
server_ = nullptr;
|
||||
|
||||
// Always execute asynchronously.
|
||||
CefPostTask(TID_UI,
|
||||
base::BindOnce(&ServerHandler::NotifyServerDestroyed, this));
|
||||
}
|
||||
|
||||
bool OnTestServerRequest(
|
||||
CefRefPtr<CefTestServer> server,
|
||||
CefRefPtr<CefRequest> request,
|
||||
CefRefPtr<CefTestServerConnection> connection) override {
|
||||
NotifyHttpRequest(request, connection);
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
void NotifyServerCreated(const std::string& server_origin) {
|
||||
CEF_REQUIRE_UI_THREAD();
|
||||
delegate_->OnServerCreated(server_origin);
|
||||
}
|
||||
|
||||
void NotifyServerDestroyed() {
|
||||
CEF_REQUIRE_UI_THREAD();
|
||||
delegate_->OnServerDestroyed();
|
||||
}
|
||||
|
||||
void NotifyHttpRequest(CefRefPtr<CefRequest> request,
|
||||
CefRefPtr<CefTestServerConnection> connection) {
|
||||
if (!CefCurrentlyOn(TID_UI)) {
|
||||
CefPostTask(TID_UI, base::BindOnce(&ServerHandler::NotifyHttpRequest,
|
||||
this, request, connection));
|
||||
return;
|
||||
}
|
||||
|
||||
auto response_callback =
|
||||
base::BindRepeating(&ServerHandler::SendResponse, connection);
|
||||
delegate_->OnHttpRequest(request, response_callback);
|
||||
}
|
||||
|
||||
static void SendResponse(CefRefPtr<CefTestServerConnection> connection,
|
||||
CefRefPtr<CefResponse> response,
|
||||
const std::string& response_data) {
|
||||
const int response_code = response->GetStatus();
|
||||
if (response_code <= 0) {
|
||||
// Intentionally not responding for incomplete request tests.
|
||||
return;
|
||||
}
|
||||
|
||||
// Incomplete response data is not supported.
|
||||
EXPECT_NE(kIncompleteDoNotSendData, response_data.c_str());
|
||||
|
||||
const CefString& content_type = response->GetMimeType();
|
||||
EXPECT_TRUE(!content_type.empty());
|
||||
|
||||
CefResponse::HeaderMap extra_headers;
|
||||
response->GetHeaderMap(extra_headers);
|
||||
|
||||
connection->SendHttpResponse(response_code, content_type,
|
||||
response_data.c_str(), response_data.size(),
|
||||
extra_headers);
|
||||
}
|
||||
|
||||
Runner::Delegate* const delegate_;
|
||||
const bool https_server_;
|
||||
|
||||
CefRefPtr<CefTestServer> server_;
|
||||
|
||||
IMPLEMENT_REFCOUNTING(ServerHandler);
|
||||
DISALLOW_COPY_AND_ASSIGN(ServerHandler);
|
||||
};
|
||||
|
||||
class ServerRunner : public Runner {
|
||||
public:
|
||||
ServerRunner(Delegate* delegate, bool https_server)
|
||||
: Runner(delegate), https_server_(https_server) {}
|
||||
|
||||
void StartServer() override {
|
||||
CEF_REQUIRE_UI_THREAD();
|
||||
DCHECK(!handler_);
|
||||
handler_ = new ServerHandler(delegate_, https_server_);
|
||||
handler_->Start();
|
||||
}
|
||||
|
||||
void ShutdownServer() override {
|
||||
CEF_REQUIRE_UI_THREAD();
|
||||
DCHECK(handler_);
|
||||
handler_->Shutdown();
|
||||
handler_ = nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
const bool https_server_;
|
||||
CefRefPtr<ServerHandler> handler_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<Runner> Runner::CreateTest(Delegate* delegate,
|
||||
bool https_server) {
|
||||
NOTREACHED();
|
||||
return nullptr;
|
||||
return std::make_unique<ServerRunner>(delegate, https_server);
|
||||
}
|
||||
|
||||
} // namespace test_server
|
||||
|
Loading…
Reference in New Issue
Block a user