1+ /*
2+ * Copyright 2002-2017 the original author or authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package org .springframework .web .filter ;
18+
19+ import org .junit .Test ;
20+ import org .junit .runner .RunWith ;
21+ import org .mockito .InOrder ;
22+ import org .mockito .Mock ;
23+ import org .mockito .Mockito ;
24+ import org .mockito .junit .MockitoJUnitRunner ;
25+ import org .springframework .http .HttpHeaders ;
26+ import org .springframework .http .HttpStatus ;
27+ import org .springframework .mock .web .test .MockFilterChain ;
28+ import org .springframework .mock .web .test .MockHttpServletRequest ;
29+
30+ import javax .servlet .http .HttpServletResponse ;
31+
32+ /**
33+ * @author Rob Winch
34+ * @since 4.3.10
35+ */
36+ @ RunWith (MockitoJUnitRunner .class )
37+ public class RelativeRedirectFilterTests {
38+ @ Mock
39+ HttpServletResponse response ;
40+
41+ RelativeRedirectFilter filter = new RelativeRedirectFilter ();
42+
43+ @ Test (expected = IllegalArgumentException .class )
44+ public void sendRedirectHttpStatusWhenNullThenIllegalArgumentException () {
45+ this .filter .setSendRedirectHttpStatus (null );
46+ }
47+
48+ @ Test (expected = IllegalArgumentException .class )
49+ public void sendRedirectHttpStatusWhenNot3xxThenIllegalArgumentException () {
50+ this .filter .setSendRedirectHttpStatus (HttpStatus .OK );
51+ }
52+
53+ @ Test
54+ public void doFilterSendRedirectWhenDefaultsThenLocationAnd302 () throws Exception {
55+ String location = "/foo" ;
56+
57+ sendRedirect (location );
58+
59+ InOrder inOrder = Mockito .inOrder (this .response );
60+ inOrder .verify (this .response ).setHeader (HttpHeaders .LOCATION , location );
61+ inOrder .verify (this .response ).setStatus (HttpStatus .FOUND .value ());
62+ }
63+
64+ @ Test
65+ public void doFilterSendRedirectWhenCustomSendRedirectHttpStatusThenLocationAnd301 () throws Exception {
66+ String location = "/foo" ;
67+ HttpStatus status = HttpStatus .MOVED_PERMANENTLY ;
68+ this .filter .setSendRedirectHttpStatus (status );
69+ sendRedirect (location );
70+
71+ InOrder inOrder = Mockito .inOrder (this .response );
72+ inOrder .verify (this .response ).setHeader (HttpHeaders .LOCATION , location );
73+ inOrder .verify (this .response ).setStatus (status .value ());
74+ }
75+
76+ private void sendRedirect (String location ) throws Exception {
77+ MockFilterChain chain = new MockFilterChain ();
78+
79+ filter .doFilterInternal (new MockHttpServletRequest (), response , chain );
80+
81+ HttpServletResponse wrappedResponse = (HttpServletResponse ) chain .getResponse ();
82+ wrappedResponse .sendRedirect (location );
83+ }
84+ }
0 commit comments