From c2a55658c0ac8aa861fc8ba9711cc8c79a8d17a5 Mon Sep 17 00:00:00 2001 From: Sean Flanigan Date: Tue, 12 Apr 2016 14:29:23 +1000 Subject: [PATCH] Handle charset as part of Content-Type headers (#110) --- .../rest/client/InvalidContentTypeFilter.java | 2 +- .../client/InvalidContentTypeFilterTest.java | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/zanata-rest-client/src/main/java/org/zanata/rest/client/InvalidContentTypeFilter.java b/zanata-rest-client/src/main/java/org/zanata/rest/client/InvalidContentTypeFilter.java index aa6da70d..273e2ada 100644 --- a/zanata-rest-client/src/main/java/org/zanata/rest/client/InvalidContentTypeFilter.java +++ b/zanata-rest-client/src/main/java/org/zanata/rest/client/InvalidContentTypeFilter.java @@ -52,7 +52,7 @@ public class InvalidContentTypeFilter extends ClientFilter { // we assume only xml or json are the valid types (wildcard type is also considered compatible) private static final Pattern VALID_TYPES_REGEX = - Pattern.compile("application/.*\\+?(\\*|xml|json)"); + Pattern.compile("application/.*\\+?(\\*|xml|json)(;.*)?"); @Override public ClientResponse handle(ClientRequest clientRequest) diff --git a/zanata-rest-client/src/test/java/org/zanata/rest/client/InvalidContentTypeFilterTest.java b/zanata-rest-client/src/test/java/org/zanata/rest/client/InvalidContentTypeFilterTest.java index 63095d5d..4de1f1ab 100644 --- a/zanata-rest-client/src/test/java/org/zanata/rest/client/InvalidContentTypeFilterTest.java +++ b/zanata-rest-client/src/test/java/org/zanata/rest/client/InvalidContentTypeFilterTest.java @@ -9,10 +9,12 @@ import org.junit.Test; import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.*; import static org.zanata.rest.client.InvalidContentTypeFilter.isContentTypeCompatible; public class InvalidContentTypeFilterTest { + // WildFly 8 and 9 have been known to add ";charset=ISO-8859-1" to + // Content-Type headers. + private static final String LATIN_1 = "ISO-8859-1"; private String sampleText = "\n" + @@ -61,8 +63,23 @@ public void testValidateContentTypes() { MatcherAssert.assertThat( isContentTypeCompatible(MediaType.WILDCARD_TYPE), equalTo(true)); + + MediaType zanataXmlType = new MediaType( + "application", "vnd.zanata+xml"); + MatcherAssert.assertThat(isContentTypeCompatible(zanataXmlType), + equalTo(true)); + MatcherAssert.assertThat(isContentTypeCompatible( + zanataXmlType.withCharset(LATIN_1)), + equalTo(true)); + + MediaType zanataVersionXmlType = new MediaType( + "application", "vnd.zanata.version+xml"); + MatcherAssert.assertThat(isContentTypeCompatible(zanataVersionXmlType), + equalTo(true)); MatcherAssert.assertThat(isContentTypeCompatible( - new MediaType("application", "vnd.zanata+xml")), equalTo(true)); + zanataVersionXmlType.withCharset(LATIN_1)), + equalTo(true)); + MatcherAssert.assertThat( isContentTypeCompatible(MediaType.TEXT_PLAIN_TYPE), equalTo(true));