Skip to content

Commit

Permalink
Fix issue where RESTEasy Reactive kept incorrect media type state
Browse files Browse the repository at this point in the history
Fixes: #20142
  • Loading branch information
geoand committed Sep 14, 2021
1 parent 7a1c973 commit e671ecb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,8 +15,8 @@ public class MediaTypeHeaderDelegate implements RuntimeDelegate.HeaderDelegate<M
public static final MediaTypeHeaderDelegate INSTANCE = new MediaTypeHeaderDelegate();
private static final int MAX_MT_CACHE_SIZE = 200;
private static final char[] quotedChars = "()<>@,;:\\\"/[]?= \t\r\n".toCharArray();
private static final Map<String, MediaType> map = new ConcurrentHashMap<String, MediaType>();
private static final Map<MediaType, String> reverseMap = new ConcurrentHashMap<MediaType, String>();
private static final Map<String, MediaType> map = new ConcurrentHashMap<>();
private static final Map<MediaType, String> reverseMap = new ConcurrentHashMap<>();

protected static boolean isValid(String str) {
if (str == null || str.length() == 0)
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit e671ecb

Please sign in to comment.