Add SpringTestContext.addFilter
Add SpringTestContext.addFilter which allows Spring Security's tests to specify a Filter to be added to the SpringTestContext. Closes gh-12071
This commit is contained in:
parent
55877c5f32
commit
a4858d9eaa
|
@ -46,7 +46,7 @@ import org.springframework.security.web.header.HeaderWriterFilter;
|
|||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ExtendWith(SpringTestContextExtension.class)
|
||||
public class HttpSecurityAddFilterTest {
|
||||
public class HttpSecurityDeferAddFilterTest {
|
||||
|
||||
public final SpringTestContext spring = new SpringTestContext(this);
|
||||
|
|
@ -57,6 +57,8 @@ public class SpringTestContext implements Closeable {
|
|||
|
||||
private List<Filter> filters = new ArrayList<>();
|
||||
|
||||
private DeferAddFilter deferAddFilter = new DeferAddFilter();
|
||||
|
||||
private List<Consumer<ConfigurableWebApplicationContext>> postProcessors = new ArrayList<>();
|
||||
|
||||
public SpringTestContext(Object test) {
|
||||
|
@ -113,16 +115,17 @@ public class SpringTestContext implements Closeable {
|
|||
}
|
||||
|
||||
public SpringTestContext mockMvcAfterSpringSecurityOk() {
|
||||
return addFilter(new OncePerRequestFilter() {
|
||||
this.deferAddFilter.addFilter(new OncePerRequestFilter() {
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||
FilterChain filterChain) {
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
private SpringTestContext addFilter(Filter filter) {
|
||||
public SpringTestContext addFilter(Filter filter) {
|
||||
this.filters.add(filter);
|
||||
return this;
|
||||
}
|
||||
|
@ -145,9 +148,10 @@ public class SpringTestContext implements Closeable {
|
|||
this.context.refresh();
|
||||
if (this.context.containsBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN)) {
|
||||
// @formatter:off
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).
|
||||
apply(springSecurity())
|
||||
.apply(new AddFilter())
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.addFilters(this.filters.toArray(new Filter[0]))
|
||||
.apply(springSecurity())
|
||||
.apply(this.deferAddFilter)
|
||||
.build();
|
||||
// @formatter:on
|
||||
this.context.getBeanFactory().registerResolvableDependency(MockMvc.class, mockMvc);
|
||||
|
@ -157,12 +161,18 @@ public class SpringTestContext implements Closeable {
|
|||
bpp.processInjection(this.test);
|
||||
}
|
||||
|
||||
private class AddFilter implements MockMvcConfigurer {
|
||||
private static class DeferAddFilter implements MockMvcConfigurer {
|
||||
|
||||
private List<Filter> filters = new ArrayList<>();
|
||||
|
||||
void addFilter(Filter filter) {
|
||||
this.filters.add(filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RequestPostProcessor beforeMockMvcCreated(ConfigurableMockMvcBuilder<?> builder,
|
||||
WebApplicationContext context) {
|
||||
builder.addFilters(SpringTestContext.this.filters.toArray(new Filter[0]));
|
||||
builder.addFilters(this.filters.toArray(new Filter[0]));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue