1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:27:46 +00:00

WebContent+WebDriver: Fully implement closing a session

There were a couple steps missing to close the remote end. Further, we
were not removing the session from the list of active sessions.
This commit is contained in:
Timothy Flynn 2022-11-08 14:14:29 -05:00 committed by Tim Flynn
parent 3ba6b5a7cb
commit cc78a74c51
7 changed files with 47 additions and 8 deletions

View file

@ -331,6 +331,21 @@ ErrorOr<Session*, Web::WebDriver::Error> Client::find_session_with_id(StringView
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSessionId, "Invalid session id");
}
ErrorOr<NonnullOwnPtr<Session>, Web::WebDriver::Error> Client::take_session_with_id(StringView session_id)
{
auto session_id_or_error = session_id.to_uint<>();
if (!session_id_or_error.has_value())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSessionId, "Invalid session id");
for (size_t i = 0; i < Client::s_sessions.size(); ++i) {
if (Client::s_sessions[i].session_id() == session_id_or_error.value()) {
return Client::s_sessions.take(i);
}
}
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSessionId, "Invalid session id");
}
void Client::close_session(unsigned session_id)
{
bool found = Client::s_sessions.remove_first_matching([&](auto const& it) {
@ -426,11 +441,8 @@ Web::WebDriver::Response Client::handle_delete_session(Vector<StringView> const&
dbgln_if(WEBDRIVER_DEBUG, "Handling DELETE /session/<session_id>");
// 1. If the current session is an active session, try to close the session.
auto* session = TRY(find_session_with_id(parameters[0]));
auto stop_result = session->stop();
if (stop_result.is_error())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::UnsupportedOperation, stop_result.error().string_literal());
auto session = TRY(take_session_with_id(parameters[0]));
TRY(session->stop());
// 2. Return success with data null.
return make_json_value(JsonValue());