Skip to content

Commit

Permalink
Merge pull request #44259 from koplas/header-language-fix
Browse files Browse the repository at this point in the history
Fix HttpHeaders.getAcceptableLanguages() on empty headers
  • Loading branch information
geoand authored Nov 6, 2024
2 parents 3742ca0 + c55d0be commit 7ec531d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public class HeaderUtil {
private static final List<Locale> LANGUAGE_WILDCARD = List.of(new Locale("*"));

private static final ClassValue<RuntimeDelegate.HeaderDelegate<?>> HEADER_DELEGATE_CACHE = new ClassValue<>() {
@Override
Expand Down Expand Up @@ -293,7 +294,7 @@ public static List<MediaType> getAcceptableMediaTypes(MultivaluedMap<String, ? e
public static List<Locale> getAcceptableLanguages(MultivaluedMap<String, ? extends Object> headers) {
List<?> accepts = headers.get(HttpHeaders.ACCEPT_LANGUAGE);
if (accepts == null || accepts.isEmpty())
return Collections.emptyList();
return LANGUAGE_WILDCARD;
List<WeightedLanguage> languages = new ArrayList<WeightedLanguage>();
for (Object obj : accepts) {
if (obj instanceof Locale) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jboss.resteasy.reactive.common.headers;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Locale;

import org.jboss.resteasy.reactive.common.util.MultivaluedTreeMap;
import org.junit.jupiter.api.Test;

class HeaderUtilTest {

@Test
void getAcceptableLanguages() {
MultivaluedTreeMap<String, String> emptyHeaders = new MultivaluedTreeMap<>();
Locale[] wildcardLocale = new Locale[] { new Locale("*") };
assertArrayEquals(wildcardLocale, HeaderUtil.getAcceptableLanguages(emptyHeaders).toArray());

MultivaluedTreeMap<String, String> singleLanguage = new MultivaluedTreeMap<>();
singleLanguage.add("Accept-Language", "de");
Locale[] singleLocale = new Locale[] { Locale.GERMAN };
assertArrayEquals(singleLocale, HeaderUtil.getAcceptableLanguages(singleLanguage).toArray());

MultivaluedTreeMap<String, String> multipleWeightedLanguages = new MultivaluedTreeMap<>();
multipleWeightedLanguages.add("Accept-Language", "da, en-gb;q=0.8, en;q=0.7");
Locale[] multipleWeightedLocales = new Locale[] { new Locale("da"), Locale.UK, Locale.ENGLISH };
assertArrayEquals(multipleWeightedLocales, HeaderUtil.getAcceptableLanguages(multipleWeightedLanguages).toArray());
}
}

0 comments on commit 7ec531d

Please sign in to comment.