From e671ecbb5bf557f604f28d64ab9754acbdea74ab Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 14 Sep 2021 18:40:00 +0300 Subject: [PATCH] Fix issue where RESTEasy Reactive kept incorrect media type state Fixes: #20142 --- .../test/mediatype/ContentTypeCaseTest.java | 63 +++++++++++++++++++ .../headers/MediaTypeHeaderDelegate.java | 7 ++- 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/mediatype/ContentTypeCaseTest.java diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/mediatype/ContentTypeCaseTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/mediatype/ContentTypeCaseTest.java new file mode 100644 index 00000000000000..7698788d50dc9c --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/mediatype/ContentTypeCaseTest.java @@ -0,0 +1,63 @@ +package io.quarkus.resteasy.reactive.server.test.mediatype; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.is; + +import java.util.function.Supplier; + +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +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.quarkus.test.QuarkusUnitTest; + +public class ContentTypeCaseTest { + + @RegisterExtension + static QuarkusUnitTest test = new QuarkusUnitTest() + .setArchiveProducer(new Supplier<>() { + @Override + public JavaArchive get() { + return ShrinkWrap.create(JavaArchive.class) + .addClasses(HelloResource.class); + } + }); + + @Test + public void test() { + given().header("test", "TeXt/Plain").get("/hello") + .then() + .statusCode(200) + .contentType("text/plain") + .body(is("text/plain")); + + given().header("test", "text/plain").get("/hello") + .then() + .statusCode(200) + .contentType("text/plain") + .body(is("text/plain")); + + given().header("test", "TEXT/PLAIN").get("/hello") + .then() + .statusCode(200) + .contentType("text/plain") + .body(is("text/plain")); + } + + @Path("hello") + public static class HelloResource { + + @GET + public Response hello(@HeaderParam("test") String contentType) { + MediaType mediaType = MediaType.valueOf(contentType); + return Response.ok(mediaType.toString()).header("content-type", mediaType).build(); + } + } +} diff --git a/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/MediaTypeHeaderDelegate.java b/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/MediaTypeHeaderDelegate.java index 9a450ad606f4ad..4cece10b57c7a8 100644 --- a/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/MediaTypeHeaderDelegate.java +++ b/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/MediaTypeHeaderDelegate.java @@ -1,6 +1,7 @@ package org.jboss.resteasy.reactive.common.headers; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import javax.ws.rs.core.MediaType; @@ -14,8 +15,8 @@ public class MediaTypeHeaderDelegate implements RuntimeDelegate.HeaderDelegate map = new ConcurrentHashMap(); - private static final Map reverseMap = new ConcurrentHashMap(); + private static final Map map = new ConcurrentHashMap<>(); + private static final Map reverseMap = new ConcurrentHashMap<>(); protected static boolean isValid(String str) { if (str == null || str.length() == 0) @@ -55,7 +56,7 @@ public static MediaType parse(String type) { reverseMap.clear(); } map.put(type, result); - reverseMap.put(result, type); + reverseMap.put(result, type.toLowerCase(Locale.ROOT)); } return result; }