AcceptHeaderLocaleResolver returns default locale in case of no supported locale found

Issue: SPR-15426
This commit is contained in:
Juergen Hoeller
2017-04-10 15:36:13 +02:00
parent 9b85c5b636
commit ea98ee820a
2 changed files with 28 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.i18n;
import java.util.Arrays;
@@ -24,15 +25,14 @@ import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest;
import static java.util.Locale.CANADA;
import static java.util.Locale.JAPANESE;
import static java.util.Locale.UK;
import static java.util.Locale.US;
import static org.junit.Assert.assertEquals;
import static java.util.Locale.*;
import static org.junit.Assert.*;
/**
* Unit tests for {@link AcceptHeaderLocaleResolver}.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
*/
public class AcceptHeaderLocaleResolverTests {
@@ -56,6 +56,17 @@ public class AcceptHeaderLocaleResolverTests {
this.resolver.setSupportedLocales(Collections.singletonList(CANADA));
assertEquals(US, this.resolver.resolveLocale(request(US, UK)));
}
@Test
public void resolvePreferredNotSupportedWithDefault() {
this.resolver.setSupportedLocales(Arrays.asList(US, JAPAN));
this.resolver.setDefaultLocale(Locale.JAPAN);
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Accept-Language", KOREA.toString());
request.setPreferredLocales(Collections.singletonList(KOREA));
assertEquals(Locale.JAPAN, this.resolver.resolveLocale(request));
}
@Test
public void defaultLocale() throws Exception {