Skip to content

Commit 9c95b92

Browse files
committed
[grid] Exposing methods to get service url and endpoint
1 parent 668d131 commit 9c95b92

2 files changed

Lines changed: 42 additions & 37 deletions

File tree

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public RelayOptions(Config config) {
5555
this.config = Require.nonNull("Config", config);
5656
}
5757

58-
private URI getServiceUri() {
58+
public URI getServiceUri() {
5959
try {
6060
Optional<String> possibleUri = config.get(RELAY_SECTION, "url");
6161
if (possibleUri.isPresent()) {
@@ -88,17 +88,30 @@ private URI getServiceUri() {
8888
}
8989
}
9090

91-
private boolean isServerUp(HttpClient client) {
92-
if (!config.get(RELAY_SECTION, "status-endpoint").isPresent()) {
91+
public URI getServiceStatusUri() {
92+
try {
93+
if (!config.get(RELAY_SECTION, "status-endpoint").isPresent()) {
94+
return null;
95+
}
96+
String statusEndpoint = config.get(RELAY_SECTION, "status-endpoint").orElse("/status");
97+
if (!statusEndpoint.startsWith("/")) {
98+
statusEndpoint = "/" + statusEndpoint;
99+
}
100+
URI serviceUri = getServiceUri();
101+
return new URI(serviceUri.toString() + statusEndpoint);
102+
} catch (URISyntaxException e) {
103+
throw new ConfigException("Unable to determine the service status url", e);
104+
}
105+
}
106+
107+
private boolean isServiceUp(HttpClient client) {
108+
URI serviceStatusUri = getServiceStatusUri();
109+
if (serviceStatusUri == null) {
93110
// If no status endpoint was configured, we assume the server is up.
94111
return true;
95112
}
96-
String statusEndpoint = config.get(RELAY_SECTION, "status-endpoint").orElse("/status");
97-
if (!statusEndpoint.startsWith("/")) {
98-
statusEndpoint = "/" + statusEndpoint;
99-
}
100113
try {
101-
HttpResponse response = client.execute(new HttpRequest(GET, statusEndpoint));
114+
HttpResponse response = client.execute(new HttpRequest(GET, serviceStatusUri.toString()));
102115
LOG.fine(string(response));
103116
return 200 == response.getStatus();
104117
} catch (Exception e) {
@@ -113,7 +126,7 @@ public Map<Capabilities, Collection<SessionFactory>> getSessionFactories(
113126
HttpClient client = clientFactory
114127
.createClient(ClientConfig.defaultConfig().baseUri(getServiceUri()));
115128

116-
if (!isServerUp(client)) {
129+
if (!isServiceUp(client)) {
117130
throw new ConfigException("Unable to reach the service at " + getServiceUri());
118131
}
119132

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

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public void basicConfigurationIsParsedSuccessfully() {
4949
NetworkOptions networkOptions = new NetworkOptions(config);
5050
Tracer tracer = DefaultTestTracer.createTracer();
5151
HttpClient.Factory httpClientFactory = networkOptions.getHttpClientFactory(tracer);
52+
RelayOptions relayOptions = new RelayOptions(config);
5253
Map<Capabilities, Collection<SessionFactory>>
53-
sessionFactories = new RelayOptions(config).getSessionFactories(tracer, httpClientFactory);
54+
sessionFactories = relayOptions.getSessionFactories(tracer, httpClientFactory);
5455

5556
Capabilities chrome = sessionFactories
5657
.keySet()
@@ -60,13 +61,7 @@ public void basicConfigurationIsParsedSuccessfully() {
6061
.orElseThrow(() -> new AssertionError("No value returned"));
6162

6263
assertThat(sessionFactories.get(chrome).size()).isEqualTo(2);
63-
64-
RelaySessionFactory relaySessionFactory = (RelaySessionFactory) sessionFactories.get(chrome)
65-
.stream()
66-
.findFirst()
67-
.orElseThrow(() -> new AssertionError("No value returned"));
68-
69-
assertThat(relaySessionFactory.getServiceUrl().toString()).isEqualTo("http://localhost:9999");
64+
assertThat(relayOptions.getServiceUri().toString()).isEqualTo("http://localhost:9999");
7065
}
7166

7267
@Test
@@ -78,27 +73,24 @@ public void hostAndPortAreParsedSuccessfully() {
7873
"configs = [\"5\", '{\"browserName\": \"firefox\"}']",
7974
};
8075
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
81-
NetworkOptions networkOptions = new NetworkOptions(config);
82-
Tracer tracer = DefaultTestTracer.createTracer();
83-
HttpClient.Factory httpClientFactory = networkOptions.getHttpClientFactory(tracer);
84-
Map<Capabilities, Collection<SessionFactory>>
85-
sessionFactories = new RelayOptions(config).getSessionFactories(tracer, httpClientFactory);
86-
87-
Capabilities firefox = sessionFactories
88-
.keySet()
89-
.stream()
90-
.filter(capabilities -> "firefox".equals(capabilities.getBrowserName()))
91-
.findFirst()
92-
.orElseThrow(() -> new AssertionError("No value returned"));
93-
94-
assertThat(sessionFactories.get(firefox).size()).isEqualTo(5);
95-
96-
RelaySessionFactory relaySessionFactory = (RelaySessionFactory) sessionFactories.get(firefox)
97-
.stream()
98-
.findFirst()
99-
.orElseThrow(() -> new AssertionError("No value returned"));
76+
RelayOptions relayOptions = new RelayOptions(config);
77+
assertThat(relayOptions.getServiceUri().toString()).isEqualTo("http://127.0.0.1:9999");
78+
}
10079

101-
assertThat(relaySessionFactory.getServiceUrl().toString()).isEqualTo("http://127.0.0.1:9999");
80+
@Test
81+
public void statusUrlIsParsedSuccessfully() {
82+
String[] rawConfig = new String[]{
83+
"[relay]",
84+
"host = '127.0.0.1'",
85+
"port = '8888'",
86+
"status-endpoint = '/statusEndpoint'",
87+
"configs = [\"5\", '{\"browserName\": \"firefox\"}']",
88+
};
89+
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
90+
RelayOptions relayOptions = new RelayOptions(config);
91+
assertThat(relayOptions.getServiceUri().toString()).isEqualTo("http://127.0.0.1:8888");
92+
assertThat(relayOptions.getServiceStatusUri().toString())
93+
.isEqualTo("http://127.0.0.1:8888/statusEndpoint");
10294
}
10395

10496
@Test

0 commit comments

Comments
 (0)