|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
* Copyright 2002-2016 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -27,6 +27,7 @@ import org.springframework.core.MethodParameter; |
|
|
|
import org.springframework.core.annotation.SynthesizingMethodParameter; |
|
|
|
import org.springframework.core.annotation.SynthesizingMethodParameter; |
|
|
|
import org.springframework.mock.web.test.MockHttpServletRequest; |
|
|
|
import org.springframework.mock.web.test.MockHttpServletRequest; |
|
|
|
import org.springframework.mock.web.test.MockHttpServletResponse; |
|
|
|
import org.springframework.mock.web.test.MockHttpServletResponse; |
|
|
|
|
|
|
|
import org.springframework.util.ReflectionUtils; |
|
|
|
import org.springframework.web.bind.ServletRequestBindingException; |
|
|
|
import org.springframework.web.bind.ServletRequestBindingException; |
|
|
|
import org.springframework.web.bind.annotation.RequestHeader; |
|
|
|
import org.springframework.web.bind.annotation.RequestHeader; |
|
|
|
import org.springframework.web.context.request.NativeWebRequest; |
|
|
|
import org.springframework.web.context.request.NativeWebRequest; |
|
|
|
@ -50,6 +51,7 @@ public class RequestHeaderMethodArgumentResolverTests { |
|
|
|
private MethodParameter paramNamedValueStringArray; |
|
|
|
private MethodParameter paramNamedValueStringArray; |
|
|
|
private MethodParameter paramSystemProperty; |
|
|
|
private MethodParameter paramSystemProperty; |
|
|
|
private MethodParameter paramContextPath; |
|
|
|
private MethodParameter paramContextPath; |
|
|
|
|
|
|
|
private MethodParameter paramResolvedName; |
|
|
|
private MethodParameter paramNamedValueMap; |
|
|
|
private MethodParameter paramNamedValueMap; |
|
|
|
|
|
|
|
|
|
|
|
private MockHttpServletRequest servletRequest; |
|
|
|
private MockHttpServletRequest servletRequest; |
|
|
|
@ -64,12 +66,13 @@ public class RequestHeaderMethodArgumentResolverTests { |
|
|
|
context.refresh(); |
|
|
|
context.refresh(); |
|
|
|
resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory()); |
|
|
|
resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory()); |
|
|
|
|
|
|
|
|
|
|
|
Method method = getClass().getMethod("params", String.class, String[].class, String.class, String.class, Map.class); |
|
|
|
Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null); |
|
|
|
paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0); |
|
|
|
paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0); |
|
|
|
paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1); |
|
|
|
paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1); |
|
|
|
paramSystemProperty = new SynthesizingMethodParameter(method, 2); |
|
|
|
paramSystemProperty = new SynthesizingMethodParameter(method, 2); |
|
|
|
paramContextPath = new SynthesizingMethodParameter(method, 3); |
|
|
|
paramContextPath = new SynthesizingMethodParameter(method, 3); |
|
|
|
paramNamedValueMap = new SynthesizingMethodParameter(method, 4); |
|
|
|
paramResolvedName = new SynthesizingMethodParameter(method, 4); |
|
|
|
|
|
|
|
paramNamedValueMap = new SynthesizingMethodParameter(method, 5); |
|
|
|
|
|
|
|
|
|
|
|
servletRequest = new MockHttpServletRequest(); |
|
|
|
servletRequest = new MockHttpServletRequest(); |
|
|
|
webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse()); |
|
|
|
webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse()); |
|
|
|
@ -97,18 +100,16 @@ public class RequestHeaderMethodArgumentResolverTests { |
|
|
|
servletRequest.addHeader("name", expected); |
|
|
|
servletRequest.addHeader("name", expected); |
|
|
|
|
|
|
|
|
|
|
|
Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null); |
|
|
|
Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null); |
|
|
|
|
|
|
|
|
|
|
|
assertTrue(result instanceof String); |
|
|
|
assertTrue(result instanceof String); |
|
|
|
assertEquals("Invalid result", expected, result); |
|
|
|
assertEquals("Invalid result", expected, result); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void resolveStringArrayArgument() throws Exception { |
|
|
|
public void resolveStringArrayArgument() throws Exception { |
|
|
|
String[] expected = new String[]{"foo", "bar"}; |
|
|
|
String[] expected = new String[] {"foo", "bar"}; |
|
|
|
servletRequest.addHeader("name", expected); |
|
|
|
servletRequest.addHeader("name", expected); |
|
|
|
|
|
|
|
|
|
|
|
Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null); |
|
|
|
Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null); |
|
|
|
|
|
|
|
|
|
|
|
assertTrue(result instanceof String[]); |
|
|
|
assertTrue(result instanceof String[]); |
|
|
|
assertArrayEquals("Invalid result", expected, (String[]) result); |
|
|
|
assertArrayEquals("Invalid result", expected, (String[]) result); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -116,7 +117,6 @@ public class RequestHeaderMethodArgumentResolverTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void resolveDefaultValue() throws Exception { |
|
|
|
public void resolveDefaultValue() throws Exception { |
|
|
|
Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null); |
|
|
|
Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null); |
|
|
|
|
|
|
|
|
|
|
|
assertTrue(result instanceof String); |
|
|
|
assertTrue(result instanceof String); |
|
|
|
assertEquals("Invalid result", "bar", result); |
|
|
|
assertEquals("Invalid result", "bar", result); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -124,18 +124,37 @@ public class RequestHeaderMethodArgumentResolverTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void resolveDefaultValueFromSystemProperty() throws Exception { |
|
|
|
public void resolveDefaultValueFromSystemProperty() throws Exception { |
|
|
|
System.setProperty("systemProperty", "bar"); |
|
|
|
System.setProperty("systemProperty", "bar"); |
|
|
|
Object result = resolver.resolveArgument(paramSystemProperty, null, webRequest, null); |
|
|
|
try { |
|
|
|
System.clearProperty("systemProperty"); |
|
|
|
Object result = resolver.resolveArgument(paramSystemProperty, null, webRequest, null); |
|
|
|
|
|
|
|
assertTrue(result instanceof String); |
|
|
|
|
|
|
|
assertEquals("bar", result); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
finally { |
|
|
|
|
|
|
|
System.clearProperty("systemProperty"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
assertTrue(result instanceof String); |
|
|
|
@Test |
|
|
|
assertEquals("bar", result); |
|
|
|
public void resolveNameFromSystemProperty() throws Exception { |
|
|
|
|
|
|
|
String expected = "foo"; |
|
|
|
|
|
|
|
servletRequest.addHeader("bar", expected); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.setProperty("systemProperty", "bar"); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
Object result = resolver.resolveArgument(paramResolvedName, null, webRequest, null); |
|
|
|
|
|
|
|
assertTrue(result instanceof String); |
|
|
|
|
|
|
|
assertEquals("Invalid result", expected, result); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
finally { |
|
|
|
|
|
|
|
System.clearProperty("systemProperty"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void resolveDefaultValueFromRequest() throws Exception { |
|
|
|
public void resolveDefaultValueFromRequest() throws Exception { |
|
|
|
servletRequest.setContextPath("/bar"); |
|
|
|
servletRequest.setContextPath("/bar"); |
|
|
|
Object result = resolver.resolveArgument(paramContextPath, null, webRequest, null); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object result = resolver.resolveArgument(paramContextPath, null, webRequest, null); |
|
|
|
assertTrue(result instanceof String); |
|
|
|
assertTrue(result instanceof String); |
|
|
|
assertEquals("/bar", result); |
|
|
|
assertEquals("/bar", result); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -146,11 +165,13 @@ public class RequestHeaderMethodArgumentResolverTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void params(@RequestHeader(name = "name", defaultValue = "bar") String param1, |
|
|
|
public void params( |
|
|
|
@RequestHeader("name") String[] param2, |
|
|
|
@RequestHeader(name = "name", defaultValue = "bar") String param1, |
|
|
|
@RequestHeader(name = "name", defaultValue="#{systemProperties.systemProperty}") String param3, |
|
|
|
@RequestHeader("name") String[] param2, |
|
|
|
@RequestHeader(name = "name", defaultValue="#{request.contextPath}") String param4, |
|
|
|
@RequestHeader(name = "name", defaultValue="#{systemProperties.systemProperty}") String param3, |
|
|
|
@RequestHeader("name") Map<?, ?> unsupported) { |
|
|
|
@RequestHeader(name = "name", defaultValue="#{request.contextPath}") String param4, |
|
|
|
|
|
|
|
@RequestHeader("#{systemProperties.systemProperty}") String param5, |
|
|
|
|
|
|
|
@RequestHeader("name") Map<?, ?> unsupported) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|