SPR-8429 Add test cases for missing header, cookie, and pathvar values
This commit is contained in:
parent
465712c845
commit
69bba30ed8
|
|
@ -18,6 +18,7 @@ package org.springframework.web.method.annotation.support;
|
|||
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.web.bind.ServletRequestBindingException;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.CookieValue;
|
||||
|
||||
|
|
@ -57,10 +58,10 @@ public abstract class AbstractCookieValueMethodArgumentResolver extends Abstract
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void handleMissingValue(String cookieName, MethodParameter parameter) {
|
||||
String paramTypeName = parameter.getParameterType().getName();
|
||||
throw new IllegalStateException(
|
||||
"Missing cookie named '" + cookieName + "' for method parameter type [" + paramTypeName + "]");
|
||||
protected void handleMissingValue(String cookieName, MethodParameter param) throws ServletRequestBindingException {
|
||||
String paramType = param.getParameterType().getName();
|
||||
throw new ServletRequestBindingException(
|
||||
"Missing cookie named '" + cookieName + "' for method parameter type [" + paramType + "]");
|
||||
}
|
||||
|
||||
private static class CookieValueNamedValueInfo extends NamedValueInfo {
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@
|
|||
package org.springframework.web.method.annotation.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.ui.ModelMap;
|
||||
|
|
@ -50,9 +49,9 @@ public class ErrorsMethodArgumentResolver implements HandlerMethodArgumentResolv
|
|||
WebDataBinderFactory binderFactory) throws Exception {
|
||||
ModelMap model = mavContainer.getModel();
|
||||
if (model.size() > 0) {
|
||||
List<String> keys = new ArrayList<String>(model.keySet());
|
||||
String lastKey = keys.get(model.size()-1);
|
||||
if (isBindingResultKey(lastKey)) {
|
||||
int lastIndex = model.size()-1;
|
||||
String lastKey = new ArrayList<String>(model.keySet()).get(lastIndex);
|
||||
if (lastKey.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
|
||||
return model.get(lastKey);
|
||||
}
|
||||
}
|
||||
|
|
@ -61,7 +60,4 @@ public class ErrorsMethodArgumentResolver implements HandlerMethodArgumentResolv
|
|||
+ "without preceding model attribute. Check your handler method signature!");
|
||||
}
|
||||
|
||||
private boolean isBindingResultKey(String key) {
|
||||
return key.startsWith(BindingResult.MODEL_KEY_PREFIX);
|
||||
}
|
||||
}
|
||||
|
|
@ -20,6 +20,7 @@ import java.util.Map;
|
|||
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.web.bind.ServletRequestBindingException;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
|
|
@ -72,10 +73,10 @@ public class RequestHeaderMethodArgumentResolver extends AbstractNamedValueMetho
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void handleMissingValue(String headerName, MethodParameter parameter) {
|
||||
String paramTypeName = parameter.getParameterType().getName();
|
||||
throw new IllegalStateException(
|
||||
"Missing header '" + headerName + "' for method parameter type [" + paramTypeName + "]");
|
||||
protected void handleMissingValue(String headerName, MethodParameter param) throws ServletRequestBindingException {
|
||||
String paramType = param.getParameterType().getName();
|
||||
throw new ServletRequestBindingException(
|
||||
"Missing header '" + headerName + "' for method parameter type [" + paramType + "]");
|
||||
}
|
||||
|
||||
private static class RequestHeaderNamedValueInfo extends NamedValueInfo {
|
||||
|
|
|
|||
|
|
@ -165,7 +165,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
|||
* Attempt to resolve a method parameter from the list of provided argument values.
|
||||
*/
|
||||
private Object resolveProvidedArgument(MethodParameter parameter, Object... providedArgs) {
|
||||
if (providedArgs == null) {
|
||||
if (providedArgs == null || parameter.hasParameterAnnotations()) {
|
||||
return null;
|
||||
}
|
||||
for (Object providedArg : providedArgs) {
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package org.springframework.web.method.annotation.support;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
|
|
@ -29,6 +30,7 @@ import org.junit.Test;
|
|||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.web.bind.ServletRequestBindingException;
|
||||
import org.springframework.web.bind.annotation.CookieValue;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
|
|
@ -81,12 +83,10 @@ public class CookieValueMethodArgumentResolverTests {
|
|||
assertEquals("Invalid result", "bar", result);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
@Test(expected = ServletRequestBindingException.class)
|
||||
public void notFound() throws Exception {
|
||||
Object result = resolver.resolveArgument(paramNamedCookie, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof String);
|
||||
assertEquals("Invalid result", "bar", result);
|
||||
resolver.resolveArgument(paramNamedCookie, null, webRequest, null);
|
||||
fail("Expected exception");
|
||||
}
|
||||
|
||||
private static class TestCookieValueMethodArgumentResolver extends AbstractCookieValueMethodArgumentResolver {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import static org.junit.Assert.assertArrayEquals;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
|
@ -30,6 +31,7 @@ import org.junit.Test;
|
|||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.web.bind.ServletRequestBindingException;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
|
|
@ -137,12 +139,10 @@ public class RequestHeaderMethodArgumentResolverTests {
|
|||
assertEquals("/bar", result);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
@Test(expected = ServletRequestBindingException.class)
|
||||
public void notFound() throws Exception {
|
||||
Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof String);
|
||||
assertEquals("Invalid result", "bar", result);
|
||||
resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
|
||||
fail("Expected exception");
|
||||
}
|
||||
|
||||
public void params(@RequestHeader(value = "name", defaultValue = "bar") String param1,
|
||||
|
|
|
|||
Loading…
Reference in New Issue