1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 12:07:44 +00:00

WebDriver: Implement DELETE /session/{id}/cookie endpoint

This commit is contained in:
Tobias Christiansen 2022-10-16 19:55:01 +02:00 committed by Linus Groh
parent c710780852
commit 0d6dc2492d
4 changed files with 51 additions and 0 deletions

View file

@ -32,6 +32,7 @@ Vector<Client::Route> Client::s_routes = {
{ HTTP::HttpRequest::Method::POST, { "session", ":session_id", "forward" }, &Client::handle_forward },
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie" }, &Client::handle_get_all_cookies },
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie", ":name" }, &Client::handle_get_named_cookie },
{ HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "cookie" }, &Client::handle_delete_all_cookies },
};
Client::Client(NonnullOwnPtr<Core::Stream::BufferedTCPSocket> socket, Core::Object* parent)
@ -514,4 +515,15 @@ ErrorOr<JsonValue, HttpError> Client::handle_get_named_cookie(Vector<StringView>
return make_json_value(cookies);
}
// DELETE /session/{session id}/cookie https://w3c.github.io/webdriver/#dfn-delete-all-cookies
ErrorOr<JsonValue, HttpError> Client::handle_delete_all_cookies(Vector<StringView> parameters, JsonValue const&)
{
dbgln_if(WEBDRIVER_DEBUG, "Handling DELETE /session/<session_id>/cookie");
Session* session = TRY(find_session_with_id(parameters[0]));
// NOTE: Spec steps handled in Session::delete_all_cookies().
auto result = TRY(session->delete_all_cookies());
return make_json_value(result);
}
}

View file

@ -58,6 +58,7 @@ private:
ErrorOr<JsonValue, HttpError> handle_forward(Vector<StringView>, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_get_all_cookies(Vector<StringView>, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_get_named_cookie(Vector<StringView>, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_delete_all_cookies(Vector<StringView>, JsonValue const& payload);
ErrorOr<Session*, HttpError> find_session_with_id(StringView session_id);
JsonValue make_json_value(JsonValue const&);

View file

@ -297,4 +297,39 @@ ErrorOr<JsonValue, HttpError> Session::get_named_cookie(String const& name)
return HttpError { 404, "no such cookie", "Cookie not found" };
}
// https://w3c.github.io/webdriver/#dfn-delete-cookies
void Session::delete_cookies(Optional<StringView> const& name)
{
// For each cookie among all associated cookies of the current browsing contexts active document,
// run the substeps of the first matching condition:
for (auto& cookie : m_browser_connection->get_all_cookies()) {
// -> name is undefined
// -> name is equal to cookie name
if (!name.has_value() || name.value() == cookie.name) {
// Set the cookie expiry time to a Unix timestamp in the past.
cookie.expiry_time = Core::DateTime::from_timestamp(0);
m_browser_connection->async_update_cookie(cookie);
}
// -> Otherwise
// Do nothing.
}
}
// DELETE /session/{session id}/cookie https://w3c.github.io/webdriver/#dfn-delete-all-cookies
ErrorOr<JsonValue, HttpError> Session::delete_all_cookies()
{
// 1. If the current browsing context is no longer open, return error with error code no such window.
auto current_window = get_window_object();
if (!current_window.has_value())
return HttpError { 404, "no such window", "Window not found" };
// FIXME: 2. Handle any user prompts, and return its value if it is an error.
// 3. Delete cookies, giving no filtering argument.
delete_cookies();
// 4. Return success with data null.
return JsonValue();
}
}

View file

@ -42,8 +42,11 @@ public:
ErrorOr<JsonValue, HttpError> forward();
ErrorOr<JsonValue, HttpError> get_all_cookies();
ErrorOr<JsonValue, HttpError> get_named_cookie(String const& name);
ErrorOr<JsonValue, HttpError> delete_all_cookies();
private:
void delete_cookies(Optional<StringView> const& name = {});
NonnullRefPtr<Client> m_client;
bool m_started { false };
unsigned m_id { 0 };