Skip to content

Commit da1a17e

Browse files
committed
Stop adding filters with disabled registrations to MockMvc
Closes gh-14636
1 parent 3b51d79 commit da1a17e

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcBuilderCustomizer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -119,12 +119,16 @@ private void addFilters(ConfigurableMockMvcBuilder<?> builder) {
119119

120120
private void addFilter(ConfigurableMockMvcBuilder<?> builder,
121121
FilterRegistrationBean registration) {
122-
addFilter(builder, registration.getFilter(), registration.getUrlPatterns());
122+
if (registration.isEnabled()) {
123+
addFilter(builder, registration.getFilter(), registration.getUrlPatterns());
124+
}
123125
}
124126

125127
private void addFilter(ConfigurableMockMvcBuilder<?> builder,
126128
DelegatingFilterProxyRegistrationBean registration) {
127-
addFilter(builder, registration.getFilter(), registration.getUrlPatterns());
129+
if (registration.isEnabled()) {
130+
addFilter(builder, registration.getFilter(), registration.getUrlPatterns());
131+
}
128132
}
129133

130134
private void addFilter(ConfigurableMockMvcBuilder<?> builder, Filter filter,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2018 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.boot.test.autoconfigure.web.servlet;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.context.TestConfiguration;
24+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.test.context.junit4.SpringRunner;
27+
import org.springframework.test.web.servlet.MockMvc;
28+
29+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
31+
32+
/**
33+
* Tests for {@link WebMvcTest} with a disabled filter registration.
34+
*
35+
* @author Andy Wilkinson
36+
*/
37+
@RunWith(SpringRunner.class)
38+
@WebMvcTest
39+
public class WebMvcTestServletFilterRegistrationDisabledIntegrationTests {
40+
41+
@Autowired
42+
private MockMvc mvc;
43+
44+
@Test
45+
public void shouldApplyFilter() throws Exception {
46+
this.mvc.perform(get("/one")).andExpect(header().string("x-test", (String) null));
47+
}
48+
49+
@TestConfiguration
50+
static class DisabledRegistrationConfiguration {
51+
52+
@Bean
53+
public FilterRegistrationBean exampleFilterRegistration(ExampleFilter filter) {
54+
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
55+
registration.setEnabled(false);
56+
return registration;
57+
}
58+
59+
}
60+
61+
}

0 commit comments

Comments
 (0)