Skip to content

Commit

Permalink
Merge pull request #20149 from geoand/#20142
Browse files Browse the repository at this point in the history
Fix issue where RESTEasy Reactive kept incorrect media type state
  • Loading branch information
geoand authored Sep 14, 2021
2 parents 16d7047 + e9efd72 commit 1261ed0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 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
Expand Up @@ -14,8 +14,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 +55,7 @@ public static MediaType parse(String type) {
reverseMap.clear();
}
map.put(type, result);
reverseMap.put(result, type);
reverseMap.put(result, internalToString(result));
}
return result;
}
Expand Down Expand Up @@ -141,10 +141,10 @@ public String toString(MediaType o) {
return result;
}

private String internalToString(MediaType type) {
StringBuilder buf = new StringBuilder();
private static String internalToString(MediaType type) {
StringBuilder buf = new StringBuilder(type.getType().length() + type.getSubtype().length() + 1);

buf.append(type.getType().toLowerCase()).append("/").append(type.getSubtype().toLowerCase());
buf.append(type.getType().toLowerCase()).append('/').append(type.getSubtype().toLowerCase());
if (type.getParameters() == null || type.getParameters().size() == 0)
return buf.toString();
for (String name : type.getParameters().keySet()) {
Expand Down

0 comments on commit 1261ed0

Please sign in to comment.