RequestHeaderMethodArgumentResolverTests.java 7.0 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 20 21 22 23 24

import java.lang.reflect.Method;
import java.util.Map;

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

26
import org.springframework.core.MethodParameter;
27
import org.springframework.core.annotation.SynthesizingMethodParameter;
R
Rob Winch 已提交
28 29
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
30
import org.springframework.util.ReflectionUtils;
31
import org.springframework.web.bind.ServletRequestBindingException;
32 33 34 35 36
import org.springframework.web.bind.annotation.RequestHeader;
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;
37 38

import static org.junit.Assert.*;
39 40

/**
41
 * Test fixture with {@link org.springframework.web.method.annotation.RequestHeaderMethodArgumentResolver}.
42
 *
43 44 45 46 47 48 49
 * @author Arjen Poutsma
 * @author Rossen Stoyanchev
 */
public class RequestHeaderMethodArgumentResolverTests {

	private RequestHeaderMethodArgumentResolver resolver;

R
Rossen Stoyanchev 已提交
50 51 52 53
	private MethodParameter paramNamedDefaultValueStringHeader;
	private MethodParameter paramNamedValueStringArray;
	private MethodParameter paramSystemProperty;
	private MethodParameter paramContextPath;
54 55
	private MethodParameter paramResolvedNameWithExpression;
	private MethodParameter paramResolvedNameWithPlaceholder;
R
Rossen Stoyanchev 已提交
56
	private MethodParameter paramNamedValueMap;
57 58 59 60 61

	private MockHttpServletRequest servletRequest;

	private NativeWebRequest webRequest;

62

63
	@Before
S
Sam Brannen 已提交
64
	@SuppressWarnings("resource")
65 66 67 68 69
	public void setUp() throws Exception {
		GenericWebApplicationContext context = new GenericWebApplicationContext();
		context.refresh();
		resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory());

70
		Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
71 72 73 74
		paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
		paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
		paramSystemProperty = new SynthesizingMethodParameter(method, 2);
		paramContextPath = new SynthesizingMethodParameter(method, 3);
75 76 77
		paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 4);
		paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 5);
		paramNamedValueMap = new SynthesizingMethodParameter(method, 6);
78 79 80 81 82 83 84

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

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

86 87 88 89 90
	@After
	public void teardown() {
		RequestContextHolder.resetRequestAttributes();
	}

91

92 93
	@Test
	public void supportsParameter() {
R
Rossen Stoyanchev 已提交
94 95 96
		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));
97 98 99 100 101 102 103
	}

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

R
Rossen Stoyanchev 已提交
104 105
		Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);
		assertTrue(result instanceof String);
106 107 108 109 110
		assertEquals("Invalid result", expected, result);
	}

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

R
Rossen Stoyanchev 已提交
114 115 116
		Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
		assertTrue(result instanceof String[]);
		assertArrayEquals("Invalid result", expected, (String[]) result);
117 118 119 120
	}

	@Test
	public void resolveDefaultValue() throws Exception {
R
Rossen Stoyanchev 已提交
121 122
		Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);
		assertTrue(result instanceof String);
123 124 125 126 127
		assertEquals("Invalid result", "bar", result);
	}

	@Test
	public void resolveDefaultValueFromSystemProperty() throws Exception {
R
Rossen Stoyanchev 已提交
128
		System.setProperty("systemProperty", "bar");
129 130 131 132 133 134 135 136 137
		try {
			Object result = resolver.resolveArgument(paramSystemProperty, null, webRequest, null);
			assertTrue(result instanceof String);
			assertEquals("bar", result);
		}
		finally {
			System.clearProperty("systemProperty");
		}
	}
R
Rossen Stoyanchev 已提交
138

139
	@Test
140
	public void resolveNameFromSystemPropertyThroughExpression() throws Exception {
141 142 143 144 145
		String expected = "foo";
		servletRequest.addHeader("bar", expected);

		System.setProperty("systemProperty", "bar");
		try {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
			Object result = resolver.resolveArgument(paramResolvedNameWithExpression, null, webRequest, null);
			assertTrue(result instanceof String);
			assertEquals("Invalid result", expected, result);
		}
		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);
163 164 165 166 167 168
			assertTrue(result instanceof String);
			assertEquals("Invalid result", expected, result);
		}
		finally {
			System.clearProperty("systemProperty");
		}
169 170 171 172 173
	}

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

175
		Object result = resolver.resolveArgument(paramContextPath, null, webRequest, null);
R
Rossen Stoyanchev 已提交
176
		assertTrue(result instanceof String);
177 178 179
		assertEquals("/bar", result);
	}

180
	@Test(expected = ServletRequestBindingException.class)
181
	public void notFound() throws Exception {
182
		resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
183 184
	}

185

186 187 188 189 190 191
	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,
192
			@RequestHeader("${systemProperty}") String param6,
193
			@RequestHeader("name") Map<?, ?> unsupported) {
194 195
	}

196
}