Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate HTTP headers when WriterInterceptor is used #27406

Merged
merged 1 commit into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public WriterInterceptorContextImpl(ResteasyReactiveRequestContext context, Writ
@Override
public void proceed() throws IOException, WebApplicationException {
Response response = context.getResponse().get();
// this is needed in order to avoid having the headers written out twice
context.serverResponse().setPreCommitListener(null);
if (index == interceptors.length) {
MessageBodyWriter effectiveWriter = writer;
if (rediscoveryNeeded) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.jboss.resteasy.reactive.server.vertx.test.response;

import static io.restassured.RestAssured.when;
import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.Matchers.is;

import io.restassured.http.Headers;
import java.io.IOException;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class CustomHeadersAndWriterInterceptorTest {

@RegisterExtension
static ResteasyReactiveUnitTest runner = new ResteasyReactiveUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(TestResource.class, DummyWriterInterceptor.class));

@Test
void testResponseHeaders() {
Headers headers = when()
.get("/test")
.then()
.statusCode(200)
.header("etag", is("0"))
.extract().headers();
assertThat(headers.getList("etag")).hasSize(1);
assertThat(headers.getList("Last-Modified")).hasSize(1);
}

@Path("/test")
public static class TestResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public Response hello() {
return Response.ok("123").lastModified(new Date()).header("etag", 0).build();
}
}

@Provider
public static class DummyWriterInterceptor implements WriterInterceptor {

@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
context.proceed();
}
}

}