MockMvc supports FilterRegistration filter init

Closes gh-33252
This commit is contained in:
rstoyanchev
2024-07-25 15:35:21 +01:00
parent 38453910cd
commit d2225c2140
4 changed files with 176 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ import org.mockito.ArgumentCaptor;
import org.springframework.http.converter.json.SpringHandlerInstantiator;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
@@ -133,12 +134,17 @@ class StandaloneMockMvcBuilderTests {
Filter filter = mock(Filter.class);
ArgumentCaptor<FilterConfig> captor = ArgumentCaptor.forClass(FilterConfig.class);
MockMvcBuilders.standaloneSetup(new PersonController())
.addFilter(filter, null, Map.of("p", "v"), EnumSet.of(DispatcherType.REQUEST), "/")
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new PersonController())
.addFilter(filter, "testFilter", Map.of("p", "v"), EnumSet.of(DispatcherType.REQUEST), "/")
.build();
verify(filter, times(1)).init(captor.capture());
assertThat(captor.getValue().getInitParameter("p")).isEqualTo("v");
// gh-33252
assertThat(mockMvc.getDispatcherServlet().getServletContext().getFilterRegistrations())
.hasSize(1).containsKey("testFilter");
}
@Test // SPR-13375