Skip to content

Commit

Permalink
fix: dropped proxy on redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
zekronium authored and vietj committed Mar 19, 2024
1 parent 07daa4e commit bb41cb5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/main/java/io/vertx/core/http/impl/HttpClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@ HttpClientRequest createRequest(HttpConnection connection, HttpClientStream stre
request.redirectHandler(resp -> {
Future<RequestOptions> 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;
}
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/io/vertx/core/http/HttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="mailto:[email protected]">Julien Viet</a>
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/vertx/test/proxy/HttpProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit bb41cb5

Please sign in to comment.