RequestHeaderMethodArgumentResolverTests.java 8.6 KB
Newer Older
1
/*
2
 * Copyright 2002-2016 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
package org.springframework.web.method.annotation;
18 19

import java.lang.reflect.Method;
20 21 22
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Date;
23 24 25 26 27
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
28

29
import org.springframework.core.MethodParameter;
30
import org.springframework.core.annotation.SynthesizingMethodParameter;
31
import org.springframework.format.support.DefaultFormattingConversionService;
R
Rob Winch 已提交
32 33
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
34
import org.springframework.util.ReflectionUtils;
35
import org.springframework.web.bind.ServletRequestBindingException;
36
import org.springframework.web.bind.annotation.RequestHeader;
37 38
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.bind.support.DefaultDataBinderFactory;
39 40 41 42
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.support.GenericWebApplicationContext;
43 44

import static org.junit.Assert.*;
45 46

/**
J
Juergen Hoeller 已提交
47
 * Test fixture with {@link RequestHeaderMethodArgumentResolver}.
48
 *
49 50 51 52 53 54 55
 * @author Arjen Poutsma
 * @author Rossen Stoyanchev
 */
public class RequestHeaderMethodArgumentResolverTests {

	private RequestHeaderMethodArgumentResolver resolver;

R
Rossen Stoyanchev 已提交
56 57 58 59
	private MethodParameter paramNamedDefaultValueStringHeader;
	private MethodParameter paramNamedValueStringArray;
	private MethodParameter paramSystemProperty;
	private MethodParameter paramContextPath;
60 61
	private MethodParameter paramResolvedNameWithExpression;
	private MethodParameter paramResolvedNameWithPlaceholder;
R
Rossen Stoyanchev 已提交
62
	private MethodParameter paramNamedValueMap;
63 64
	private MethodParameter paramDate;
	private MethodParameter paramInstant;
65 66 67 68 69

	private MockHttpServletRequest servletRequest;

	private NativeWebRequest webRequest;

70

71
	@Before
S
Sam Brannen 已提交
72
	@SuppressWarnings("resource")
J
Juergen Hoeller 已提交
73
	public void setup() throws Exception {
74 75 76 77
		GenericWebApplicationContext context = new GenericWebApplicationContext();
		context.refresh();
		resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory());

78
		Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
79 80 81 82
		paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
		paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
		paramSystemProperty = new SynthesizingMethodParameter(method, 2);
		paramContextPath = new SynthesizingMethodParameter(method, 3);
83 84 85
		paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 4);
		paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 5);
		paramNamedValueMap = new SynthesizingMethodParameter(method, 6);
86 87
		paramDate = new SynthesizingMethodParameter(method, 7);
		paramInstant = new SynthesizingMethodParameter(method, 8);
88 89 90 91 92 93 94

		servletRequest = new MockHttpServletRequest();
		webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());

		// Expose request to the current thread (for SpEL expressions)
		RequestContextHolder.setRequestAttributes(webRequest);
	}
95

96
	@After
J
Juergen Hoeller 已提交
97
	public void reset() {
98 99 100
		RequestContextHolder.resetRequestAttributes();
	}

101

102 103
	@Test
	public void supportsParameter() {
R
Rossen Stoyanchev 已提交
104 105 106
		assertTrue("String parameter not supported", resolver.supportsParameter(paramNamedDefaultValueStringHeader));
		assertTrue("String array parameter not supported", resolver.supportsParameter(paramNamedValueStringArray));
		assertFalse("non-@RequestParam parameter supported", resolver.supportsParameter(paramNamedValueMap));
107 108 109 110 111 112 113
	}

	@Test
	public void resolveStringArgument() throws Exception {
		String expected = "foo";
		servletRequest.addHeader("name", expected);

R
Rossen Stoyanchev 已提交
114 115
		Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);
		assertTrue(result instanceof String);
116
		assertEquals(expected, result);
117 118 119 120
	}

	@Test
	public void resolveStringArrayArgument() throws Exception {
121
		String[] expected = new String[] {"foo", "bar"};
122 123
		servletRequest.addHeader("name", expected);

R
Rossen Stoyanchev 已提交
124 125
		Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
		assertTrue(result instanceof String[]);
126
		assertArrayEquals(expected, (String[]) result);
127 128 129 130
	}

	@Test
	public void resolveDefaultValue() throws Exception {
R
Rossen Stoyanchev 已提交
131 132
		Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);
		assertTrue(result instanceof String);
133
		assertEquals("bar", result);
134 135 136 137
	}

	@Test
	public void resolveDefaultValueFromSystemProperty() throws Exception {
R
Rossen Stoyanchev 已提交
138
		System.setProperty("systemProperty", "bar");
139 140 141 142 143 144 145 146 147
		try {
			Object result = resolver.resolveArgument(paramSystemProperty, null, webRequest, null);
			assertTrue(result instanceof String);
			assertEquals("bar", result);
		}
		finally {
			System.clearProperty("systemProperty");
		}
	}
R
Rossen Stoyanchev 已提交
148

149
	@Test
150
	public void resolveNameFromSystemPropertyThroughExpression() throws Exception {
151 152 153 154 155
		String expected = "foo";
		servletRequest.addHeader("bar", expected);

		System.setProperty("systemProperty", "bar");
		try {
156 157
			Object result = resolver.resolveArgument(paramResolvedNameWithExpression, null, webRequest, null);
			assertTrue(result instanceof String);
158
			assertEquals(expected, result);
159 160 161 162 163 164 165 166 167 168 169 170 171 172
		}
		finally {
			System.clearProperty("systemProperty");
		}
	}

	@Test
	public void resolveNameFromSystemPropertyThroughPlaceholder() throws Exception {
		String expected = "foo";
		servletRequest.addHeader("bar", expected);

		System.setProperty("systemProperty", "bar");
		try {
			Object result = resolver.resolveArgument(paramResolvedNameWithPlaceholder, null, webRequest, null);
173
			assertTrue(result instanceof String);
174
			assertEquals(expected, result);
175 176 177 178
		}
		finally {
			System.clearProperty("systemProperty");
		}
179 180 181 182 183
	}

	@Test
	public void resolveDefaultValueFromRequest() throws Exception {
		servletRequest.setContextPath("/bar");
R
Rossen Stoyanchev 已提交
184

185
		Object result = resolver.resolveArgument(paramContextPath, null, webRequest, null);
R
Rossen Stoyanchev 已提交
186
		assertTrue(result instanceof String);
187 188 189
		assertEquals("/bar", result);
	}

190
	@Test(expected = ServletRequestBindingException.class)
191
	public void notFound() throws Exception {
192
		resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
193 194
	}

195
	@Test
S
Sam Brannen 已提交
196
	@SuppressWarnings("deprecation")
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	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);
	}

224

225 226 227 228 229 230
	public void params(
			@RequestHeader(name = "name", defaultValue = "bar") String param1,
			@RequestHeader("name") String[] param2,
			@RequestHeader(name = "name", defaultValue="#{systemProperties.systemProperty}") String param3,
			@RequestHeader(name = "name", defaultValue="#{request.contextPath}") String param4,
			@RequestHeader("#{systemProperties.systemProperty}") String param5,
231
			@RequestHeader("${systemProperty}") String param6,
232 233 234
			@RequestHeader("name") Map<?, ?> unsupported,
			@RequestHeader("name") Date dateParam,
			@RequestHeader("name") Instant instantParam) {
235 236
	}

237
}