Skip to content

Commit 815bc89

Browse files
committed
added new tests
1 parent de4ab85 commit 815bc89

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ public class JdkHttpClient implements HttpClient {
8080
private final ExecutorService executorService;
8181
private final Duration readTimeout;
8282
private final Duration connectTimeout;
83+
private final ClientConfig config;
8384

8485
JdkHttpClient(ClientConfig config) {
8586
Objects.requireNonNull(config, "Client config must be set");
86-
87+
88+
this.config = config;
8789
this.messages = new JdkHttpMessages(config);
8890
this.readTimeout = config.readTimeout();
8991
this.connectTimeout = config.connectionTimeout();
@@ -338,7 +340,7 @@ public WebSocket send(Message message) {
338340
throw new WebDriverException(cause);
339341
} catch (InterruptedException e) {
340342
Thread.currentThread().interrupt();
341-
throw new WebDriverException(e.getMessage());
343+
throw new WebDriverException(e.getMessage(), e);
342344
} catch (java.util.concurrent.TimeoutException e) {
343345
throw new TimeoutException(e);
344346
} finally {
@@ -522,6 +524,11 @@ private HttpResponse execute0(HttpRequest req) throws UncheckedIOException {
522524
}
523525
}
524526

527+
// Package-private method for testing
528+
URI getBaseUri() {
529+
return config.baseUri();
530+
}
531+
525532
@Override
526533
public void close() {
527534
if (this.client == null) {

java/test/org/openqa/selenium/remote/http/jdk/JdkHttpClientTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,65 @@
1919

2020
import org.openqa.selenium.remote.http.HttpClient;
2121
import org.openqa.selenium.remote.internal.HttpClientTestBase;
22+
import org.junit.Test;
23+
import static org.hamcrest.MatcherAssert.assertThat;
24+
import static org.hamcrest.Matchers.is;
25+
import static org.hamcrest.Matchers.isNull;
26+
27+
import java.net.URI;
28+
import java.net.URISyntaxException;
2229

2330
class JdkHttpClientTest extends HttpClientTestBase {
2431

2532
@Override
2633
protected HttpClient.Factory createFactory() {
2734
return new JdkHttpClient.Factory();
2835
}
36+
37+
@Test
38+
void shouldStripCredentialsFromUrl() throws URISyntaxException {
39+
URI originalUri = new URI("http://admin:password@localhost:4444/wd/hub");
40+
ClientConfig config = ClientConfig.defaultConfig().baseUri(originalUri);
41+
42+
JdkHttpClient client = new JdkHttpClient(config);
43+
44+
// Get the modified URI from the client's config
45+
URI modifiedUri = client.getBaseUri();
46+
47+
assertThat(modifiedUri.getUserInfo()).isNull();
48+
assertThat(modifiedUri.getHost()).isEqualTo("localhost");
49+
assertThat(modifiedUri.getPort()).isEqualTo(4444);
50+
assertThat(modifiedUri.getPath()).isEqualTo("/wd/hub");
51+
}
52+
53+
@Test
54+
void shouldHandleUrlWithoutCredentials() throws URISyntaxException {
55+
URI originalUri = new URI("http://localhost:4444/wd/hub");
56+
ClientConfig config = ClientConfig.defaultConfig().baseUri(originalUri);
57+
58+
JdkHttpClient client = new JdkHttpClient(config);
59+
60+
URI modifiedUri = client.getBaseUri();
61+
62+
assertThat(modifiedUri).isEqualTo(originalUri);
63+
}
64+
65+
@Test
66+
void shouldPreserveUrlComponentsExceptCredentials() throws URISyntaxException {
67+
URI originalUri = new URI(
68+
"https://admin:password@localhost:4444/wd/hub?debug=true#fragment");
69+
ClientConfig config = ClientConfig.defaultConfig().baseUri(originalUri);
70+
71+
JdkHttpClient client = new JdkHttpClient(config);
72+
73+
URI modifiedUri = client.getBaseUri();
74+
75+
assertThat(modifiedUri.getScheme()).isEqualTo("https");
76+
assertThat(modifiedUri.getUserInfo()).isNull();
77+
assertThat(modifiedUri.getHost()).isEqualTo("localhost");
78+
assertThat(modifiedUri.getPort()).isEqualTo(4444);
79+
assertThat(modifiedUri.getPath()).isEqualTo("/wd/hub");
80+
assertThat(modifiedUri.getQuery()).isEqualTo("debug=true");
81+
assertThat(modifiedUri.getFragment()).isEqualTo("fragment");
82+
}
2983
}

0 commit comments

Comments
 (0)