RequestHeaderMethodArgumentResolverTests.java 5.4 KB
Newer Older
1
/*
2
 * Copyright 2002-2015 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;
R
Rob Winch 已提交
27 28
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
29
import org.springframework.web.bind.ServletRequestBindingException;
30 31 32 33 34
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;
35 36

import static org.junit.Assert.*;
37 38

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

	private RequestHeaderMethodArgumentResolver resolver;

R
Rossen Stoyanchev 已提交
48 49 50 51 52
	private MethodParameter paramNamedDefaultValueStringHeader;
	private MethodParameter paramNamedValueStringArray;
	private MethodParameter paramSystemProperty;
	private MethodParameter paramContextPath;
	private MethodParameter paramNamedValueMap;
53 54 55 56 57 58 59 60 61 62 63 64

	private MockHttpServletRequest servletRequest;

	private NativeWebRequest webRequest;

	@Before
	public void setUp() throws Exception {
		GenericWebApplicationContext context = new GenericWebApplicationContext();
		context.refresh();
		resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory());

		Method method = getClass().getMethod("params", String.class, String[].class, String.class, String.class, Map.class);
R
Rossen Stoyanchev 已提交
65 66 67 68 69
		paramNamedDefaultValueStringHeader = new MethodParameter(method, 0);
		paramNamedValueStringArray = new MethodParameter(method, 1);
		paramSystemProperty = new MethodParameter(method, 2);
		paramContextPath = new MethodParameter(method, 3);
		paramNamedValueMap = new MethodParameter(method, 4);
70 71 72 73 74 75 76

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

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

78 79 80 81 82 83 84
	@After
	public void teardown() {
		RequestContextHolder.resetRequestAttributes();
	}

	@Test
	public void supportsParameter() {
R
Rossen Stoyanchev 已提交
85 86 87
		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));
88 89 90 91 92 93 94
	}

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

R
Rossen Stoyanchev 已提交
95 96 97
		Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);

		assertTrue(result instanceof String);
98 99 100 101 102 103 104 105
		assertEquals("Invalid result", expected, result);
	}

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

R
Rossen Stoyanchev 已提交
106 107 108 109
		Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);

		assertTrue(result instanceof String[]);
		assertArrayEquals("Invalid result", expected, (String[]) result);
110 111 112 113
	}

	@Test
	public void resolveDefaultValue() throws Exception {
R
Rossen Stoyanchev 已提交
114 115 116
		Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);

		assertTrue(result instanceof String);
117 118 119 120 121
		assertEquals("Invalid result", "bar", result);
	}

	@Test
	public void resolveDefaultValueFromSystemProperty() throws Exception {
R
Rossen Stoyanchev 已提交
122 123 124 125 126
		System.setProperty("systemProperty", "bar");
		Object result = resolver.resolveArgument(paramSystemProperty, null, webRequest, null);
		System.clearProperty("systemProperty");

		assertTrue(result instanceof String);
127 128 129 130 131 132
		assertEquals("bar", result);
	}

	@Test
	public void resolveDefaultValueFromRequest() throws Exception {
		servletRequest.setContextPath("/bar");
R
Rossen Stoyanchev 已提交
133 134 135
		Object result = resolver.resolveArgument(paramContextPath, null, webRequest, null);

		assertTrue(result instanceof String);
136 137 138
		assertEquals("/bar", result);
	}

139
	@Test(expected = ServletRequestBindingException.class)
140
	public void notFound() throws Exception {
141
		resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
142 143
	}

144
	public void params(@RequestHeader(name = "name", defaultValue = "bar") String param1,
145
					   @RequestHeader("name") String[] param2,
146 147
					   @RequestHeader(name = "name", defaultValue="#{systemProperties.systemProperty}") String param3,
					   @RequestHeader(name = "name", defaultValue="#{request.contextPath}") String param4,
148 149 150
					   @RequestHeader("name") Map<?, ?> unsupported) {
	}

R
Rossen Stoyanchev 已提交
151
}