|
| 1 | +/* |
| 2 | + * Copyright 2012-present 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 | + |
| 17 | +package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import jakarta.servlet.FilterChain; |
| 24 | +import jakarta.servlet.FilterConfig; |
| 25 | +import jakarta.servlet.ServletException; |
| 26 | +import jakarta.servlet.annotation.WebInitParam; |
| 27 | +import jakarta.servlet.http.HttpServletRequest; |
| 28 | +import jakarta.servlet.http.HttpServletResponse; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.boot.autoconfigure.security.SecurityProperties; |
| 33 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 34 | +import org.springframework.boot.test.context.TestConfiguration; |
| 35 | +import org.springframework.boot.web.servlet.FilterRegistration; |
| 36 | +import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| 37 | +import org.springframework.context.annotation.Bean; |
| 38 | +import org.springframework.core.annotation.Order; |
| 39 | +import org.springframework.test.web.servlet.assertj.MockMvcTester; |
| 40 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 41 | + |
| 42 | +import static org.assertj.core.api.Assertions.assertThat; |
| 43 | + |
| 44 | +/** |
| 45 | + * Tests for {@link FilterRegistration} and {@link FilterRegistrationBean} with |
| 46 | + * {@link WebMvcTest @WebMvcTest}. |
| 47 | + * |
| 48 | + * @author Dmytro Nosan |
| 49 | + */ |
| 50 | +@WebMvcTest |
| 51 | +class WebMvcTestServletFilterRegistrationIntegrationTests { |
| 52 | + |
| 53 | + @Autowired |
| 54 | + private MockMvcTester mvc; |
| 55 | + |
| 56 | + @Test |
| 57 | + void annotation() { |
| 58 | + assertThat(this.mvc.get().uri("/annotation")).headers() |
| 59 | + .hasValue("name", "annotation") |
| 60 | + .hasValue("param1", "value1") |
| 61 | + .hasValue("param2", "value2") |
| 62 | + .doesNotContainHeader("param3") |
| 63 | + .doesNotContainHeader("param4"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void registration() { |
| 68 | + assertThat(this.mvc.get().uri("/registration")).headers() |
| 69 | + .hasValue("name", "registration") |
| 70 | + .hasValue("param3", "value3") |
| 71 | + .hasValue("param4", "value4") |
| 72 | + .doesNotContainHeader("param1") |
| 73 | + .doesNotContainHeader("param2"); |
| 74 | + } |
| 75 | + |
| 76 | + @TestConfiguration(proxyBeanMethods = false) |
| 77 | + static class FilterRegistrationConfiguration { |
| 78 | + |
| 79 | + @Bean |
| 80 | + @FilterRegistration(name = "annotation", urlPatterns = "/annotation", |
| 81 | + initParameters = { @WebInitParam(name = "param1", value = "value1"), |
| 82 | + @WebInitParam(name = "param2", value = "value2") }) |
| 83 | + @Order(SecurityProperties.DEFAULT_FILTER_ORDER - 1) |
| 84 | + TestFilter testFilterAnnotationBean() { |
| 85 | + return new TestFilter(); |
| 86 | + } |
| 87 | + |
| 88 | + @Bean |
| 89 | + FilterRegistrationBean<TestFilter> testFilterRegistrationBean() { |
| 90 | + FilterRegistrationBean<TestFilter> registration = new FilterRegistrationBean<>(new TestFilter()); |
| 91 | + registration.setName("registration"); |
| 92 | + registration.addUrlPatterns("/registration"); |
| 93 | + registration.setInitParameters(Map.of("param3", "value3", "param4", "value4")); |
| 94 | + registration.setOrder(SecurityProperties.DEFAULT_FILTER_ORDER - 1); |
| 95 | + return registration; |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + private static final class TestFilter extends OncePerRequestFilter { |
| 101 | + |
| 102 | + @Override |
| 103 | + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, |
| 104 | + FilterChain filterChain) throws ServletException, IOException { |
| 105 | + response.addHeader("name", getFilterName()); |
| 106 | + FilterConfig config = getFilterConfig(); |
| 107 | + if (config != null) { |
| 108 | + Collections.list(config.getInitParameterNames()) |
| 109 | + .forEach((name) -> response.addHeader(name, config.getInitParameter(name))); |
| 110 | + } |
| 111 | + filterChain.doFilter(request, response); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments