Skip to content

Commit

Permalink
Add basic auth to the JreHttpClient
Browse files Browse the repository at this point in the history
This allows it to be used with SauceLabs, and presumably
other cloud providers.
  • Loading branch information
shs96c committed Oct 3, 2017
1 parent 3578c0f commit c32c038
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.selenium.remote.internal;

import static java.nio.charset.StandardCharsets.UTF_8;

import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.HttpRequest;
Expand All @@ -28,19 +30,28 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class JreHttpClient implements HttpClient {

private final URL url;
private final String auth;

private JreHttpClient(URL url) {
if (!url.getProtocol().toLowerCase().startsWith("http")) {
throw new IllegalArgumentException("Base URL must be an http URL: " + url);
}
this.url = url;

String authority = url.getAuthority();
if (authority == null || "".equals(authority)) {
auth = null;
} else {
auth = "Basic " + Base64.getEncoder().encodeToString(url.getUserInfo().getBytes(UTF_8));
}
}

@Override
Expand All @@ -50,6 +61,9 @@ public HttpResponse execute(HttpRequest request, boolean followRedirects) throws
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setInstanceFollowRedirects(followRedirects);
if (auth != null) {
connection.setRequestProperty("Authorization", auth);
}
for (String name : request.getHeaderNames()) {
for (String value : request.getHeaders(name)) {
connection.addRequestProperty(name, value);
Expand Down

0 comments on commit c32c038

Please sign in to comment.