@ -16,8 +16,11 @@
package org.springframework.boot.autoconfigure.web ;
import java.nio.charset.Charset ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Locale ;
import java.util.Map ;
import javax.servlet.Filter ;
@ -27,13 +30,17 @@ import org.junit.Test;
import org.junit.rules.ExpectedException ;
import org.springframework.beans.factory.NoSuchBeanDefinitionException ;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer ;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor ;
import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory ;
import org.springframework.boot.test.util.EnvironmentTestUtils ;
import org.springframework.boot.web.filter.OrderedHiddenHttpMethodFilter ;
import org.springframework.boot.web.filter.OrderedHttpPutFormContentFilter ;
import org.springframework.context.annotation.AnnotationConfigApplicationContext ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.core.annotation.AnnotationAwareOrderComparator ;
import org.springframework.mock.web.MockServletContext ;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext ;
import org.springframework.web.filter.CharacterEncodingFilter ;
import org.springframework.web.filter.HiddenHttpMethodFilter ;
@ -49,7 +56,7 @@ public class HttpEncodingAutoConfigurationTests {
@Rule
public final ExpectedException thrown = ExpectedException . none ( ) ;
private AnnotationConfig ApplicationContext context ;
private AnnotationConfig Web ApplicationContext context ;
@After
public void close ( ) {
@ -135,6 +142,31 @@ public class HttpEncodingAutoConfigurationTests {
assertThat ( beans . get ( 1 ) ) . isInstanceOf ( HiddenHttpMethodFilter . class ) ;
}
@Test
public void noLocaleCharsetMapping ( ) {
load ( EmptyConfiguration . class ) ;
Map < String , EmbeddedServletContainerCustomizer > beans =
this . context . getBeansOfType ( EmbeddedServletContainerCustomizer . class ) ;
assertThat ( beans . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( this . context . getBean ( MockEmbeddedServletContainerFactory . class )
. getLocaleCharsetMappings ( ) . size ( ) ) . isEqualTo ( 0 ) ;
}
@Test
public void customLocaleCharsetMappings ( ) {
load ( EmptyConfiguration . class , "spring.http.encoding.mapping.en:UTF-8" ,
"spring.http.encoding.mapping.fr_FR:UTF-8" ) ;
Map < String , EmbeddedServletContainerCustomizer > beans =
this . context . getBeansOfType ( EmbeddedServletContainerCustomizer . class ) ;
assertThat ( beans . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( this . context . getBean ( MockEmbeddedServletContainerFactory . class )
. getLocaleCharsetMappings ( ) . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( this . context . getBean ( MockEmbeddedServletContainerFactory . class )
. getLocaleCharsetMappings ( ) . get ( Locale . ENGLISH ) ) . isEqualTo ( Charset . forName ( "UTF-8" ) ) ;
assertThat ( this . context . getBean ( MockEmbeddedServletContainerFactory . class )
. getLocaleCharsetMappings ( ) . get ( Locale . FRANCE ) ) . isEqualTo ( Charset . forName ( "UTF-8" ) ) ;
}
private void assertCharacterEncodingFilter ( CharacterEncodingFilter actual ,
String encoding , boolean forceRequestEncoding ,
boolean forceResponseEncoding ) {
@ -147,12 +179,14 @@ public class HttpEncodingAutoConfigurationTests {
this . context = doLoad ( new Class < ? > [ ] { config } , environment ) ;
}
private AnnotationConfig ApplicationContext doLoad ( Class < ? > [ ] configs ,
private AnnotationConfig Web ApplicationContext doLoad ( Class < ? > [ ] configs ,
String . . . environment ) {
AnnotationConfig ApplicationContext applicationContext = new AnnotationConfig ApplicationContext( ) ;
AnnotationConfig Web ApplicationContext applicationContext = new AnnotationConfig Web ApplicationContext( ) ;
EnvironmentTestUtils . addEnvironment ( applicationContext , environment ) ;
applicationContext . register ( configs ) ;
applicationContext . register ( HttpEncodingAutoConfiguration . class ) ;
applicationContext . register ( MinimalWebAutoConfiguration . class ,
HttpEncodingAutoConfiguration . class ) ;
applicationContext . setServletContext ( new MockServletContext ( ) ) ;
applicationContext . refresh ( ) ;
return applicationContext ;
}
@ -190,4 +224,19 @@ public class HttpEncodingAutoConfigurationTests {
}
@Configuration
static class MinimalWebAutoConfiguration {
@Bean
public MockEmbeddedServletContainerFactory mockEmbeddedServletContainerFactory ( ) {
return new MockEmbeddedServletContainerFactory ( ) ;
}
@Bean
public EmbeddedServletContainerCustomizerBeanPostProcessor
embeddedServletContainerCustomizerBeanPostProcessor ( ) {
return new EmbeddedServletContainerCustomizerBeanPostProcessor ( ) ;
}
}
}