|
19 | 19 |
|
20 | 20 | import org.openqa.selenium.remote.http.HttpClient; |
21 | 21 | 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; |
22 | 29 |
|
23 | 30 | class JdkHttpClientTest extends HttpClientTestBase { |
24 | 31 |
|
25 | 32 | @Override |
26 | 33 | protected HttpClient.Factory createFactory() { |
27 | 34 | return new JdkHttpClient.Factory(); |
28 | 35 | } |
| 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 | + } |
29 | 83 | } |
0 commit comments