-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use proper media type when executing writer interceptors
The one from the response can be null if we return a partial response but the type is defined by @produces. We need to consider the one from the context. Fixes #34839
- Loading branch information
Showing
3 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...kus/resteasy/reactive/server/test/providers/WriterInterceptorContentTypeOverrideTest.java
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.quarkus.resteasy.reactive.server.test.providers; | ||
|
||
import java.io.IOException; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.Provider; | ||
import jakarta.ws.rs.ext.WriterInterceptorContext; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class WriterInterceptorContentTypeOverrideTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest().withApplicationRoot( | ||
jar -> jar.addClasses(MediaTypeResponseInterceptor.class, TestResource.class)); | ||
|
||
@Test | ||
public void producesMediaTypePresentInWriterInterceptor() { | ||
RestAssured.when().get("/test").then().body(Matchers.containsString("Hello")); | ||
} | ||
|
||
@Path("/test") | ||
public static class TestResource { | ||
|
||
@GET | ||
@Produces(MediaType.TEXT_XML) | ||
public Response hello() { | ||
Greeting greeting = new Greeting("Hello"); | ||
return Response.ok(greeting).type(MediaType.TEXT_PLAIN).build(); | ||
} | ||
} | ||
|
||
public record Greeting(String message) { | ||
} | ||
|
||
@Priority(5000) | ||
@Provider | ||
public static class MediaTypeResponseInterceptor implements jakarta.ws.rs.ext.WriterInterceptor { | ||
|
||
@Override | ||
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { | ||
if (!context.getMediaType().isCompatible(MediaType.TEXT_PLAIN_TYPE)) { | ||
throw new IllegalStateException("MediaType was not overridden by Response, got: " + context.getMediaType() | ||
+ " instead of expected: " + MediaType.TEXT_PLAIN); | ||
} | ||
context.proceed(); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
.../io/quarkus/resteasy/reactive/server/test/providers/WriterInterceptorContentTypeTest.java
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.quarkus.resteasy.reactive.server.test.providers; | ||
|
||
import java.io.IOException; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.Provider; | ||
import jakarta.ws.rs.ext.WriterInterceptorContext; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class WriterInterceptorContentTypeTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest().withApplicationRoot( | ||
jar -> jar.addClasses(MediaTypeResponseInterceptor.class, TestResource.class)); | ||
|
||
@Test | ||
public void producesMediaTypePresentInWriterInterceptor() { | ||
RestAssured.when().get("/test").then().body(Matchers.containsString("Hello")); | ||
} | ||
|
||
@Path("/test") | ||
public static class TestResource { | ||
|
||
@GET | ||
@Produces(MediaType.TEXT_XML) | ||
public Response hello() { | ||
Greeting greeting = new Greeting("Hello"); | ||
return Response.ok(greeting).build(); | ||
} | ||
} | ||
|
||
public record Greeting(String message) { | ||
} | ||
|
||
@Priority(5000) | ||
@Provider | ||
public static class MediaTypeResponseInterceptor implements jakarta.ws.rs.ext.WriterInterceptor { | ||
|
||
@Override | ||
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { | ||
if (!context.getMediaType().isCompatible(MediaType.TEXT_XML_TYPE)) { | ||
throw new IllegalStateException("MediaType was not provided, got: " + context.getMediaType() | ||
+ " instead of expected: " + MediaType.TEXT_XML); | ||
} | ||
context.proceed(); | ||
} | ||
} | ||
} |
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