ceftests: Generalize the test_server::Observer interface (see issue #3348)

Don't expose CefServer implementation details to consumers.
This commit is contained in:
Marshall Greenblatt
2022-07-29 16:05:20 -04:00
parent 8353564d92
commit 68be2b8938
5 changed files with 143 additions and 345 deletions

View File

@@ -1325,35 +1325,16 @@ class CookieAccessServerHandler : public test_server::ObserverHelper,
delete this;
}
bool OnHttpRequest(CefRefPtr<CefServer> server,
int connection_id,
const CefString& client_address,
CefRefPtr<CefRequest> request) override {
bool OnHttpRequest(CefRefPtr<CefRequest> request,
const ResponseCallback& response_callback) override {
EXPECT_UI_THREAD();
EXPECT_FALSE(client_address.empty());
// Log the requests for better error reporting.
request_log_ += request->GetMethod().ToString() + " " +
request->GetURL().ToString() + "\n";
HandleRequest(server, connection_id, request);
actual_http_request_ct_++;
return true;
}
private:
void VerifyResults() {
EXPECT_TRUE(got_server_created_);
EXPECT_TRUE(got_server_destroyed_);
EXPECT_EQ(expected_http_request_ct_, actual_http_request_ct_)
<< request_log_;
}
void HandleRequest(CefRefPtr<CefServer> server,
int connection_id,
CefRefPtr<CefRequest> request) {
const std::string& url = request->GetURL();
ResponseDataMap::const_iterator it = data_map_.find(url);
if (it != data_map_.end()) {
@@ -1365,48 +1346,21 @@ class CookieAccessServerHandler : public test_server::ObserverHelper,
TestCookieString(cookie_str, it->second->cookie_js_ct_,
it->second->cookie_net_ct_);
SendResponse(server, connection_id, it->second->response,
it->second->response_data);
} else {
response_callback.Run(it->second->response, it->second->response_data);
return true;
} else if (!IgnoreURL(url)) {
// Unknown test.
if (!IgnoreURL(url)) {
ADD_FAILURE() << "Unexpected url: " << url;
}
server->SendHttp500Response(connection_id, "Unknown test");
ADD_FAILURE() << "Unexpected url: " << url;
}
return false;
}
static void SendResponse(CefRefPtr<CefServer> server,
int connection_id,
CefRefPtr<CefResponse> response,
const std::string& response_data) {
// Execute on the server thread because some methods require it.
CefRefPtr<CefTaskRunner> task_runner = server->GetTaskRunner();
if (!task_runner->BelongsToCurrentThread()) {
task_runner->PostTask(CefCreateClosureTask(
base::BindOnce(CookieAccessServerHandler::SendResponse, server,
connection_id, response, response_data)));
return;
}
int response_code = response->GetStatus();
const CefString& content_type = response->GetMimeType();
int64 content_length = static_cast<int64>(response_data.size());
CefResponse::HeaderMap extra_headers;
response->GetHeaderMap(extra_headers);
server->SendHttpResponse(connection_id, response_code, content_type,
content_length, extra_headers);
if (content_length != 0) {
server->SendRawData(connection_id, response_data.data(),
response_data.size());
server->CloseConnection(connection_id);
}
// The connection should be closed.
EXPECT_FALSE(server->IsValidConnection(connection_id));
private:
void VerifyResults() {
EXPECT_TRUE(got_server_created_);
EXPECT_TRUE(got_server_destroyed_);
EXPECT_EQ(expected_http_request_ct_, actual_http_request_ct_)
<< request_log_;
}
void RunCompleteCallback() {

View File

@@ -311,10 +311,8 @@ class TestServerObserver : public test_server::ObserverHelper {
std::move(ready_callback_).Run();
}
bool OnHttpRequest(CefRefPtr<CefServer> server,
int connection_id,
const CefString& client_address,
CefRefPtr<CefRequest> request) override {
bool OnHttpRequest(CefRefPtr<CefRequest> request,
const ResponseCallback& response_callback) override {
CEF_REQUIRE_UI_THREAD();
Resource* resource = setup_->GetResource(request);
if (!resource) {
@@ -325,8 +323,7 @@ class TestServerObserver : public test_server::ObserverHelper {
resource->response_ct++;
EXPECT_TRUE(resource->VerifyRequest(request))
<< request->GetURL().ToString();
test_server::SendResponse(server, connection_id, resource->response,
resource->response_data);
response_callback.Run(resource->response, resource->response_data);
// Stop propagating the callback.
return true;

View File

@@ -6,8 +6,10 @@
#include <vector>
#include "include/cef_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 {
@@ -16,6 +18,7 @@ const char kServerAddress[] = "127.0.0.1";
const uint16 kServerPort = 8098;
const char kServerScheme[] = "http";
const char kServerOrigin[] = "http://127.0.0.1:8098";
const char kIncompleteDoNotSendData[] = "DO NOT SEND";
namespace {
@@ -33,7 +36,7 @@ class ServerHandler : public CefServerHandler {
}
~ServerHandler() override {
DCHECK(!server_);
EXPECT_FALSE(server_);
NotifyServerHandlerDeleted();
}
@@ -53,15 +56,13 @@ class ServerHandler : public CefServerHandler {
void OnClientConnected(CefRefPtr<CefServer> server,
int connection_id) override {
DCHECK(server->HasConnection());
DCHECK(server->IsValidConnection(connection_id));
NotifyClientConnected(server, connection_id);
EXPECT_TRUE(server->HasConnection());
EXPECT_TRUE(server->IsValidConnection(connection_id));
}
void OnClientDisconnected(CefRefPtr<CefServer> server,
int connection_id) override {
DCHECK(!server->IsValidConnection(connection_id));
NotifyClientDisconnected(server, connection_id);
EXPECT_FALSE(server->IsValidConnection(connection_id));
}
void OnHttpRequest(CefRefPtr<CefServer> server,
@@ -87,10 +88,6 @@ class ServerHandler : public CefServerHandler {
static void NotifyServerCreated(const std::string& server_origin);
static void NotifyServerDestroyed();
static void NotifyServerHandlerDeleted();
static void NotifyClientConnected(CefRefPtr<CefServer> server,
int connection_id);
static void NotifyClientDisconnected(CefRefPtr<CefServer> server,
int connection_id);
static void NotifyHttpRequest(CefRefPtr<CefServer> server,
int connection_id,
const CefString& client_address,
@@ -107,15 +104,15 @@ class ServerManager {
public:
ServerManager() {
CEF_REQUIRE_UI_THREAD();
DCHECK(!g_manager);
EXPECT_FALSE(g_manager);
g_manager = this;
}
~ServerManager() {
CEF_REQUIRE_UI_THREAD();
DCHECK(observer_list_.empty());
DCHECK(start_callback_list_.empty());
DCHECK(stop_callback_.is_null());
EXPECT_TRUE(observer_list_.empty());
EXPECT_TRUE(start_callback_list_.empty());
EXPECT_TRUE(stop_callback_.is_null());
g_manager = nullptr;
}
@@ -147,7 +144,7 @@ class ServerManager {
}
// Only 1 stop callback supported.
DCHECK(stop_callback_.is_null());
EXPECT_TRUE(stop_callback_.is_null());
stop_callback_ = std::move(callback);
handler_->Shutdown();
@@ -169,13 +166,13 @@ class ServerManager {
break;
}
}
DCHECK(found);
EXPECT_TRUE(found);
}
void NotifyServerCreated(const std::string& server_origin) {
CEF_REQUIRE_UI_THREAD();
DCHECK(origin_.empty());
EXPECT_TRUE(origin_.empty());
origin_ = server_origin;
for (auto& callback : start_callback_list_) {
@@ -195,47 +192,12 @@ class ServerManager {
void NotifyServerHandlerDeleted() {
CEF_REQUIRE_UI_THREAD();
DCHECK(!stop_callback_.is_null());
EXPECT_FALSE(stop_callback_.is_null());
std::move(stop_callback_).Run();
delete this;
}
void NotifyClientConnected(CefRefPtr<CefServer> server, int connection_id) {
CEF_REQUIRE_UI_THREAD();
if (observer_list_.empty())
return;
// Use a copy in case |observer_list_| is modified during iteration.
ObserverList list = observer_list_;
ObserverList::const_iterator it = list.begin();
for (; it != list.end(); ++it) {
if ((*it)->OnClientConnected(server, connection_id)) {
break;
}
}
}
void NotifyClientDisconnected(CefRefPtr<CefServer> server,
int connection_id) {
CEF_REQUIRE_UI_THREAD();
if (observer_list_.empty())
return;
// Use a copy in case |observer_list_| is modified during iteration.
ObserverList list = observer_list_;
ObserverList::const_iterator it = list.begin();
for (; it != list.end(); ++it) {
if ((*it)->OnClientDisconnected(server, connection_id)) {
break;
}
}
}
void NotifyHttpRequest(CefRefPtr<CefServer> server,
int connection_id,
const CefString& client_address,
@@ -252,17 +214,19 @@ class ServerManager {
return;
}
DCHECK(!observer_list_.empty()) << url;
EXPECT_FALSE(observer_list_.empty()) << url;
// Use a copy in case |observer_list_| is modified during iteration.
ObserverList list = observer_list_;
bool handled = false;
auto response_callback = base::BindRepeating(&ServerManager::SendResponse,
server, connection_id);
ObserverList::const_iterator it = list.begin();
for (; it != list.end(); ++it) {
if ((*it)->OnHttpRequest(server, connection_id, client_address,
request)) {
if ((*it)->OnHttpRequest(request, response_callback)) {
handled = true;
break;
}
@@ -274,6 +238,52 @@ class ServerManager {
}
private:
static void SendResponse(CefRefPtr<CefServer> server,
int connection_id,
CefRefPtr<CefResponse> response,
const std::string& response_data) {
// Execute on the server thread because some methods require it.
CefRefPtr<CefTaskRunner> task_runner = server->GetTaskRunner();
if (!task_runner->BelongsToCurrentThread()) {
task_runner->PostTask(CefCreateClosureTask(
base::BindOnce(ServerManager::SendResponse, server, connection_id,
response, response_data)));
return;
}
// No response should be sent yet.
EXPECT_TRUE(server->IsValidConnection(connection_id));
const int response_code = response->GetStatus();
if (response_code <= 0) {
// Intentionally not responding for incomplete request tests.
return;
}
const CefString& content_type = response->GetMimeType();
int64 content_length = static_cast<int64>(response_data.size());
CefResponse::HeaderMap extra_headers;
response->GetHeaderMap(extra_headers);
server->SendHttpResponse(connection_id, response_code, content_type,
content_length, extra_headers);
if (response_data == kIncompleteDoNotSendData) {
// Intentionally not sending data for incomplete request tests.
return;
}
if (content_length != 0) {
server->SendRawData(connection_id, response_data.data(),
response_data.size());
server->CloseConnection(connection_id);
}
// The connection should be closed.
EXPECT_FALSE(server->IsValidConnection(connection_id));
}
CefRefPtr<ServerHandler> handler_;
std::string origin_;
@@ -295,7 +305,7 @@ ServerManager* GetServerManager() {
ServerManager* GetOrCreateServerManager() {
if (!g_manager) {
new ServerManager();
DCHECK(g_manager);
EXPECT_TRUE(g_manager);
}
return g_manager;
}
@@ -332,30 +342,6 @@ void ServerHandler::NotifyServerHandlerDeleted() {
GetServerManager()->NotifyServerHandlerDeleted();
}
// static
void ServerHandler::NotifyClientConnected(CefRefPtr<CefServer> server,
int connection_id) {
if (!CefCurrentlyOn(TID_UI)) {
CefPostTask(TID_UI, base::BindOnce(ServerHandler::NotifyClientConnected,
server, connection_id));
return;
}
GetServerManager()->NotifyClientConnected(server, connection_id);
}
// static
void ServerHandler::NotifyClientDisconnected(CefRefPtr<CefServer> server,
int connection_id) {
if (!CefCurrentlyOn(TID_UI)) {
CefPostTask(TID_UI, base::BindOnce(ServerHandler::NotifyClientDisconnected,
server, connection_id));
return;
}
GetServerManager()->NotifyClientDisconnected(server, connection_id);
}
// static
void ServerHandler::NotifyHttpRequest(CefRefPtr<CefServer> server,
int connection_id,
@@ -376,7 +362,7 @@ class ObserverRegistration : public CefRegistration {
public:
explicit ObserverRegistration(Observer* const observer)
: observer_(observer) {
DCHECK(observer_);
EXPECT_TRUE(observer_);
}
~ObserverRegistration() override {
@@ -410,7 +396,7 @@ void InitializeRegistration(CefRefPtr<ObserverRegistration> registration,
return;
}
DCHECK(!g_stopping);
EXPECT_FALSE(g_stopping);
registration->Initialize();
if (!callback.is_null())
@@ -420,26 +406,26 @@ void InitializeRegistration(CefRefPtr<ObserverRegistration> registration,
} // namespace
void Start(StartDoneCallback callback) {
DCHECK(!callback.is_null());
EXPECT_FALSE(callback.is_null());
if (!CefCurrentlyOn(TID_UI)) {
CefPostTask(TID_UI, base::BindOnce(Start, std::move(callback)));
return;
}
DCHECK(!g_stopping);
EXPECT_FALSE(g_stopping);
GetOrCreateServerManager()->Start(std::move(callback));
}
void Stop(DoneCallback callback) {
DCHECK(!callback.is_null());
EXPECT_FALSE(callback.is_null());
if (!CefCurrentlyOn(TID_UI)) {
CefPostTask(TID_UI, base::BindOnce(Stop, std::move(callback)));
return;
}
// Stop will be called one time on test framework shutdown.
DCHECK(!g_stopping);
EXPECT_FALSE(g_stopping);
g_stopping = true;
ServerManager* manager = GetServerManager();
@@ -452,7 +438,7 @@ void Stop(DoneCallback callback) {
CefRefPtr<CefRegistration> AddObserver(Observer* observer,
DoneCallback callback) {
DCHECK(observer);
EXPECT_TRUE(observer);
CefRefPtr<ObserverRegistration> registration =
new ObserverRegistration(observer);
InitializeRegistration(registration, std::move(callback));
@@ -464,27 +450,6 @@ CefRefPtr<CefRegistration> AddObserverAndStart(Observer* observer,
return AddObserver(observer, base::BindOnce(Start, std::move(callback)));
}
void SendResponse(CefRefPtr<CefServer> server,
int connection_id,
CefRefPtr<CefResponse> response,
const std::string& response_data) {
const int response_code = response->GetStatus();
const CefString& content_type = response->GetMimeType();
int64 content_length = static_cast<int64>(response_data.size());
CefResponse::HeaderMap extra_headers;
response->GetHeaderMap(extra_headers);
server->SendHttpResponse(connection_id, response_code, content_type,
content_length, extra_headers);
if (content_length != 0) {
server->SendRawData(connection_id, response_data.data(),
response_data.size());
server->CloseConnection(connection_id);
}
}
// ObserverHelper
ObserverHelper::ObserverHelper() : weak_ptr_factory_(this) {
@@ -492,12 +457,12 @@ ObserverHelper::ObserverHelper() : weak_ptr_factory_(this) {
}
ObserverHelper::~ObserverHelper() {
DCHECK(state_ == State::NONE);
EXPECT_TRUE(state_ == State::NONE);
}
void ObserverHelper::Initialize() {
CEF_REQUIRE_UI_THREAD();
DCHECK(state_ == State::NONE);
EXPECT_TRUE(state_ == State::NONE);
state_ = State::INITIALIZING;
registration_ =
AddObserverAndStart(this, base::BindOnce(&ObserverHelper::OnStartDone,
@@ -506,23 +471,23 @@ void ObserverHelper::Initialize() {
void ObserverHelper::Shutdown() {
CEF_REQUIRE_UI_THREAD();
DCHECK(state_ == State::INITIALIZED);
EXPECT_TRUE(state_ == State::INITIALIZED);
state_ = State::SHUTTINGDOWN;
registration_ = nullptr;
}
void ObserverHelper::OnStartDone(const std::string& server_origin) {
DCHECK(state_ == State::INITIALIZING);
EXPECT_TRUE(state_ == State::INITIALIZING);
state_ = State::INITIALIZED;
OnInitialized(server_origin);
}
void ObserverHelper::OnRegistered() {
DCHECK(state_ == State::INITIALIZING);
EXPECT_TRUE(state_ == State::INITIALIZING);
}
void ObserverHelper::OnUnregistered() {
DCHECK(state_ == State::SHUTTINGDOWN);
EXPECT_TRUE(state_ == State::SHUTTINGDOWN);
state_ = State::NONE;
OnShutdown();
}

View File

@@ -12,7 +12,6 @@
#include "include/cef_registration.h"
#include "include/cef_request.h"
#include "include/cef_response.h"
#include "include/cef_server.h"
namespace test_server {
@@ -21,6 +20,9 @@ extern const uint16 kServerPort;
extern const char kServerScheme[];
extern const char kServerOrigin[];
// Used with incomplete tests for data that should not be sent.
extern const char kIncompleteDoNotSendData[];
using DoneCallback = base::OnceClosure;
using StartDoneCallback =
@@ -35,8 +37,7 @@ void Start(StartDoneCallback callback);
// UI thread. This method will be called by the test framework on shutdown.
void Stop(DoneCallback callback);
// Observer for CefServerHandler callbacks. Methods will be called on the UI
// thread.
// Observer for server callbacks. Methods will be called on the UI thread.
class Observer {
public:
// Called when this Observer is registered.
@@ -45,30 +46,25 @@ class Observer {
// Called when this Observer is unregistered.
virtual void OnUnregistered() = 0;
// See CefServerHandler documentation for usage. Return true if the callback
// was handled.
virtual bool OnClientConnected(CefRefPtr<CefServer> server,
int connection_id) {
return false;
}
virtual bool OnClientDisconnected(CefRefPtr<CefServer> server,
int connection_id) {
return false;
}
virtual bool OnHttpRequest(CefRefPtr<CefServer> server,
int connection_id,
const CefString& client_address,
CefRefPtr<CefRequest> request) = 0;
using ResponseCallback =
base::RepeatingCallback<void(CefRefPtr<CefResponse> response,
const std::string& response_data)>;
// Return true and execute |response_callback| either synchronously or
// asynchronously if the request was handled. Do not execute
// |response_callback| when returning false.
virtual bool OnHttpRequest(CefRefPtr<CefRequest> request,
const ResponseCallback& response_callback) = 0;
protected:
virtual ~Observer() {}
};
// Add an observer for CefServerHandler callbacks. Remains registered until the
// returned CefRegistration object is destroyed. Registered observers will be
// executed in the order of registration until one returns true to indicate that
// it handled the callback. |callback| will be executed on the UI thread after
// registration is complete.
// Add an observer for server callbacks. Remains registered until the returned
// CefRegistration object is destroyed. Registered observers will be executed in
// the order of registration until one returns true to indicate that it handled
// the callback. |callback| will be executed on the UI thread after registration
// is complete.
CefRefPtr<CefRegistration> AddObserver(Observer* observer,
DoneCallback callback);
@@ -76,15 +72,9 @@ CefRefPtr<CefRegistration> AddObserver(Observer* observer,
CefRefPtr<CefRegistration> AddObserverAndStart(Observer* observer,
StartDoneCallback callback);
// Helper for sending a fully qualified response.
void SendResponse(CefRefPtr<CefServer> server,
int connection_id,
CefRefPtr<CefResponse> response,
const std::string& response_data);
// Helper for managing Observer registration and callbacks. Only used on the UI
// thread.
class ObserverHelper : Observer {
class ObserverHelper : public Observer {
public:
ObserverHelper();
~ObserverHelper() override;

View File

@@ -79,9 +79,6 @@ const char kRequestSaveCookieName[] = "urcookie_save";
const char kCacheControlHeader[] = "cache-control";
// Used with incomplete tests for data that should not be sent.
const char kIncompleteDoNotSendData[] = "DO NOT SEND";
enum RequestTestMode {
REQTEST_GET = 0,
REQTEST_GET_NODATA,
@@ -1238,8 +1235,6 @@ class RequestServerHandler : public test_server::ObserverHelper {
public:
RequestServerHandler()
: initialized_(false),
expected_connection_ct_(-1),
actual_connection_ct_(0),
expected_http_request_ct_(-1),
actual_http_request_ct_(0) {}
@@ -1254,7 +1249,7 @@ class RequestServerHandler : public test_server::ObserverHelper {
// Must be called before CreateServer().
void SetExpectedRequestCount(int count) {
EXPECT_FALSE(initialized_);
expected_connection_ct_ = expected_http_request_ct_ = count;
expected_http_request_ct_ = count;
}
// |complete_callback| will be executed on the UI thread after the server is
@@ -1262,7 +1257,7 @@ class RequestServerHandler : public test_server::ObserverHelper {
void CreateServer(base::OnceClosure complete_callback) {
EXPECT_UI_THREAD();
if (expected_connection_ct_ < 0) {
if (expected_http_request_ct_ < 0) {
// Default to the assumption of one request per registered URL.
SetExpectedRequestCount(static_cast<int>(data_map_.size()));
}
@@ -1308,116 +1303,60 @@ class RequestServerHandler : public test_server::ObserverHelper {
delete this;
}
bool OnClientConnected(CefRefPtr<CefServer> server,
int connection_id) override {
bool OnHttpRequest(CefRefPtr<CefRequest> request,
const ResponseCallback& response_callback) override {
EXPECT_UI_THREAD();
if (!IsChromeRuntimeEnabled()) {
EXPECT_TRUE(connection_id_set_.find(connection_id) ==
connection_id_set_.end());
connection_id_set_.insert(connection_id);
}
actual_connection_ct_++;
return true;
}
bool OnClientDisconnected(CefRefPtr<CefServer> server,
int connection_id) override {
EXPECT_UI_THREAD();
if (!IsChromeRuntimeEnabled()) {
ConnectionIdSet::iterator it = connection_id_set_.find(connection_id);
EXPECT_TRUE(it != connection_id_set_.end());
connection_id_set_.erase(it);
}
return true;
}
bool OnHttpRequest(CefRefPtr<CefServer> server,
int connection_id,
const CefString& client_address,
CefRefPtr<CefRequest> request) override {
EXPECT_UI_THREAD();
if (!IsChromeRuntimeEnabled()) {
EXPECT_TRUE(VerifyConnection(connection_id));
}
EXPECT_FALSE(client_address.empty());
// Log the requests for better error reporting.
request_log_ += request->GetMethod().ToString() + " " +
request->GetURL().ToString() + "\n";
// TODO(chrome-runtime): Debug why favicon requests don't always have the
// correct resource type.
const std::string& url = request->GetURL();
if (request->GetResourceType() == RT_FAVICON ||
url.find("/favicon.ico") != std::string::npos) {
// We don't currently handle favicon requests.
server->SendHttp404Response(connection_id);
return true;
}
HandleRequest(server, connection_id, request);
actual_http_request_ct_++;
return true;
return HandleRequest(request, response_callback);
}
private:
bool VerifyConnection(int connection_id) {
return connection_id_set_.find(connection_id) != connection_id_set_.end();
}
void VerifyResults() {
EXPECT_TRUE(got_initialized_);
EXPECT_TRUE(got_shutdown_);
if (!IsChromeRuntimeEnabled()) {
EXPECT_TRUE(connection_id_set_.empty());
EXPECT_EQ(expected_connection_ct_, actual_connection_ct_) << request_log_;
EXPECT_EQ(expected_http_request_ct_, actual_http_request_ct_)
<< request_log_;
}
EXPECT_EQ(expected_http_request_ct_, actual_http_request_ct_)
<< request_log_;
}
void HandleRequest(CefRefPtr<CefServer> server,
int connection_id,
CefRefPtr<CefRequest> request) {
bool HandleRequest(CefRefPtr<CefRequest> request,
const ResponseCallback& response_callback) {
RequestDataMap::Entry entry = data_map_.Find(request->GetURL());
if (entry.type == RequestDataMap::Entry::TYPE_NORMAL) {
const bool needs_auth = entry.settings->expect_authentication &&
!IsAuthorized(request, entry.settings->username,
entry.settings->password);
if (needs_auth) {
HandleAuthRequest(server, connection_id, request);
return;
return HandleAuthRequest(request, response_callback);
}
HandleNormalRequest(server, connection_id, request, entry.settings);
return HandleNormalRequest(request, response_callback, entry.settings);
} else if (entry.type == RequestDataMap::Entry::TYPE_REDIRECT) {
HandleRedirectRequest(server, connection_id, request,
entry.redirect_request, entry.redirect_response);
return HandleRedirectRequest(request, response_callback,
entry.redirect_request,
entry.redirect_response);
} else {
// Unknown test.
ADD_FAILURE() << "url: " << request->GetURL().ToString();
server->SendHttp500Response(connection_id, "Unknown test");
}
return false;
}
static void HandleAuthRequest(CefRefPtr<CefServer> server,
int connection_id,
CefRefPtr<CefRequest> request) {
static bool HandleAuthRequest(CefRefPtr<CefRequest> request,
const ResponseCallback& response_callback) {
CefRefPtr<CefResponse> response = CefResponse::Create();
GetAuthResponse(response);
SendResponse(server, connection_id, response, std::string());
response_callback.Run(response, std::string());
return true;
}
static void HandleNormalRequest(CefRefPtr<CefServer> server,
int connection_id,
CefRefPtr<CefRequest> request,
static bool HandleNormalRequest(CefRefPtr<CefRequest> request,
const ResponseCallback& response_callback,
RequestRunSettings* settings) {
VerifyNormalRequest(settings, request, true);
@@ -1432,12 +1371,12 @@ class RequestServerHandler : public test_server::ObserverHelper {
response_data = settings->response_data.substr(expected_offset);
}
SendResponse(server, connection_id, response, response_data);
response_callback.Run(response, response_data);
return true;
}
static void HandleRedirectRequest(CefRefPtr<CefServer> server,
int connection_id,
CefRefPtr<CefRequest> request,
static bool HandleRedirectRequest(CefRefPtr<CefRequest> request,
const ResponseCallback& response_callback,
CefRefPtr<CefRequest> redirect_request,
CefRefPtr<CefResponse> redirect_response) {
if (redirect_response->GetStatus() == 302) {
@@ -1455,50 +1394,8 @@ class RequestServerHandler : public test_server::ObserverHelper {
// Verify that the request was sent correctly.
TestRequestEqual(redirect_request, request, true);
SendResponse(server, connection_id, redirect_response, std::string());
}
static void SendResponse(CefRefPtr<CefServer> server,
int connection_id,
CefRefPtr<CefResponse> response,
const std::string& response_data) {
// Execute on the server thread because some methods require it.
CefRefPtr<CefTaskRunner> task_runner = server->GetTaskRunner();
if (!task_runner->BelongsToCurrentThread()) {
task_runner->PostTask(CefCreateClosureTask(
base::BindOnce(RequestServerHandler::SendResponse, server,
connection_id, response, response_data)));
return;
}
const int response_code = response->GetStatus();
if (response_code <= 0) {
// Intentionally not responding for incomplete request tests.
return;
}
const CefString& content_type = response->GetMimeType();
int64 content_length = static_cast<int64>(response_data.size());
CefResponse::HeaderMap extra_headers;
response->GetHeaderMap(extra_headers);
server->SendHttpResponse(connection_id, response_code, content_type,
content_length, extra_headers);
if (response_data == kIncompleteDoNotSendData) {
// Intentionally not sending data for incomplete request tests.
return;
}
if (content_length != 0) {
server->SendRawData(connection_id, response_data.data(),
response_data.size());
server->CloseConnection(connection_id);
}
// The connection should be closed.
EXPECT_FALSE(server->IsValidConnection(connection_id));
response_callback.Run(redirect_response, std::string());
return true;
}
void RunCompleteCallback(bool startup) {
@@ -1526,11 +1423,6 @@ class RequestServerHandler : public test_server::ObserverHelper {
TrackCallback got_initialized_;
TrackCallback got_shutdown_;
typedef std::set<int> ConnectionIdSet;
ConnectionIdSet connection_id_set_;
int expected_connection_ct_;
int actual_connection_ct_;
int expected_http_request_ct_;
int actual_http_request_ct_;
@@ -2397,7 +2289,7 @@ class RequestTestRunner : public base::RefCountedThreadSafe<RequestTestRunner> {
// There will be a response but the request will be aborted without
// receiving any data.
settings_.response_data = kIncompleteDoNotSendData;
settings_.response_data = test_server::kIncompleteDoNotSendData;
settings_.expected_error_code = ERR_ABORTED;
settings_.expected_status = UR_FAILED;
// TODO(network): Download progress notifications are sent for incomplete