Skip to content

Commit 7dd6163

Browse files
authored
[grid][java]: relay service can set protocol version in fetching status (#13849)
* [grid][java]: relay service can set protocol version in fetching status Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com> * [grid][java] fix code as suggestions Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com> --------- Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
1 parent e6e7d8e commit 7dd6163

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

java/src/org/openqa/selenium/grid/node/relay/RelayFlags.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ public class RelayFlags implements HasRoles {
8484
@ConfigValue(section = RELAY_SECTION, name = "status-endpoint", example = "\"/status\"")
8585
private String serviceStatusEndpoint;
8686

87+
@Parameter(
88+
names = {"--service-protocol-version"},
89+
description =
90+
"Enforce a specific protocol version in HttpClient when communicating with the endpoint"
91+
+ " service status")
92+
@ConfigValue(section = RELAY_SECTION, name = "protocol-version", example = "\"HTTP/1.1\"")
93+
private String serviceProtocolVersion;
94+
8795
@Override
8896
public Set<Role> getRoles() {
8997
return Collections.singleton(NODE_ROLE);

java/src/org/openqa/selenium/grid/node/relay/RelayOptions.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.common.collect.Multimap;
2626
import java.net.URI;
2727
import java.net.URISyntaxException;
28+
import java.net.http.HttpClient.Version;
2829
import java.time.Duration;
2930
import java.util.Collection;
3031
import java.util.List;
@@ -103,6 +104,27 @@ public URI getServiceStatusUri() {
103104
}
104105
}
105106

107+
public String getServiceProtocolVersion() {
108+
String rawProtocolVersion = config.get(RELAY_SECTION, "protocol-version").orElse("");
109+
String protocolVersion = rawProtocolVersion;
110+
if (protocolVersion.isEmpty()) {
111+
return protocolVersion;
112+
} else {
113+
protocolVersion = normalizeProtocolVersion(protocolVersion);
114+
}
115+
try {
116+
return Version.valueOf(protocolVersion).toString();
117+
} catch (IllegalArgumentException e) {
118+
LOG.info("Unsupported protocol version: " + protocolVersion);
119+
throw new ConfigException("Unsupported protocol version provided: " + rawProtocolVersion, e);
120+
}
121+
}
122+
123+
private String normalizeProtocolVersion(String protocolVersion) {
124+
// Support input in the form of "http/1.1" or "HTTP/1.1"
125+
return protocolVersion.toUpperCase().replaceAll("/", "_").replaceAll("\\.", "_");
126+
}
127+
106128
// Method being used in SessionSlot
107129
@SuppressWarnings("unused")
108130
private boolean isServiceUp(HttpClient client) {
@@ -160,6 +182,7 @@ public Map<Capabilities, Collection<SessionFactory>> getSessionFactories(
160182
sessionTimeout,
161183
getServiceUri(),
162184
getServiceStatusUri(),
185+
getServiceProtocolVersion(),
163186
immutable));
164187
}
165188
LOG.info(String.format("Mapping %s, %d times", immutable, maxSessions));

java/src/org/openqa/selenium/grid/node/relay/RelaySessionFactory.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public class RelaySessionFactory implements SessionFactory {
7676
private final Duration sessionTimeout;
7777
private final URL serviceUrl;
7878
private final URL serviceStatusUrl;
79+
private final String serviceProtocolVersion;
7980
private final Capabilities stereotype;
8081

8182
public RelaySessionFactory(
@@ -84,12 +85,15 @@ public RelaySessionFactory(
8485
Duration sessionTimeout,
8586
URI serviceUri,
8687
URI serviceStatusUri,
88+
String serviceProtocolVersion,
8789
Capabilities stereotype) {
8890
this.tracer = Require.nonNull("Tracer", tracer);
8991
this.clientFactory = Require.nonNull("HTTP client", clientFactory);
9092
this.sessionTimeout = Require.nonNull("Session timeout", sessionTimeout);
9193
this.serviceUrl = createUrlFromUri(Require.nonNull("Service URL", serviceUri));
9294
this.serviceStatusUrl = createUrlFromUri(serviceStatusUri);
95+
this.serviceProtocolVersion =
96+
Require.nonNull("Service protocol version", serviceProtocolVersion);
9397
this.stereotype = ImmutableCapabilities.copyOf(Require.nonNull("Stereotype", stereotype));
9498
}
9599

@@ -209,7 +213,12 @@ public boolean isServiceUp() {
209213
// If no status endpoint was configured, we assume the server is up.
210214
return true;
211215
}
212-
try (HttpClient client = clientFactory.createClient(serviceStatusUrl)) {
216+
217+
ClientConfig clientConfig = ClientConfig.defaultConfig().baseUrl(serviceStatusUrl);
218+
if (!serviceProtocolVersion.isEmpty()) {
219+
clientConfig = clientConfig.version(serviceProtocolVersion);
220+
}
221+
try (HttpClient client = clientFactory.createClient(clientConfig)) {
213222
HttpResponse response =
214223
client.execute(new HttpRequest(HttpMethod.GET, serviceStatusUrl.toString()));
215224
LOG.log(Debug.getDebugLogLevel(), () -> Contents.string(response));

java/test/org/openqa/selenium/grid/node/relay/RelayOptionsTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,55 @@ void statusUrlIsParsedSuccessfully() {
9595
.isEqualTo("http://127.0.0.1:8888/statusEndpoint");
9696
}
9797

98+
@Test
99+
void protocolVersionIsParsedSuccessfully() {
100+
String[] rawConfig =
101+
new String[] {
102+
"[relay]",
103+
"host = '127.0.0.1'",
104+
"port = '8888'",
105+
"status-endpoint = '/statusEndpoint'",
106+
"protocol-version = 'HTTP/1.1'",
107+
"configs = [\"5\", '{\"browserName\": \"firefox\"}']",
108+
};
109+
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
110+
RelayOptions relayOptions = new RelayOptions(config);
111+
assertThat(relayOptions.getServiceProtocolVersion()).isEqualTo("HTTP_1_1");
112+
rawConfig =
113+
new String[] {
114+
"[relay]",
115+
"host = '127.0.0.1'",
116+
"port = '8888'",
117+
"status-endpoint = '/statusEndpoint'",
118+
"protocol-version = 'HTTP_1_1'",
119+
"configs = [\"5\", '{\"browserName\": \"firefox\"}']",
120+
};
121+
config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
122+
relayOptions = new RelayOptions(config);
123+
assertThat(relayOptions.getServiceProtocolVersion()).isEqualTo("HTTP_1_1");
124+
}
125+
126+
@Test
127+
void protocolVersionThrowsConfigException() {
128+
String[] rawConfig =
129+
new String[] {
130+
"[relay]",
131+
"host = '127.0.0.1'",
132+
"port = '8888'",
133+
"status-endpoint = '/statusEndpoint'",
134+
"protocol-version = 'HTTP/0.9'",
135+
"configs = [\"5\", '{\"browserName\": \"firefox\"}']",
136+
};
137+
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
138+
RelayOptions relayOptions = new RelayOptions(config);
139+
assertThatExceptionOfType(ConfigException.class)
140+
.isThrownBy(
141+
() -> {
142+
relayOptions.getServiceProtocolVersion();
143+
})
144+
.withMessageContaining("Unsupported protocol version provided: HTTP/0.9");
145+
}
146+
98147
@Test
99148
void missingConfigsThrowsConfigException() {
100149
String[] rawConfig =

0 commit comments

Comments
 (0)