1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:17:34 +00:00

WebDriver: Implement GET /session/{id}/cookie/{name} endpoint

This commit is contained in:
Tobias Christiansen 2022-10-15 19:22:20 +02:00 committed by Linus Groh
parent b6f101f1c0
commit a34f8c444b
7 changed files with 56 additions and 0 deletions

View file

@ -81,4 +81,18 @@ Messages::WebDriverSessionClient::GetAllCookiesResponse WebDriverConnection::get
return { {} }; return { {} };
} }
Messages::WebDriverSessionClient::GetNamedCookieResponse WebDriverConnection::get_named_cookie(String const& name)
{
dbgln("WebDriverConnection: get_named_cookie");
if (auto browser_window = m_browser_window.strong_ref()) {
if (browser_window->active_tab().on_get_cookies_entries) {
for (auto cookie : browser_window->active_tab().on_get_cookies_entries()) {
if (cookie.name == name)
return { cookie };
}
}
}
return { {} };
}
} }

View file

@ -44,6 +44,7 @@ public:
virtual void back() override; virtual void back() override;
virtual void forward() override; virtual void forward() override;
virtual Messages::WebDriverSessionClient::GetAllCookiesResponse get_all_cookies() override; virtual Messages::WebDriverSessionClient::GetAllCookiesResponse get_all_cookies() override;
virtual Messages::WebDriverSessionClient::GetNamedCookieResponse get_named_cookie(String const& name) override;
private: private:
WebDriverConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, NonnullRefPtr<BrowserWindow> browser_window); WebDriverConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, NonnullRefPtr<BrowserWindow> browser_window);

View file

@ -12,4 +12,6 @@ endpoint WebDriverSessionClient {
back() =| back() =|
forward() =| forward() =|
get_all_cookies() => (Vector<Web::Cookie::Cookie> cookies) get_all_cookies() => (Vector<Web::Cookie::Cookie> cookies)
get_named_cookie(String name) => (Optional<Web::Cookie::Cookie> cookie)
} }

View file

@ -31,6 +31,7 @@ Vector<Client::Route> Client::s_routes = {
{ HTTP::HttpRequest::Method::POST, { "session", ":session_id", "back" }, &Client::handle_back }, { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "back" }, &Client::handle_back },
{ HTTP::HttpRequest::Method::POST, { "session", ":session_id", "forward" }, &Client::handle_forward }, { 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" }, &Client::handle_get_all_cookies },
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie", ":name" }, &Client::handle_get_named_cookie },
}; };
Client::Client(NonnullOwnPtr<Core::Stream::BufferedTCPSocket> socket, Core::Object* parent) Client::Client(NonnullOwnPtr<Core::Stream::BufferedTCPSocket> socket, Core::Object* parent)
@ -501,4 +502,16 @@ ErrorOr<JsonValue, HttpError> Client::handle_get_all_cookies(Vector<StringView>
return make_json_value(cookies); return make_json_value(cookies);
} }
// GET /session/{session id}/cookie/{name} https://w3c.github.io/webdriver/#dfn-get-named-cookie
ErrorOr<JsonValue, HttpError> Client::handle_get_named_cookie(Vector<StringView> parameters, JsonValue const&)
{
dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/cookie/<name>");
Session* session = TRY(find_session_with_id(parameters[0]));
// NOTE: Spec steps handled in Session::get_all_cookies().
auto cookies = TRY(session->get_named_cookie(parameters[1]));
return make_json_value(cookies);
}
} }

View file

@ -57,6 +57,7 @@ private:
ErrorOr<JsonValue, HttpError> handle_back(Vector<StringView>, JsonValue const& payload); ErrorOr<JsonValue, HttpError> handle_back(Vector<StringView>, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_forward(Vector<StringView>, JsonValue const& payload); 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_all_cookies(Vector<StringView>, JsonValue const& payload);
ErrorOr<JsonValue, HttpError> handle_get_named_cookie(Vector<StringView>, JsonValue const& payload);
ErrorOr<Session*, HttpError> find_session_with_id(StringView session_id); ErrorOr<Session*, HttpError> find_session_with_id(StringView session_id);
JsonValue make_json_value(JsonValue const&); JsonValue make_json_value(JsonValue const&);

View file

@ -273,4 +273,28 @@ ErrorOr<JsonValue, HttpError> Session::get_all_cookies()
return JsonValue(cookies); return JsonValue(cookies);
} }
// GET /session/{session id}/cookie/{name} https://w3c.github.io/webdriver/#dfn-get-named-cookie
ErrorOr<JsonValue, HttpError> Session::get_named_cookie(String const& name)
{
// 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. If the url variable name is equal to a cookies cookie name amongst all associated cookies of the
// current browsing contexts active document, return success with the serialized cookie as data.
auto maybe_cookie = m_browser_connection->get_named_cookie(name);
if (maybe_cookie.has_value()) {
auto cookie = maybe_cookie.release_value();
auto serialized_cookie = serialize_cookie(cookie);
return JsonValue(serialized_cookie);
}
// 4. Otherwise, return error with error code no such cookie.
return HttpError { 404, "no such cookie", "Cookie not found" };
}
} }

View file

@ -41,6 +41,7 @@ public:
ErrorOr<JsonValue, HttpError> back(); ErrorOr<JsonValue, HttpError> back();
ErrorOr<JsonValue, HttpError> forward(); ErrorOr<JsonValue, HttpError> forward();
ErrorOr<JsonValue, HttpError> get_all_cookies(); ErrorOr<JsonValue, HttpError> get_all_cookies();
ErrorOr<JsonValue, HttpError> get_named_cookie(String const& name);
private: private:
NonnullRefPtr<Client> m_client; NonnullRefPtr<Client> m_client;