-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that MessageBodyWriter is passed the proper media type
Fixes: #41354
- Loading branch information
Showing
3 changed files
with
102 additions
and
3 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
93 changes: 93 additions & 0 deletions
93
...a/org/jboss/resteasy/reactive/server/vertx/test/matching/StringMessageBodyWriterTest.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,93 @@ | ||
package org.jboss.resteasy.reactive.server.vertx.test.matching; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.util.function.Supplier; | ||
|
||
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.MultivaluedMap; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.MessageBodyWriter; | ||
import jakarta.ws.rs.ext.Provider; | ||
|
||
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.restassured.http.Header; | ||
|
||
public class StringMessageBodyWriterTest { | ||
|
||
@RegisterExtension | ||
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class); | ||
} | ||
}); | ||
|
||
@Test | ||
void testHelloEndpoint() { | ||
given() | ||
.when().get("/hello") | ||
.then() | ||
.statusCode(200) | ||
.body(is("Greeting response: Hello from Quarkus REST")); | ||
} | ||
|
||
@Test | ||
void testWithNoAcceptHeader() { | ||
// Prevent RestAssured from setting any Accept header | ||
final var header = new Header("Accept", null); | ||
|
||
given() | ||
.when() | ||
.header(header) | ||
.get("/hello") | ||
.then() | ||
.statusCode(200) | ||
.body(is("Greeting response: Hello from Quarkus REST")); | ||
} | ||
|
||
@Path("/hello") | ||
public static class GreetingResource { | ||
|
||
@GET | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public Response hello() { | ||
return Response.ok("Hello from Quarkus REST").build(); | ||
} | ||
} | ||
|
||
@Provider | ||
public static class GreetingMessageBodyWriter implements MessageBodyWriter<String> { | ||
|
||
@Override | ||
public boolean isWriteable(final Class<?> aClass, final Type type, final Annotation[] annotations, | ||
final MediaType mediaType) { | ||
return String.class.isAssignableFrom(aClass) && MediaType.TEXT_PLAIN_TYPE.isCompatible(mediaType); | ||
} | ||
|
||
@Override | ||
public void writeTo(final String s, final Class<?> aClass, final Type type, final Annotation[] annotations, | ||
final MediaType mediaType, | ||
final MultivaluedMap<String, Object> multivaluedMap, final OutputStream outputStream) | ||
throws IOException, WebApplicationException { | ||
|
||
final var content = "Greeting response: " + s; | ||
outputStream.write(content.getBytes()); | ||
} | ||
} | ||
} |