Skip to content

Commit

Permalink
Make sure Transfer-Encoding set by a Resource is honored
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Sep 24, 2024
1 parent 2723368 commit 7005ab9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.restassured.RestAssured.when;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;

import java.io.IOException;

Expand All @@ -11,13 +12,15 @@
import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.vertx.web.RouteFilter;
import io.restassured.http.Headers;
import io.vertx.ext.web.RoutingContext;

public class VertxHeadersTest {
Expand All @@ -35,6 +38,16 @@ void testVaryHeaderValues() {
assertThat(headers.getValues(HttpHeaders.VARY)).containsExactlyInAnyOrder("Origin", "Prefer");
}

@Test
void testTransferEncodingHeaderValues() {
Headers headers = when().get("/test/response")
.then()
.statusCode(200)
.header("Transfer-Encoding", is("chunked")).extract().headers();

assertThat(headers.asList()).noneMatch(h -> h.getName().equals("transfer-encoding"));
}

public static class VertxFilter {
@RouteFilter
void addVary(final RoutingContext rc) {
Expand All @@ -59,5 +72,52 @@ public static class TestResource {
public String test() {
return "test";
}

@GET
@Path("response")
public Response response() {
final String text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
TestDTO testDTO = new TestDTO();
for (int i = 0; i < 100; i++) {
testDTO.setText(testDTO.getText() + text);
testDTO.setMoreText(testDTO.getMoreText() + text);
testDTO.setEvenMoreText(testDTO.getEvenMoreText() + text);
}

//Simulate getting a chunked response from external microservice
Response response = Response.ok().entity("test").header("Transfer-Encoding", "chunked").build();
//Forwarding the response
return Response.fromResponse(response).build();
}
}

public static class TestDTO {
public String text = "";
public String moreText = "";
public String evenMoreText = "";

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public String getMoreText() {
return moreText;
}

public void setMoreText(String moreText) {
this.moreText = moreText;
}

public String getEvenMoreText() {
return evenMoreText;
}

public void setEvenMoreText(String evenMoreText) {
this.evenMoreText = evenMoreText;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void accept(ResteasyReactiveRequestContext context) {
private static final String LENGTH = "Length";
private static final String LENGTH_LOWER = "length";
private static final String CONTENT_TYPE = CONTENT + "-" + TYPE; // use this instead of the Vert.x constant because the TCK expects upper case
private static final String TRANSFER_ENCODING = "Transfer-Encoding";

public final static List<Serialisers.BuiltinReader> BUILTIN_READERS = List.of(
new Serialisers.BuiltinReader(String.class, ServerStringMessageBodyHandler.class,
Expand Down Expand Up @@ -519,7 +520,7 @@ public static void encodeResponseHeaders(ResteasyReactiveRequestContext requestC
vertxResponse.addResponseHeader(header, (CharSequence) HeaderUtil.headerToString(o));
}
}
if (header.equalsIgnoreCase("Transfer-Encoding")) { // using both headers together is not allowed
if (header.equalsIgnoreCase(TRANSFER_ENCODING)) { // using both headers together is not allowed
vertxResponse.removeResponseHeader("Content-Length");
}
} else {
Expand All @@ -533,7 +534,8 @@ public static void encodeResponseHeaders(ResteasyReactiveRequestContext requestC
}

private static boolean requireSingleHeader(String header) {
if (!(header.startsWith(CONTENT) || header.startsWith(CONTENT_LOWER) || header.startsWith(LOCATION))) {
if (!(header.startsWith(CONTENT) || header.startsWith(CONTENT_LOWER) || header.startsWith(LOCATION)
|| header.equalsIgnoreCase(TRANSFER_ENCODING))) {
return false;
}
if (header.length() < CONTENT.length() + 2) {
Expand Down

0 comments on commit 7005ab9

Please sign in to comment.