|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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 | + * https://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 | +package org.springframework.web.filter; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import javax.servlet.DispatcherType; |
| 20 | +import javax.servlet.FilterChain; |
| 21 | +import javax.servlet.ServletException; |
| 22 | +import javax.servlet.http.HttpServlet; |
| 23 | +import javax.servlet.http.HttpServletRequest; |
| 24 | +import javax.servlet.http.HttpServletResponse; |
| 25 | + |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import org.springframework.mock.web.test.MockFilterChain; |
| 30 | +import org.springframework.mock.web.test.MockHttpServletRequest; |
| 31 | +import org.springframework.mock.web.test.MockHttpServletResponse; |
| 32 | +import org.springframework.web.util.WebUtils; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | + |
| 37 | +/** |
| 38 | + * Unit tests for {@link OncePerRequestFilter}. |
| 39 | + * @author Rossen Stoyanchev |
| 40 | + * @since 5.1.9 |
| 41 | + */ |
| 42 | +public class OncePerRequestFilterTests { |
| 43 | + |
| 44 | + private final TestOncePerRequestFilter filter = new TestOncePerRequestFilter(); |
| 45 | + |
| 46 | + private MockHttpServletRequest request; |
| 47 | + |
| 48 | + private MockFilterChain filterChain; |
| 49 | + |
| 50 | + |
| 51 | + @Before |
| 52 | + @SuppressWarnings("serial") |
| 53 | + public void setup() throws Exception { |
| 54 | + this.request = new MockHttpServletRequest(); |
| 55 | + this.request.setScheme("http"); |
| 56 | + this.request.setServerName("localhost"); |
| 57 | + this.request.setServerPort(80); |
| 58 | + this.filterChain = new MockFilterChain(new HttpServlet() {}); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Test |
| 63 | + public void filterOnce() throws ServletException, IOException { |
| 64 | + |
| 65 | + // Already filtered |
| 66 | + this.request.setAttribute(this.filter.getAlreadyFilteredAttributeName(), Boolean.TRUE); |
| 67 | + |
| 68 | + this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain); |
| 69 | + assertThat(this.filter.didFilter).isFalse(); |
| 70 | + assertThat(this.filter.didFilterNestedErrorDispatch).isFalse(); |
| 71 | + |
| 72 | + // Remove already filtered |
| 73 | + this.request.removeAttribute(this.filter.getAlreadyFilteredAttributeName()); |
| 74 | + this.filter.reset(); |
| 75 | + |
| 76 | + this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain); |
| 77 | + assertThat(this.filter.didFilter).isTrue(); |
| 78 | + assertThat(this.filter.didFilterNestedErrorDispatch).isFalse(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void shouldNotFilterErrorDispatch() throws ServletException, IOException { |
| 83 | + |
| 84 | + initErrorDispatch(); |
| 85 | + |
| 86 | + this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain); |
| 87 | + assertThat(this.filter.didFilter).isFalse(); |
| 88 | + assertThat(this.filter.didFilterNestedErrorDispatch).isFalse(); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void shouldNotFilterNestedErrorDispatch() throws ServletException, IOException { |
| 93 | + |
| 94 | + initErrorDispatch(); |
| 95 | + this.request.setAttribute(this.filter.getAlreadyFilteredAttributeName(), Boolean.TRUE); |
| 96 | + |
| 97 | + this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain); |
| 98 | + assertThat(this.filter.didFilter).isFalse(); |
| 99 | + assertThat(this.filter.didFilterNestedErrorDispatch).isFalse(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test // gh-23196 |
| 103 | + public void filterNestedErrorDispatch() throws ServletException, IOException { |
| 104 | + |
| 105 | + // Opt in for ERROR dispatch |
| 106 | + this.filter.setShouldNotFilterErrorDispatch(false); |
| 107 | + |
| 108 | + this.request.setAttribute(this.filter.getAlreadyFilteredAttributeName(), Boolean.TRUE); |
| 109 | + initErrorDispatch(); |
| 110 | + |
| 111 | + this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain); |
| 112 | + assertThat(this.filter.didFilter).isFalse(); |
| 113 | + assertThat(this.filter.didFilterNestedErrorDispatch).isTrue(); |
| 114 | + } |
| 115 | + |
| 116 | + private void initErrorDispatch() { |
| 117 | + this.request.setDispatcherType(DispatcherType.ERROR); |
| 118 | + this.request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error"); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + private static class TestOncePerRequestFilter extends OncePerRequestFilter { |
| 123 | + |
| 124 | + private boolean shouldNotFilter; |
| 125 | + |
| 126 | + private boolean shouldNotFilterAsyncDispatch = true; |
| 127 | + |
| 128 | + private boolean shouldNotFilterErrorDispatch = true; |
| 129 | + |
| 130 | + private boolean didFilter; |
| 131 | + |
| 132 | + private boolean didFilterNestedErrorDispatch; |
| 133 | + |
| 134 | + |
| 135 | + public void setShouldNotFilter(boolean shouldNotFilter) { |
| 136 | + this.shouldNotFilter = shouldNotFilter; |
| 137 | + } |
| 138 | + |
| 139 | + public void setShouldNotFilterAsyncDispatch(boolean shouldNotFilterAsyncDispatch) { |
| 140 | + this.shouldNotFilterAsyncDispatch = shouldNotFilterAsyncDispatch; |
| 141 | + } |
| 142 | + |
| 143 | + public void setShouldNotFilterErrorDispatch(boolean shouldNotFilterErrorDispatch) { |
| 144 | + this.shouldNotFilterErrorDispatch = shouldNotFilterErrorDispatch; |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + public boolean didFilter() { |
| 149 | + return this.didFilter; |
| 150 | + } |
| 151 | + |
| 152 | + public boolean didFilterNestedErrorDispatch() { |
| 153 | + return this.didFilterNestedErrorDispatch; |
| 154 | + } |
| 155 | + |
| 156 | + public void reset() { |
| 157 | + this.didFilter = false; |
| 158 | + this.didFilterNestedErrorDispatch = false; |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + @Override |
| 163 | + protected boolean shouldNotFilter(HttpServletRequest request) { |
| 164 | + return this.shouldNotFilter; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + protected boolean shouldNotFilterAsyncDispatch() { |
| 169 | + return this.shouldNotFilterAsyncDispatch; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + protected boolean shouldNotFilterErrorDispatch() { |
| 174 | + return this.shouldNotFilterErrorDispatch; |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, |
| 179 | + FilterChain filterChain) { |
| 180 | + |
| 181 | + this.didFilter = true; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + protected void doFilterNestedErrorDispatch(HttpServletRequest request, HttpServletResponse response, |
| 186 | + FilterChain filterChain) { |
| 187 | + |
| 188 | + this.didFilterNestedErrorDispatch = true; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | +} |
0 commit comments