Selaa lähdekoodia

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

Tobias Christiansen 2 vuotta sitten
vanhempi
commit
a34f8c444b

+ 14 - 0
Userland/Applications/Browser/WebDriverConnection.cpp

@@ -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 - 0
Userland/Applications/Browser/WebDriverConnection.h

@@ -44,6 +44,7 @@ public:
     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);

+ 2 - 0
Userland/Applications/Browser/WebDriverSessionClient.ipc

@@ -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 - 0
Userland/Services/WebDriver/Client.cpp

@@ -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);
+}
+
 }

+ 1 - 0
Userland/Services/WebDriver/Client.h

@@ -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&);

+ 24 - 0
Userland/Services/WebDriver/Session.cpp

@@ -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 - 0
Userland/Services/WebDriver/Session.h

@@ -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;