mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:17:44 +00:00
WebDriver: Implement GET /session/{id}/cookie/{name}
endpoint
This commit is contained in:
parent
b6f101f1c0
commit
a34f8c444b
7 changed files with 56 additions and 0 deletions
|
@ -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", "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 },
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -57,6 +57,7 @@ private:
|
|||
ErrorOr<JsonValue, HttpError> handle_back(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_named_cookie(Vector<StringView>, JsonValue const& payload);
|
||||
|
||||
ErrorOr<Session*, HttpError> find_session_with_id(StringView session_id);
|
||||
JsonValue make_json_value(JsonValue const&);
|
||||
|
|
|
@ -273,4 +273,28 @@ ErrorOr<JsonValue, HttpError> Session::get_all_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 cookie’s cookie name amongst all associated cookies of the
|
||||
// current browsing context’s 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" };
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -41,6 +41,7 @@ public:
|
|||
ErrorOr<JsonValue, HttpError> back();
|
||||
ErrorOr<JsonValue, HttpError> forward();
|
||||
ErrorOr<JsonValue, HttpError> get_all_cookies();
|
||||
ErrorOr<JsonValue, HttpError> get_named_cookie(String const& name);
|
||||
|
||||
private:
|
||||
NonnullRefPtr<Client> m_client;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue