Skip to content

Commit

Permalink
[java] Update subPath for ProxyNodeWebsockets
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <[email protected]>
  • Loading branch information
VietND96 committed Jan 8, 2024
1 parent b0f42c3 commit b42111a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
public class ProxyNodeWebsockets
implements BiFunction<String, Consumer<Message>, Optional<Consumer<Message>>> {

private static final UrlTemplate CDP_TEMPLATE = new UrlTemplate("/session/{sessionId}/se/cdp");
private static final UrlTemplate BIDI_TEMPLATE = new UrlTemplate("/session/{sessionId}/se/bidi");
private static final UrlTemplate FWD_TEMPLATE = new UrlTemplate("/session/{sessionId}/se/fwd");
private static final UrlTemplate VNC_TEMPLATE = new UrlTemplate("/session/{sessionId}/se/vnc");
private static final UrlTemplate CDP_TEMPLATE = new UrlTemplate("^/session/{sessionId}/se/cdp");
private static final UrlTemplate BIDI_TEMPLATE = new UrlTemplate("^/session/{sessionId}/se/bidi");
private static final UrlTemplate FWD_TEMPLATE = new UrlTemplate("^/session/{sessionId}/se/fwd");
private static final UrlTemplate VNC_TEMPLATE = new UrlTemplate("^/session/{sessionId}/se/vnc");
private static final Logger LOG = Logger.getLogger(ProxyNodeWebsockets.class.getName());
private static final ImmutableSet<String> CDP_ENDPOINT_CAPS =
ImmutableSet.of("goog:chromeOptions", "moz:debuggerAddress", "ms:edgeOptions");
Expand Down
20 changes: 20 additions & 0 deletions java/src/org/openqa/selenium/grid/node/config/NodeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class NodeOptions {
public static final boolean DEFAULT_ENABLE_CDP = true;
public static final boolean DEFAULT_ENABLE_BIDI = true;
static final String NODE_SECTION = "node";
static final String NETWORK_SECTION = "network";
static final boolean DEFAULT_DETECT_DRIVERS = true;
static final boolean DEFAULT_USE_SELENIUM_MANAGER = false;
static final boolean OVERRIDE_MAX_SESSIONS = false;
Expand Down Expand Up @@ -162,6 +163,25 @@ public boolean isManagedDownloadsEnabled() {
return config.getBool(NODE_SECTION, "enable-managed-downloads").orElse(Boolean.FALSE);
}

public String getSubPath() {
String configSubPath = normalizeSubPath(config.get(NETWORK_SECTION, "sub-path").orElse(""));
if (configSubPath.isEmpty()) {
configSubPath = normalizeSubPath(getPublicGridUri().map(URI::getPath).orElse(configSubPath));
}
return configSubPath;
}

public static String normalizeSubPath(String prefix) {
prefix = prefix.trim();
if (!prefix.startsWith("/")) {
prefix = "/" + prefix; // Prefix with a '/' if absent.
}
if (prefix.endsWith("/")) {
prefix = prefix.substring(0, prefix.length() - 1); // Remove the trailing '/' if present.
}
return prefix;
}

public Node getNode() {
return config.getClass(NODE_SECTION, "implementation", Node.class, DEFAULT_NODE_IMPLEMENTATION);
}
Expand Down
4 changes: 1 addition & 3 deletions java/src/org/openqa/selenium/grid/node/local/LocalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -838,9 +838,7 @@ private Session createExternalSession(
private URI rewrite(String path) {
try {
String scheme = "https".equals(gridUri.getScheme()) ? "wss" : "ws";
if (gridUri.getPath() != null && !gridUri.getPath().equals("/")) {
path = gridUri.getPath() + path;
}
path = NodeOptions.normalizeSubPath(gridUri.getPath()) + path;
return new URI(
scheme, gridUri.getUserInfo(), gridUri.getHost(), gridUri.getPort(), path, null, null);
} catch (URISyntaxException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,39 @@ void settingTheHubWithDefaultValueSetsTheGridUrlToTheNonLoopbackAddress() {
.isEqualTo(Optional.of(URI.create(nonLoopbackAddressUrl)));
}

@Test
void settingSubPathForNodeServerFromNetworkSection() {
String[] rawConfig =
new String[] {
"[network]", "sub-path = \"/mySubPath\"",
};
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
NodeOptions nodeOptions = new NodeOptions(config);
assertThat(nodeOptions.getSubPath()).isEqualTo("/mySubPath");
}

@Test
void settingSubPathForNodeServerExtractFromGridUrl() {
String[] rawConfig =
new String[] {
"[node]", "grid-url = \"http://localhost:4444/mySubPath\"",
};
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
NodeOptions nodeOptions = new NodeOptions(config);
assertThat(nodeOptions.getSubPath()).isEqualTo("/mySubPath");
}

@Test
void settingSubPathForNodeServerExtractFromHub() {
String[] rawConfig =
new String[] {
"[node]", "hub = \"http://0.0.0.0:4444/mySubPath\"",
};
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
NodeOptions nodeOptions = new NodeOptions(config);
assertThat(nodeOptions.getSubPath()).isEqualTo("/mySubPath");
}

@Test
void notSettingSlotMatcherAvailable() {
String[] rawConfig =
Expand Down

0 comments on commit b42111a

Please sign in to comment.