2424import static org .mockito .Mockito .verify ;
2525import static org .mockito .Mockito .when ;
2626
27+ import org .junit .jupiter .api .AfterEach ;
2728import org .junit .jupiter .api .BeforeEach ;
2829import org .junit .jupiter .api .Test ;
2930
@@ -53,12 +54,18 @@ public void setUp() {
5354 when (req .getContextPath ()).thenReturn ("" );
5455 resp = mock (HttpServletResponse .class );
5556 next = mock (FilterChain .class );
57+ System .setProperty ("ControlFilterTests" , "bypassPreventsStreamExploitation" );
5658 filter = new ControlFilter ();
59+
60+ }
61+
62+ @ AfterEach
63+ public void tearDown () {
64+ System .clearProperty ("ControlFilterTests" );
5765 }
5866
5967 @ Test
6068 public void filterWithExactAllowedPath () throws Exception {
61- System .setProperty ("ControlFilterTests" , "bypassPreventsStreamExploitation" );
6269 when (config .getInitParameter ("redirectPath" )).thenReturn ("/foo" );
6370 when (config .getInitParameter ("allowedPaths" )).thenReturn ("/foo:/bar" );
6471 when (req .getRequestURI ()).thenReturn ("/servlet/bar" );
@@ -67,12 +74,10 @@ public void filterWithExactAllowedPath() throws Exception {
6774 filter .init (config );
6875 filter .doFilter (req , resp , next );
6976 verify (next ).doFilter (req , resp );
70- System .clearProperty ("ControlFilterTests" );
7177 }
7278
7379 @ Test
7480 public void filterWithAllowedSubPath () throws Exception {
75- System .setProperty ("ControlFilterTests" , "bypassPreventsStreamExploitation" );
7681 when (config .getInitParameter ("redirectPath" )).thenReturn ("/foo" );
7782 when (config .getInitParameter ("allowedPaths" )).thenReturn ("/foo:/bar" );
7883 when (req .getRequestURI ()).thenReturn ("/servlet/bar/baz" );
@@ -81,38 +86,32 @@ public void filterWithAllowedSubPath() throws Exception {
8186 filter .init (config );
8287 filter .doFilter (req , resp , next );
8388 verify (next ).doFilter (req , resp );
84- System .clearProperty ("ControlFilterTests" );
8589 }
8690
8791 @ Test
8892 public void filterWithRedirection () throws Exception {
89- System .setProperty ("ControlFilterTests" , "bypassPreventsStreamExploitation" );
9093 when (config .getInitParameter ("redirectPath" )).thenReturn ("/foo" );
9194 when (config .getInitParameter ("allowedPaths" )).thenReturn ("/bar:/baz" );
9295 when (req .getRequestURI ()).thenReturn ("/missing/path" );
9396
9497 filter .init (config );
9598 filter .doFilter (req , resp , next );
9699 verify (resp ).sendRedirect ("/foo" );
97- System .clearProperty ("ControlFilterTests" );
98100 }
99101
100102 @ Test
101103 public void filterWithURIredirection () throws Exception {
102- System .setProperty ("ControlFilterTests" , "bypassPreventsStreamExploitation" );
103104 when (config .getInitParameter ("redirectPath" )).thenReturn ("http://example.org/foo" );
104105 when (config .getInitParameter ("allowedPaths" )).thenReturn ("/foo:/bar" );
105106 when (req .getRequestURI ()).thenReturn ("/baz" );
106107
107108 filter .init (config );
108109 filter .doFilter (req , resp , next );
109110 verify (resp ).sendRedirect ("http://example.org/foo" );
110- System .clearProperty ("ControlFilterTests" );
111111 }
112112
113113 @ Test
114114 public void bailsOutWithVariousErrorCodes () throws Exception {
115- System .setProperty ("ControlFilterTests" , "bypassPreventsStreamExploitation" );
116115 when (config .getInitParameter ("allowedPaths" )).thenReturn ("/foo" );
117116 when (req .getRequestURI ()).thenReturn ("/baz" );
118117
@@ -138,12 +137,10 @@ public void bailsOutWithVariousErrorCodes() throws Exception {
138137 filter .init (config );
139138 filter .doFilter (req , resp , next );
140139 verify (resp ).sendError (404 , "/baz" );
141- System .clearProperty ("ControlFilterTests" );
142140 }
143141
144142 @ Test
145143 public void redirectAllAllowed () throws Exception {
146- System .setProperty ("ControlFilterTests" , "bypassPreventsStreamExploitation" );
147144 when (config .getInitParameter ("redirectPath" )).thenReturn ("/bar" );
148145 when (config .getInitParameter ("forceRedirectAll" )).thenReturn ("Y" );
149146 when (config .getInitParameter ("allowedPaths" )).thenReturn ("/foo" );
@@ -152,12 +149,10 @@ public void redirectAllAllowed() throws Exception {
152149 filter .init (config );
153150 filter .doFilter (req , resp , next );
154151 verify (resp ).sendRedirect ("/bar" );
155- System .clearProperty ("ControlFilterTests" );
156152 }
157153
158154 @ Test
159155 public void redirectAllNotAllowed () throws Exception {
160- System .setProperty ("ControlFilterTests" , "bypassPreventsStreamExploitation" );
161156 when (config .getInitParameter ("redirectPath" )).thenReturn ("/bar" );
162157 when (config .getInitParameter ("forceRedirectAll" )).thenReturn ("Y" );
163158 when (config .getInitParameter ("allowedPaths" )).thenReturn ("/foo" );
@@ -166,12 +161,10 @@ public void redirectAllNotAllowed() throws Exception {
166161 filter .init (config );
167162 filter .doFilter (req , resp , next );
168163 verify (resp ).sendRedirect ("/bar" );
169- System .clearProperty ("ControlFilterTests" );
170164 }
171165
172166 @ Test
173167 public void redirectAllRecursive () throws Exception {
174- System .setProperty ("ControlFilterTests" , "bypassPreventsStreamExploitation" );
175168 when (config .getInitParameter ("redirectPath" )).thenReturn ("/foo" );
176169 when (config .getInitParameter ("forceRedirectAll" )).thenReturn ("Y" );
177170 when (config .getInitParameter ("allowedPaths" )).thenReturn ("/foo" );
@@ -189,6 +182,5 @@ public void redirectAllRecursive() throws Exception {
189182 filter .doFilter (req , resp , next );
190183 verify (next ).doFilter (req , resp );
191184 verify (session ).removeAttribute ("_FORCE_REDIRECT_" );
192- System .clearProperty ("ControlFilterTests" );
193185 }
194186}
0 commit comments