@ -17,6 +17,9 @@
@@ -17,6 +17,9 @@
package org.springframework.web.method.annotation ;
import java.lang.reflect.Method ;
import java.time.Instant ;
import java.time.format.DateTimeFormatter ;
import java.util.Date ;
import java.util.Map ;
import org.junit.After ;
@ -25,11 +28,14 @@ import org.junit.Test;
@@ -25,11 +28,14 @@ import org.junit.Test;
import org.springframework.core.MethodParameter ;
import org.springframework.core.annotation.SynthesizingMethodParameter ;
import org.springframework.format.support.DefaultFormattingConversionService ;
import org.springframework.mock.web.test.MockHttpServletRequest ;
import org.springframework.mock.web.test.MockHttpServletResponse ;
import org.springframework.util.ReflectionUtils ;
import org.springframework.web.bind.ServletRequestBindingException ;
import org.springframework.web.bind.annotation.RequestHeader ;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer ;
import org.springframework.web.bind.support.DefaultDataBinderFactory ;
import org.springframework.web.context.request.NativeWebRequest ;
import org.springframework.web.context.request.RequestContextHolder ;
import org.springframework.web.context.request.ServletWebRequest ;
@ -54,6 +60,8 @@ public class RequestHeaderMethodArgumentResolverTests {
@@ -54,6 +60,8 @@ public class RequestHeaderMethodArgumentResolverTests {
private MethodParameter paramResolvedNameWithExpression ;
private MethodParameter paramResolvedNameWithPlaceholder ;
private MethodParameter paramNamedValueMap ;
private MethodParameter paramDate ;
private MethodParameter paramInstant ;
private MockHttpServletRequest servletRequest ;
@ -75,6 +83,8 @@ public class RequestHeaderMethodArgumentResolverTests {
@@ -75,6 +83,8 @@ public class RequestHeaderMethodArgumentResolverTests {
paramResolvedNameWithExpression = new SynthesizingMethodParameter ( method , 4 ) ;
paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter ( method , 5 ) ;
paramNamedValueMap = new SynthesizingMethodParameter ( method , 6 ) ;
paramDate = new SynthesizingMethodParameter ( method , 7 ) ;
paramInstant = new SynthesizingMethodParameter ( method , 8 ) ;
servletRequest = new MockHttpServletRequest ( ) ;
webRequest = new ServletWebRequest ( servletRequest , new MockHttpServletResponse ( ) ) ;
@ -103,7 +113,7 @@ public class RequestHeaderMethodArgumentResolverTests {
@@ -103,7 +113,7 @@ public class RequestHeaderMethodArgumentResolverTests {
Object result = resolver . resolveArgument ( paramNamedDefaultValueStringHeader , null , webRequest , null ) ;
assertTrue ( result instanceof String ) ;
assertEquals ( "Invalid result" , expected , result ) ;
assertEquals ( expected , result ) ;
}
@Test
@ -113,14 +123,14 @@ public class RequestHeaderMethodArgumentResolverTests {
@@ -113,14 +123,14 @@ public class RequestHeaderMethodArgumentResolverTests {
Object result = resolver . resolveArgument ( paramNamedValueStringArray , null , webRequest , null ) ;
assertTrue ( result instanceof String [ ] ) ;
assertArrayEquals ( "Invalid result" , expected , ( String [ ] ) result ) ;
assertArrayEquals ( expected , ( String [ ] ) result ) ;
}
@Test
public void resolveDefaultValue ( ) throws Exception {
Object result = resolver . resolveArgument ( paramNamedDefaultValueStringHeader , null , webRequest , null ) ;
assertTrue ( result instanceof String ) ;
assertEquals ( "Invalid result" , " bar" , result ) ;
assertEquals ( "bar" , result ) ;
}
@Test
@ -145,7 +155,7 @@ public class RequestHeaderMethodArgumentResolverTests {
@@ -145,7 +155,7 @@ public class RequestHeaderMethodArgumentResolverTests {
try {
Object result = resolver . resolveArgument ( paramResolvedNameWithExpression , null , webRequest , null ) ;
assertTrue ( result instanceof String ) ;
assertEquals ( "Invalid result" , expected , result ) ;
assertEquals ( expected , result ) ;
}
finally {
System . clearProperty ( "systemProperty" ) ;
@ -161,7 +171,7 @@ public class RequestHeaderMethodArgumentResolverTests {
@@ -161,7 +171,7 @@ public class RequestHeaderMethodArgumentResolverTests {
try {
Object result = resolver . resolveArgument ( paramResolvedNameWithPlaceholder , null , webRequest , null ) ;
assertTrue ( result instanceof String ) ;
assertEquals ( "Invalid result" , expected , result ) ;
assertEquals ( expected , result ) ;
}
finally {
System . clearProperty ( "systemProperty" ) ;
@ -182,6 +192,34 @@ public class RequestHeaderMethodArgumentResolverTests {
@@ -182,6 +192,34 @@ public class RequestHeaderMethodArgumentResolverTests {
resolver . resolveArgument ( paramNamedValueStringArray , null , webRequest , null ) ;
}
@Test
public void dateConversion ( ) throws Exception {
String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100" ;
servletRequest . addHeader ( "name" , rfc1123val ) ;
ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer ( ) ;
bindingInitializer . setConversionService ( new DefaultFormattingConversionService ( ) ) ;
Object result = resolver . resolveArgument ( paramDate , null , webRequest ,
new DefaultDataBinderFactory ( bindingInitializer ) ) ;
assertTrue ( result instanceof Date ) ;
assertEquals ( new Date ( rfc1123val ) , result ) ;
}
@Test
public void instantConversion ( ) throws Exception {
String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100" ;
servletRequest . addHeader ( "name" , rfc1123val ) ;
ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer ( ) ;
bindingInitializer . setConversionService ( new DefaultFormattingConversionService ( ) ) ;
Object result = resolver . resolveArgument ( paramInstant , null , webRequest ,
new DefaultDataBinderFactory ( bindingInitializer ) ) ;
assertTrue ( result instanceof Instant ) ;
assertEquals ( Instant . from ( DateTimeFormatter . RFC_1123_DATE_TIME . parse ( rfc1123val ) ) , result ) ;
}
public void params (
@RequestHeader ( name = "name" , defaultValue = "bar" ) String param1 ,
@ -190,7 +228,9 @@ public class RequestHeaderMethodArgumentResolverTests {
@@ -190,7 +228,9 @@ public class RequestHeaderMethodArgumentResolverTests {
@RequestHeader ( name = "name" , defaultValue = "#{request.contextPath}" ) String param4 ,
@RequestHeader ( "#{systemProperties.systemProperty}" ) String param5 ,
@RequestHeader ( "${systemProperty}" ) String param6 ,
@RequestHeader ( "name" ) Map < ? , ? > unsupported ) {
@RequestHeader ( "name" ) Map < ? , ? > unsupported ,
@RequestHeader ( "name" ) Date dateParam ,
@RequestHeader ( "name" ) Instant instantParam ) {
}
}