Skip to content

Commit

Permalink
WebDriver: Implement GET /session/{id}/cookie/{name} endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TobyAsE authored and linusg committed Oct 16, 2022
1 parent b6f101f commit a34f8c4
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Userland/Applications/Browser/WebDriverConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,18 @@ Messages::WebDriverSessionClient::GetAllCookiesResponse WebDriverConnection::get
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 { {} };
}

}
1 change: 1 addition & 0 deletions Userland/Applications/Browser/WebDriverConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class WebDriverConnection final
virtual void back() override;
virtual void forward() override;
virtual Messages::WebDriverSessionClient::GetAllCookiesResponse get_all_cookies() override;
virtual Messages::WebDriverSessionClient::GetNamedCookieResponse get_named_cookie(String const& name) override;

private:
WebDriverConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, NonnullRefPtr<BrowserWindow> browser_window);
Expand Down
2 changes: 2 additions & 0 deletions Userland/Applications/Browser/WebDriverSessionClient.ipc
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ endpoint WebDriverSessionClient {
back() =|
forward() =|
get_all_cookies() => (Vector<Web::Cookie::Cookie> cookies)
get_named_cookie(String name) => (Optional<Web::Cookie::Cookie> cookie)

}
13 changes: 13 additions & 0 deletions Userland/Services/WebDriver/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

}
1 change: 1 addition & 0 deletions Userland/Services/WebDriver/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Client final : public Core::Object {
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&);
Expand Down
24 changes: 24 additions & 0 deletions Userland/Services/WebDriver/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
}

}
1 change: 1 addition & 0 deletions Userland/Services/WebDriver/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Session {
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;
Expand Down

0 comments on commit a34f8c4

Please sign in to comment.