Refactor relative redirect filter support

Issue: SPR-15717
This commit is contained in:
Rossen Stoyanchev
2017-07-19 18:46:16 +02:00
parent fadf04e4b4
commit 68e6b148cb
5 changed files with 164 additions and 72 deletions

View File

@@ -408,7 +408,7 @@ public class ForwardedHeaderFilterTests {
this.request.addHeader(X_FORWARDED_PROTO, "https");
this.request.addHeader(X_FORWARDED_HOST, "example.com");
this.request.addHeader(X_FORWARDED_PORT, "443");
this.filter.setRequestOnly(true);
this.filter.setRelativeRedirects(true);
String location = sendRedirect("/a");
@@ -417,7 +417,7 @@ public class ForwardedHeaderFilterTests {
@Test
public void sendRedirectWhenRequestOnlyAndNoXForwardedThenUsesRelativeRedirects() throws Exception {
this.filter.setRequestOnly(true);
this.filter.setRelativeRedirects(true);
String location = sendRedirect("/a");

View File

@@ -16,67 +16,96 @@
package org.springframework.web.filter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
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 javax.servlet.http.HttpServletResponse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
/**
* Unit tests for {@link RelativeRedirectFilter}.
* @author Rob Winch
* @since 4.3.10
*/
@RunWith(MockitoJUnitRunner.class)
public class RelativeRedirectFilterTests {
@Mock
HttpServletResponse response;
RelativeRedirectFilter filter = new RelativeRedirectFilter();
@Test(expected = IllegalArgumentException.class)
public void sendRedirectHttpStatusWhenNullThenIllegalArgumentException() {
this.filter.setSendRedirectHttpStatus(null);
this.filter.setRedirectStatus(null);
}
@Test(expected = IllegalArgumentException.class)
public void sendRedirectHttpStatusWhenNot3xxThenIllegalArgumentException() {
this.filter.setSendRedirectHttpStatus(HttpStatus.OK);
this.filter.setRedirectStatus(HttpStatus.OK);
}
@Test
public void doFilterSendRedirectWhenDefaultsThenLocationAnd302() throws Exception {
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);
inOrder.verify(this.response).setStatus(HttpStatus.FOUND.value());
}
@Test
public void doFilterSendRedirectWhenCustomSendRedirectHttpStatusThenLocationAnd301() throws Exception {
String location = "/foo";
HttpStatus status = HttpStatus.MOVED_PERMANENTLY;
this.filter.setSendRedirectHttpStatus(status);
this.filter.setRedirectStatus(status);
sendRedirect(location);
InOrder inOrder = Mockito.inOrder(this.response);
inOrder.verify(this.response).setHeader(HttpHeaders.LOCATION, location);
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();
filter.doFilterInternal(new MockHttpServletRequest(), response, chain);
this.filter.doFilterInternal(new MockHttpServletRequest(), this.response, chain);
HttpServletResponse wrappedResponse = (HttpServletResponse) chain.getResponse();
wrappedResponse.sendRedirect(location);