diff options
author | Emmanuel Bourg <ebourg@apache.org> | 2017-07-24 19:29:45 +0200 |
---|---|---|
committer | Emmanuel Bourg <ebourg@apache.org> | 2017-07-24 19:29:45 +0200 |
commit | 2062ec42e5d3b880b5120118adcbb279204d7353 (patch) | |
tree | b69fd57fae5b1ab576d56ef82bdada1fb057b94e /spring-web/src/test/java/org/springframework/web | |
parent | 16cac1f52d8e8159e0e688b2fda3f89291cec969 (diff) |
New upstream version 4.3.10
Diffstat (limited to 'spring-web/src/test/java/org/springframework/web')
5 files changed, 147 insertions, 14 deletions
diff --git a/spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerTests.java b/spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerTests.java index 89c5cd24..0d805d78 100644 --- a/spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerTests.java +++ b/spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerTests.java @@ -328,7 +328,7 @@ public class WebAsyncManagerTests { Exception exception = new Exception(); DeferredResultProcessingInterceptor interceptor = mock(DeferredResultProcessingInterceptor.class); - willThrow(exception).given(interceptor).postProcess(this.asyncWebRequest, deferredResult, 25);; + willThrow(exception).given(interceptor).postProcess(this.asyncWebRequest, deferredResult, 25); setupDefaultAsyncScenario(); diff --git a/spring-web/src/test/java/org/springframework/web/filter/ForwardedHeaderFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/ForwardedHeaderFilterTests.java index 15aff6ff..4e20defb 100644 --- a/spring-web/src/test/java/org/springframework/web/filter/ForwardedHeaderFilterTests.java +++ b/spring-web/src/test/java/org/springframework/web/filter/ForwardedHeaderFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.web.filter; import java.io.IOException; import java.util.Enumeration; - import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.ServletException; @@ -32,20 +32,18 @@ import org.springframework.mock.web.test.MockFilterChain; import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletResponse; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; /** * Unit tests for {@link ForwardedHeaderFilter}. + * * @author Rossen Stoyanchev * @author EddĂș MelĂ©ndez * @author Rob Winch */ public class ForwardedHeaderFilterTests { - private static final String X_FORWARDED_PROTO = "x-forwarded-proto"; // SPR-14372 (case insensitive) + private static final String X_FORWARDED_PROTO = "x-forwarded-proto"; // SPR-14372 (case insensitive) private static final String X_FORWARDED_HOST = "x-forwarded-host"; private static final String X_FORWARDED_PORT = "x-forwarded-port"; private static final String X_FORWARDED_PREFIX = "x-forwarded-prefix"; @@ -60,7 +58,7 @@ public class ForwardedHeaderFilterTests { @Before @SuppressWarnings("serial") - public void setUp() throws Exception { + public void setup() throws Exception { this.request = new MockHttpServletRequest(); this.request.setScheme("http"); this.request.setServerName("localhost"); @@ -185,9 +183,7 @@ public class ForwardedHeaderFilterTests { @Test public void caseInsensitiveForwardedPrefix() throws Exception { this.request = new MockHttpServletRequest() { - // Make it case-sensitive (SPR-14372) - @Override public String getHeader(String header) { Enumeration<String> names = getHeaderNames(); @@ -404,6 +400,27 @@ public class ForwardedHeaderFilterTests { assertEquals("../foo/bar", redirectedUrl); } + @Test + public void sendRedirectWhenRequestOnlyAndXForwardedThenUsesRelativeRedirects() throws Exception { + this.request.addHeader(X_FORWARDED_PROTO, "https"); + this.request.addHeader(X_FORWARDED_HOST, "example.com"); + this.request.addHeader(X_FORWARDED_PORT, "443"); + this.filter.setRelativeRedirects(true); + + String location = sendRedirect("/a"); + + assertEquals("/a", location); + } + + @Test + public void sendRedirectWhenRequestOnlyAndNoXForwardedThenUsesRelativeRedirects() throws Exception { + this.filter.setRelativeRedirects(true); + + String location = sendRedirect("/a"); + + assertEquals("/a", location); + } + private String sendRedirect(final String location) throws ServletException, IOException { MockHttpServletResponse response = doWithFiltersAndGetResponse(this.filter, new OncePerRequestFilter() { @Override @@ -412,10 +429,10 @@ public class ForwardedHeaderFilterTests { response.sendRedirect(location); } }); - return response.getRedirectedUrl(); } + @SuppressWarnings("serial") private MockHttpServletResponse doWithFiltersAndGetResponse(Filter... filters) throws ServletException, IOException { MockHttpServletResponse response = new MockHttpServletResponse(); FilterChain filterChain = new MockFilterChain(new HttpServlet() {}, filters); diff --git a/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java index 179eb4cc..6a5de837 100644 --- a/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java +++ b/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -136,6 +136,7 @@ public class HttpPutFormContentFilterTests { @Test public void getParameterValues() throws Exception { + request.setQueryString("name=value1&name=value2"); request.addParameter("name", "value1"); request.addParameter("name", "value2"); request.setContent("name=value3&name=value4".getBytes("ISO-8859-1")); @@ -149,6 +150,7 @@ public class HttpPutFormContentFilterTests { @Test public void getParameterValuesFromQueryString() throws Exception { + request.setQueryString("name=value1&name=value2"); request.addParameter("name", "value1"); request.addParameter("name", "value2"); request.setContent("anotherName=anotherValue".getBytes("ISO-8859-1")); @@ -188,6 +190,7 @@ public class HttpPutFormContentFilterTests { @Test public void getParameterMap() throws Exception { + request.setQueryString("name=value1&name=value2"); request.addParameter("name", "value1"); request.addParameter("name", "value2"); request.setContent("name=value3&name4=value4".getBytes("ISO-8859-1")); diff --git a/spring-web/src/test/java/org/springframework/web/filter/RelativeRedirectFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/RelativeRedirectFilterTests.java new file mode 100644 index 00000000..46b80734 --- /dev/null +++ b/spring-web/src/test/java/org/springframework/web/filter/RelativeRedirectFilterTests.java @@ -0,0 +1,110 @@ +/* + * Copyright 2002-2017 the original author or authors. + * + * 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. + */ + +package org.springframework.web.filter; + +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletResponseWrapper; + +import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.Mockito; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.mock.web.test.MockFilterChain; +import org.springframework.mock.web.test.MockHttpServletRequest; +import org.springframework.mock.web.test.MockHttpServletResponse; + +import static org.junit.Assert.*; + +/** + * Unit tests for {@link RelativeRedirectFilter}. + * + * @author Rob Winch + * @author Juergen Hoeller + */ +public class RelativeRedirectFilterTests { + + private RelativeRedirectFilter filter = new RelativeRedirectFilter(); + + private HttpServletResponse response = Mockito.mock(HttpServletResponse.class); + + + @Test(expected = IllegalArgumentException.class) + public void sendRedirectHttpStatusWhenNullThenIllegalArgumentException() { + this.filter.setRedirectStatus(null); + } + + @Test(expected = IllegalArgumentException.class) + public void sendRedirectHttpStatusWhenNot3xxThenIllegalArgumentException() { + this.filter.setRedirectStatus(HttpStatus.OK); + } + + @Test + public void doFilterSendRedirectWhenDefaultsThenLocationAnd303() throws Exception { + String location = "/foo"; + sendRedirect(location); + + InOrder inOrder = Mockito.inOrder(this.response); + inOrder.verify(this.response).setStatus(HttpStatus.SEE_OTHER.value()); + inOrder.verify(this.response).setHeader(HttpHeaders.LOCATION, location); + } + + @Test + public void doFilterSendRedirectWhenCustomSendRedirectHttpStatusThenLocationAnd301() throws Exception { + String location = "/foo"; + HttpStatus status = HttpStatus.MOVED_PERMANENTLY; + this.filter.setRedirectStatus(status); + sendRedirect(location); + + InOrder inOrder = Mockito.inOrder(this.response); + inOrder.verify(this.response).setStatus(status.value()); + inOrder.verify(this.response).setHeader(HttpHeaders.LOCATION, location); + } + + @Test + public void wrapOnceOnly() throws Exception { + HttpServletResponse original = new MockHttpServletResponse(); + + MockFilterChain chain = new MockFilterChain(); + this.filter.doFilterInternal(new MockHttpServletRequest(), original, chain); + + HttpServletResponse wrapped1 = (HttpServletResponse) chain.getResponse(); + assertNotSame(original, wrapped1); + + chain.reset(); + this.filter.doFilterInternal(new MockHttpServletRequest(), wrapped1, chain); + HttpServletResponse current = (HttpServletResponse) chain.getResponse(); + assertSame(wrapped1, current); + + chain.reset(); + HttpServletResponse wrapped2 = new HttpServletResponseWrapper(wrapped1); + this.filter.doFilterInternal(new MockHttpServletRequest(), wrapped2, chain); + current = (HttpServletResponse) chain.getResponse(); + assertSame(wrapped2, current); + } + + + private void sendRedirect(String location) throws Exception { + MockFilterChain chain = new MockFilterChain(); + this.filter.doFilterInternal(new MockHttpServletRequest(), this.response, chain); + + HttpServletResponse wrappedResponse = (HttpServletResponse) chain.getResponse(); + wrappedResponse.sendRedirect(location); + } + +} diff --git a/spring-web/src/test/java/org/springframework/web/util/UriUtilsTests.java b/spring-web/src/test/java/org/springframework/web/util/UriUtilsTests.java index 2227674a..a54333e2 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriUtilsTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import static org.junit.Assert.*; /** * @author Arjen Poutsma * @author Juergen Hoeller + * @author Med Belamachi */ public class UriUtilsTests { @@ -113,6 +114,8 @@ public class UriUtilsTests { assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/a")); assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/path/a")); assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/path/a.do")); + assertEquals("html", UriUtils.extractFileExtension("/products/view.html#aaa?bbb")); + assertEquals("html", UriUtils.extractFileExtension("/products/view.html#aaa.xml?bbb")); assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=a")); assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=/path/a")); assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=/path/a.do")); |