diff --git a/src/main/java/io/vertx/core/http/impl/HttpClientImpl.java b/src/main/java/io/vertx/core/http/impl/HttpClientImpl.java index 5ef7d7eecc9..a3e733fcd25 100644 --- a/src/main/java/io/vertx/core/http/impl/HttpClientImpl.java +++ b/src/main/java/io/vertx/core/http/impl/HttpClientImpl.java @@ -467,7 +467,10 @@ HttpClientRequest createRequest(HttpConnection connection, HttpClientStream stre request.redirectHandler(resp -> { Future fut_ = rHandler.apply(resp); if (fut_ != null) { - return fut_.compose(this::request); + return fut_.compose(o -> { + o.setProxyOptions(options.getProxyOptions()); + return this.request(o); + }); } else { return null; } diff --git a/src/test/java/io/vertx/core/http/HttpTest.java b/src/test/java/io/vertx/core/http/HttpTest.java index bad20197e50..3cf9b1b7e67 100644 --- a/src/test/java/io/vertx/core/http/HttpTest.java +++ b/src/test/java/io/vertx/core/http/HttpTest.java @@ -60,8 +60,10 @@ import java.util.function.*; import java.util.stream.IntStream; +import static io.vertx.core.http.HttpMethod.GET; import static io.vertx.core.http.HttpMethod.PUT; import static io.vertx.test.core.TestUtils.*; +import static org.hamcrest.CoreMatchers.instanceOf; /** * @author Julien Viet @@ -3726,6 +3728,55 @@ public void testFollowRedirectPutOn308() throws Exception { testFollowRedirect(HttpMethod.PUT, HttpMethod.PUT, 308, 308, 1, "http://" + DEFAULT_HTTP_HOST_AND_PORT + "/redirected", "http://" + DEFAULT_HTTP_HOST_AND_PORT + "/somepath"); } + + @Test + public void testFollowRedirectsWithProxy() throws Exception { + Assume.assumeThat("Proxy is only supported with HTTP/1", this, instanceOf(Http1xTest.class)); + waitFor(2); + String location = "http://" + DEFAULT_HTTP_HOST + ":" + DEFAULT_HTTP_PORT + "/ok"; + server.requestHandler(req -> { + if (!req.headers().contains("foo", "bar", true)) { + fail("Missing expected header"); + return; + } + assertEquals(Collections.singletonList("bar"), req.headers().getAll("foo")); + if (req.path().equals("/redirect")) { + req.response().setStatusCode(301).putHeader("Location", location).end(); + } else { + req.response().end(req.path()); + complete(); + } + }); + + startServer(); + startProxy(null, ProxyType.HTTP); + client.request( + new RequestOptions(requestOptions) + .setServer(null) + .setMethod(GET) + .setURI("/redirect") + .setProxyOptions(new ProxyOptions().setPort(proxy.port())) + ) + .compose(req -> req + .putHeader("foo", "bar") + .setFollowRedirects(true) + .send() + .compose(resp -> { + assertEquals(200, resp.statusCode()); + assertEquals(location, proxy.getLastUri()); + return resp.body().compose(body -> { + if (resp.statusCode() == 200) { + assertEquals(Buffer.buffer("/ok"), body); + } else { + assertEquals(Buffer.buffer(), body); + } + return Future.succeededFuture(); + }); + }) + ).onSuccess(v -> testComplete()); + await(); + } + private void testFollowRedirect( HttpMethod method, HttpMethod expectedMethod, diff --git a/src/test/java/io/vertx/test/proxy/HttpProxy.java b/src/test/java/io/vertx/test/proxy/HttpProxy.java index ae81c392cf6..533e74159d8 100644 --- a/src/test/java/io/vertx/test/proxy/HttpProxy.java +++ b/src/test/java/io/vertx/test/proxy/HttpProxy.java @@ -166,7 +166,7 @@ public HttpProxy start(Vertx vertx) throws Exception { } resp.body().onComplete(ar2 -> { if (ar2.succeeded()) { - request.response().end(ar2.result()); + request.response().setStatusCode(resp.statusCode()).end(ar2.result()); } else { request.response().setStatusCode(500).end(ar2.cause().toString() + " on client request"); }