From 77384fbcb92799cac86a0283835664ae8a663b01 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 27 Nov 2024 12:06:54 +0530 Subject: [PATCH 1/3] change scope of `additionalCommands` to `protected` --- java/src/org/openqa/selenium/remote/HttpCommandExecutor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/src/org/openqa/selenium/remote/HttpCommandExecutor.java b/java/src/org/openqa/selenium/remote/HttpCommandExecutor.java index 03fc84bc45b19..c8e8de8a44e08 100644 --- a/java/src/org/openqa/selenium/remote/HttpCommandExecutor.java +++ b/java/src/org/openqa/selenium/remote/HttpCommandExecutor.java @@ -46,7 +46,7 @@ public class HttpCommandExecutor implements CommandExecutor, NeedsLocalLogs { private final URL remoteServer; public final HttpClient client; private final HttpClient.Factory httpClientFactory; - private final Map additionalCommands; + protected final Map additionalCommands; protected CommandCodec commandCodec; protected ResponseCodec responseCodec; From c4837a03a8b4a568c85f34a838a0e64d02dea9d8 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 27 Nov 2024 13:19:23 +0530 Subject: [PATCH 2/3] expose getter `getAdditionalCommands` --- .../selenium/remote/HttpCommandExecutor.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/java/src/org/openqa/selenium/remote/HttpCommandExecutor.java b/java/src/org/openqa/selenium/remote/HttpCommandExecutor.java index c8e8de8a44e08..1eb3283eae014 100644 --- a/java/src/org/openqa/selenium/remote/HttpCommandExecutor.java +++ b/java/src/org/openqa/selenium/remote/HttpCommandExecutor.java @@ -25,6 +25,8 @@ import java.io.IOException; import java.net.URL; +import java.util.Collections; +import java.util.HashMap; import java.util.Map; import org.openqa.selenium.NoSuchSessionException; import org.openqa.selenium.SessionNotCreatedException; @@ -46,7 +48,7 @@ public class HttpCommandExecutor implements CommandExecutor, NeedsLocalLogs { private final URL remoteServer; public final HttpClient client; private final HttpClient.Factory httpClientFactory; - protected final Map additionalCommands; + private final Map additionalCommands; protected CommandCodec commandCodec; protected ResponseCodec responseCodec; @@ -109,11 +111,34 @@ public HttpCommandExecutor( ClientConfig config, HttpClient.Factory httpClientFactory) { remoteServer = Require.nonNull("HTTP client configuration", config).baseUrl(); - this.additionalCommands = Require.nonNull("Additional commands", additionalCommands); + this.additionalCommands = + new HashMap<>(Require.nonNull("Additional commands", additionalCommands)); this.httpClientFactory = Require.nonNull("HTTP client factory", httpClientFactory); this.client = this.httpClientFactory.createClient(config); } + /** + * Returns an immutable view of the additional commands. + * + * @return an unmodifiable map of additional commands. + */ + public Map getAdditionalCommands() { + return Collections.unmodifiableMap(additionalCommands); + } + + /** + * Adds or updates additional commands. This method is protected to allow subclasses to define + * their commands. + * + * @param commandName the name of the command to add or update. + * @param info the CommandInfo for the command. + */ + protected void addAdditionalCommand(String commandName, CommandInfo info) { + Require.nonNull("Command name", commandName); + Require.nonNull("Command info", info); + this.additionalCommands.put(commandName, info); + } + /** * It may be useful to extend the commands understood by this {@code HttpCommandExecutor} at run * time, and this can be achieved via this method. Note, this is protected, and expected usage is From f4c02154abf98ac4808aa8c715efde2b228804db Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 27 Nov 2024 13:20:31 +0530 Subject: [PATCH 3/3] add unit test for `additionalCommands` --- .../RemoteWebDriverInitializationTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/java/test/org/openqa/selenium/remote/RemoteWebDriverInitializationTest.java b/java/test/org/openqa/selenium/remote/RemoteWebDriverInitializationTest.java index 111cac68e5085..8c01983488ed3 100644 --- a/java/test/org/openqa/selenium/remote/RemoteWebDriverInitializationTest.java +++ b/java/test/org/openqa/selenium/remote/RemoteWebDriverInitializationTest.java @@ -51,6 +51,7 @@ import org.openqa.selenium.remote.http.ClientConfig; import org.openqa.selenium.remote.http.Contents; import org.openqa.selenium.remote.http.HttpClient; +import org.openqa.selenium.remote.http.HttpMethod; import org.openqa.selenium.remote.http.HttpResponse; @Tag("UnitTests") @@ -234,4 +235,22 @@ public void quit() { quitCalled = true; } } + + @Test + void additionalCommandsCanBeModified() throws MalformedURLException { + HttpClient client = mock(HttpClient.class); + HttpClient.Factory factory = mock(HttpClient.Factory.class); + + when(factory.createClient(any(ClientConfig.class))).thenReturn(client); + + URL url = new URL("http://localhost:4444/"); + HttpCommandExecutor executor = + new HttpCommandExecutor(emptyMap(), ClientConfig.defaultConfig().baseUrl(url), factory); + + String commandName = "customCommand"; + CommandInfo commandInfo = new CommandInfo("/session/:sessionId/custom", HttpMethod.GET); + executor.addAdditionalCommand(commandName, commandInfo); + + assertThat(executor.getAdditionalCommands()).containsEntry(commandName, commandInfo); + } }