Skip to content

Commit

Permalink
Fix incorrect response code when media type is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jan 30, 2025
1 parent 0dc333e commit 2ea0e7e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
if (selectedHolder.mtWithoutParamsToResource.size() == 1) {
selectedResource = selectedHolder.mtWithoutParamsToResource.values().iterator().next();
} else {
MediaType produces = selectMediaType(requestContext, selectedHolder);
MediaType produces = null;
try {
produces = selectMediaType(requestContext, selectedHolder);
} catch (Exception e) {
throw new WebApplicationException(Response.status(Response.Status.NOT_ACCEPTABLE).build());
}
requestContext.setResponseContentType(produces);
MediaType key = produces;
if (!key.getParameters().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.jboss.resteasy.reactive.server.vertx.test.matching;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

import java.util.function.Supplier;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.RestPath;
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;

public class InvalidHeaderTest {

@RegisterExtension
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(TestResource.class);
}
});

@Test
void test() {
given()
.header("Accept", "application/json")
.when().get("/test/1")
.then()
.statusCode(200)
.body(is("{\"id\": \"1\"}"));

given()
.header("Accept", "text/plain")
.when().get("/test/1")
.then()
.statusCode(200)
.body(is("1"));

given()
.header("Accept", "foobar")
.when().get("/test/1")
.then()
.statusCode(406);
}

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

@GET
@Produces("*/*")
public String hello() {
return "Hello from Quarkus REST";
}

@GET
@Path("/{id}")
@Produces(MediaType.TEXT_PLAIN)
public String file(@RestPath String id) {
return id;
}

@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public String file2(@RestPath String id) {
return "{\"id\": \"" + id + "\"}";
}
}
}

0 comments on commit 2ea0e7e

Please sign in to comment.