Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RESTEASY-2301] Accept header not correctly parsed by org.jboss.resteasy.client.jaxrs.cache.CacheInterceptor #2108

Merged
merged 2 commits into from Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -206,7 +206,17 @@ else if (exp != null)

byte[] cached = ReadFromStream.readFromStream(1024, response.getEntityStream());
// if Accept is present, use it, if not fallback to response Content-Type
MediaType mediaType = accept != null ? MediaType.valueOf(accept) : MediaType.valueOf(contentType);
MediaType mediaType = MediaType.valueOf(contentType);
if (accept != null) {
for (String acceptItem : accept.split("\\s*,\\s*")) {
MediaType acceptMediaType = MediaType.valueOf(acceptItem);
if (mediaType.isCompatible(acceptMediaType)) {
mediaType = acceptMediaType;
break;
}
}
}

final BrowserCache.Entry entry = cache.put(request.getUri().toString(), mediaType,
response.getHeaders(), cached, expires, etag, lastModified);

Expand Down
Expand Up @@ -231,4 +231,28 @@ public void testCachedValueWithWildCardAccept() throws Exception
}
}


@Test
// Reproduces RESTEASY-2301
public void testCachedValueWithMultipleAccept() throws Exception
{
BrowserCache cache = new LightweightBrowserCache();
CacheInterceptor interceptor = new CacheInterceptor(cache);
final String url = generateURL();
ClientInvocationBuilder requestA = (ClientInvocationBuilder) client.target(url).register(interceptor).path("echo")
.queryParam("msg", "Hello world").request();
try (ClientResponse responseA = (ClientResponse) requestA.accept(JSON_NO_CHARSET, XML_NO_CHARSET + ";q=0.5").get())
{
Assert.assertEquals(Status.OK.getStatusCode(), responseA.getStatus());
Assert.assertEquals("Content type must be " + JSON_WITH_CHARSET, JSON_WITH_CHARSET,
responseA.getHeaderString("Content-Type"));
Assert.assertNotNull("Cache must contain data", cache.getAny(requestA.getURI().toString()));

// the response must be cached as json
Assert.assertNotNull("Cache must contain data for the given accepted content type",
cache.get(requestA.getURI().toString(), MediaType.APPLICATION_JSON_TYPE));

}
}

}