-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes for ProxyRequest and ProxyResponse with null Body (#111)
See #110 In some cases, users want to skip the proxied request or the backend response entirely. To do so, they can set a null Body in ProxyRequest or ProxyResponse. However: - For ProxyRequest, it led to a NPE - For ProxyResponse, the content length was not overwritten if present Signed-off-by: Thomas Segismont <[email protected]>
- Loading branch information
1 parent
5fc5b87
commit de37cc1
Showing
3 changed files
with
94 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation | ||
* Copyright (c) 2011-2025 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
|
@@ -31,6 +31,8 @@ | |
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static io.vertx.core.http.HttpHeaders.CONTENT_LENGTH; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
|
@@ -115,6 +117,46 @@ public Future<Void> handleProxyResponse(ProxyContext context) { | |
}); | ||
} | ||
|
||
@Test | ||
public void testFilterNullBodies(TestContext ctx) { | ||
Async latch = ctx.async(3); | ||
SocketAddress backend = startHttpBackend(ctx, 8081, req -> { | ||
req.body().onComplete(ctx.asyncAssertSuccess(body -> { | ||
ctx.assertEquals(0, body.length()); | ||
ctx.assertEquals("0", req.getHeader(CONTENT_LENGTH)); | ||
req.response().end("IGNORED_BACKEND_RESPONSE_BODY"); | ||
})); | ||
}); | ||
startProxy(proxy -> proxy.origin(backend).addInterceptor(new ProxyInterceptor() { | ||
@Override | ||
public Future<ProxyResponse> handleProxyRequest(ProxyContext context) { | ||
context.request().setBody(null); | ||
Future<ProxyResponse> fut = context.sendRequest(); | ||
fut.onComplete(ctx.asyncAssertSuccess(v -> latch.countDown())); | ||
return fut; | ||
} | ||
|
||
@Override | ||
public Future<Void> handleProxyResponse(ProxyContext context) { | ||
context.response().setBody(null); | ||
Future<Void> fut = context.sendResponse(); | ||
fut.onComplete(ctx.asyncAssertSuccess(v -> latch.countDown())); | ||
return fut; | ||
} | ||
})); | ||
client = vertx.createHttpClient(); | ||
client | ||
.request(HttpMethod.POST, 8080, "localhost", "/") | ||
.compose(req -> req | ||
.send("IGNORED_CLIENT_REQUEST_BODY") | ||
.compose(resp -> resp.body().map(resp)) | ||
).onComplete(ctx.asyncAssertSuccess(resp -> { | ||
ctx.assertEquals(0, resp.body().result().length()); | ||
ctx.assertEquals("0", resp.getHeader(CONTENT_LENGTH)); | ||
latch.countDown(); | ||
})); | ||
} | ||
|
||
@Test | ||
public void testUpstreamRefuse(TestContext ctx) { | ||
SocketAddress backend = SocketAddress.inetSocketAddress(8081, "localhost"); | ||
|