Skip to content

Commit 726579a

Browse files
committed
Merge branch '2.0.x'
2 parents 42a77a0 + 89f5cfc commit 726579a

File tree

2 files changed

+70
-20
lines changed

2 files changed

+70
-20
lines changed

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030

3131
import org.springframework.beans.factory.ListableBeanFactory;
3232
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
33+
import org.springframework.boot.web.servlet.AbstractFilterRegistrationBean;
3334
import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean;
3435
import org.springframework.boot.web.servlet.FilterRegistrationBean;
3536
import org.springframework.boot.web.servlet.RegistrationBean;
36-
import org.springframework.boot.web.servlet.ServletContextInitializer;
3737
import org.springframework.boot.web.servlet.ServletContextInitializerBeans;
3838
import org.springframework.context.ApplicationContext;
3939
import org.springframework.context.ConfigurableApplicationContext;
@@ -108,25 +108,11 @@ private LinesWriter getLinesWriter() {
108108
}
109109

110110
private void addFilters(ConfigurableMockMvcBuilder<?> builder) {
111-
FilterRegistrationBeans initializers = new FilterRegistrationBeans(this.context);
112-
for (ServletContextInitializer initializer : initializers) {
113-
if (initializer instanceof FilterRegistrationBean) {
114-
addFilter(builder, (FilterRegistrationBean<?>) initializer);
115-
}
116-
if (initializer instanceof DelegatingFilterProxyRegistrationBean) {
117-
addFilter(builder, (DelegatingFilterProxyRegistrationBean) initializer);
118-
}
119-
}
120-
}
121-
122-
private void addFilter(ConfigurableMockMvcBuilder<?> builder,
123-
FilterRegistrationBean<?> registration) {
124-
addFilter(builder, registration.getFilter(), registration.getUrlPatterns());
125-
}
126-
127-
private void addFilter(ConfigurableMockMvcBuilder<?> builder,
128-
DelegatingFilterProxyRegistrationBean registration) {
129-
addFilter(builder, registration.getFilter(), registration.getUrlPatterns());
111+
FilterRegistrationBeans registrations = new FilterRegistrationBeans(this.context);
112+
registrations.stream().map(AbstractFilterRegistrationBean.class::cast)
113+
.filter(AbstractFilterRegistrationBean::isEnabled)
114+
.forEach((registration) -> addFilter(builder, registration.getFilter(),
115+
registration.getUrlPatterns()));
130116
}
131117

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

0 commit comments

Comments
 (0)