diff --git a/src/test/java/itest/util/Utils.java b/src/test/java/itest/util/Utils.java index c0a2daeea..9d202f841 100644 --- a/src/test/java/itest/util/Utils.java +++ b/src/test/java/itest/util/Utils.java @@ -15,14 +15,23 @@ */ package itest.util; +import java.net.URI; + import io.cryostat.core.sys.Environment; +import io.cryostat.util.HttpStatusCodeIdentifier; +import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpHeaders; +import io.vertx.core.Future; import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; import io.vertx.core.file.FileSystem; import io.vertx.core.http.HttpClient; import io.vertx.core.http.HttpClientOptions; +import io.vertx.core.http.HttpMethod; +import io.vertx.core.http.RequestOptions; +import io.vertx.core.http.impl.HttpUtils; import io.vertx.ext.web.client.WebClient; +import org.jboss.logging.Logger; public class Utils { @@ -50,7 +59,63 @@ public class Utils { private static final Vertx VERTX = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true)); - public static final HttpClient HTTP_CLIENT = VERTX.createHttpClient(HTTP_CLIENT_OPTIONS); + public static final HttpClient HTTP_CLIENT = + VERTX.createHttpClient(HTTP_CLIENT_OPTIONS) + .redirectHandler( + resp -> { + Logger logger = Logger.getLogger(WebClient.class); + // see io.vertx.core.http.impl.HttpClientImpl.DEFAULT_HANDLER + // lightly modified version of the default handler. Ignores handling + // of specific HTTP GET and HEAD methods and allows any verb to be + // redirected, and does not rewrite port numbers + try { + int statusCode = resp.statusCode(); + String location = resp.getHeader(HttpHeaders.LOCATION); + if (location == null + || !HttpStatusCodeIdentifier.isRedirectCode( + statusCode)) { + logger.infov( + "Ignoring non-redirect request: {0} {1}", + location, statusCode); + return null; + } + HttpMethod m = resp.request().getMethod(); + URI uri = + HttpUtils.resolveURIReference( + resp.request().absoluteURI(), location); + boolean ssl; + int port = uri.getPort(); + String protocol = uri.getScheme(); + char chend = protocol.charAt(protocol.length() - 1); + if (chend == 'p') { + ssl = false; + } else if (chend == 's') { + ssl = true; + } else { + return null; + } + String requestURI = uri.getPath(); + if (requestURI == null || requestURI.isEmpty()) { + requestURI = "/"; + } + String query = uri.getQuery(); + if (query != null) { + requestURI += "?" + query; + } + RequestOptions options = new RequestOptions(); + options.setMethod(m); + options.setHost(uri.getHost()); + options.setPort(port); + options.setSsl(ssl); + options.setURI(requestURI); + options.setHeaders(resp.request().headers()); + options.removeHeader(HttpHeaders.CONTENT_LENGTH); + logger.infov("Redirecting request to: {0}", options.toJson()); + return Future.succeededFuture(options); + } catch (Exception e) { + return Future.failedFuture(e); + } + }); private static final WebClient WEB_CLIENT_INSTANCE = WebClient.wrap(HTTP_CLIENT); public static WebClient getWebClient() {