Skip to content

Commit

Permalink
Use java.util.Locale to parse the languages from the Accept-Language …
Browse files Browse the repository at this point in the history
…header.

Updated the unit tests to check the header with case insensitivity.

This is a fix for https://issues.redhat.com/projects/RESTEASY/issues/RESTEASY-3387.
Signed-off-by:Nathan Erwin <nathan.d.erwin@gmail.com>
  • Loading branch information
nderwin authored and jamezp committed Mar 1, 2024
1 parent 9d19d1f commit 07c62c2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ public static Locale extractLocale(String lang) {
int q = lang.indexOf(';');
if (q > -1)
lang = lang.substring(0, q);
String[] split = lang.trim().split("-");
if (split.length == 1)
return new Locale(split[0].toLowerCase());
else if (split.length == 2)
return new Locale(split[0].toLowerCase(), split[1].toLowerCase());
else if (split.length > 2)
return new Locale(split[0], split[1], split[2]);
return null; // unreachable
return Locale.forLanguageTag(lang);
}

/**
Expand All @@ -29,9 +22,6 @@ else if (split.length > 2)
* @return converted language format string
*/
public static String toLanguageString(Locale value) {
StringBuffer buf = new StringBuffer(value.getLanguage().toLowerCase());
if (value.getCountry() != null && !value.getCountry().equals(""))
buf.append("-").append(value.getCountry().toLowerCase());
return buf.toString();
return value.toLanguageTag();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public void testLanguageOk() {
MultivaluedMap<String, Object> headers = response.getHeaders();

Assert.assertTrue("Content-Language header is not present in response", headers.keySet().contains("Content-Language"));
Assert.assertEquals("Content-Language header does not have expected value", "en-us",
headers.getFirst("Content-Language"));
Assert.assertTrue("Content-Language header does not have expected value",
"en-us".equalsIgnoreCase(headers.getFirst("Content-Language").toString()));
}

/**
Expand All @@ -91,8 +91,8 @@ public void testLanguageVariant() {
MultivaluedMap<String, Object> headers = response.getHeaders();

Assert.assertTrue("Content-Language header is not present in response", headers.keySet().contains("Content-Language"));
Assert.assertEquals("Content-Language header does not have expected value", "en-us",
headers.getFirst("Content-Language"));
Assert.assertTrue("Content-Language header does not have expected value",
"en-us".equalsIgnoreCase(headers.getFirst("Content-Language").toString()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void testGetComplexAcceptLanguageEn() throws Exception {
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals("GET", response.readEntity(String.class));
Assert.assertEquals(MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8"), response.getMediaType());
Assert.assertEquals("en-us", new LocaleDelegate().toString(response.getLanguage()));
Assert.assertTrue("en-us".equalsIgnoreCase(new LocaleDelegate().toString(response.getLanguage())));
response.close();
}

Expand All @@ -220,7 +220,7 @@ public void testGetComplexAcceptLanguageEnUs() throws Exception {
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals("GET", response.readEntity(String.class));
Assert.assertEquals(MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8"), response.getMediaType());
Assert.assertEquals("en-us", new LocaleDelegate().toString(response.getLanguage()));
Assert.assertTrue("en-us".equalsIgnoreCase(new LocaleDelegate().toString(response.getLanguage())));
response.close();
}

Expand All @@ -237,7 +237,7 @@ public void testGetComplexShuffleAcceptMedia() throws Exception {
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals("GET", response.readEntity(String.class));
Assert.assertEquals(MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8"), response.getMediaType());
Assert.assertEquals("en-us", new LocaleDelegate().toString(response.getLanguage()));
Assert.assertTrue("en-us".equalsIgnoreCase(new LocaleDelegate().toString(response.getLanguage())));
response.close();
}

Expand Down

0 comments on commit 07c62c2

Please sign in to comment.