使用 MockMvc 的自定义 Spring 安全过滤器测试总是 returns 404
Custom Spring Security Filter Test with MockMvc always returns 404
我正在尝试使用 MockMvc 测试自定义过滤器,它总是 returns 状态 404
而不是从过滤器 class 返回的实际状态。
过滤器 class 如下所示:
public class CustomFilter extends GenericFilterBean {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
// some logic
chain.doFilter(servletRequest, servletResponse);
}
}
这是使用 FilterRegistrationBean
在 SpringBootApplication
中注册的
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
try {
ConfigurableApplicationContext ctx = SpringApplication.run(MyApplication.class, args);
} catch (Throwable e) {
}
@Bean
public FilterRegistrationBean<CustomFilter> customFilter() {
FilterRegistrationBean<CustomFilter> filterRegistration = new FilterRegistrationBean<>();
filterRegistration.setFilter(new CustomFilter());
filterRegistration.addUrlPatterns("/test/*");
return filterRegistration;
}
}
对此的测试编写如下:
@RunWith(SpringRunner.class)
@SpringBootTest(CustomFilter.class)
@EnableAutoConfiguration
@WebAppConfiguration
public class CustomFilterTest {
@Autowired
private CustomFilter filter;
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
@Before
public void setup() {
this.mvc = MockMvcBuilders.webAppContextSetup(context)
.addFilters(filter)
.build();
}
@Test
public void testCustomFilter() throws Throwable {
MockHttpServletRequestBuilder request = MockMvcRequestBuilders
.request(HttpMethod.POST, "/resource/abc")
.header("SomeHeader", "xxx")
.content("{}");
MockHttpServletResponse response = mvc.perform(request).andReturn().getResponse();
assertEquals(response.getStatus(), HttpServletResponse.SC_CONTINUE);
}
}
断言总是returns状态值为404。
我是不是漏掉了什么?
将 urlPatterns 添加到 addFilter(filter, "/resource/abc")
并为上面的代码片段添加一个测试控制器,如下所示:
@RestController
public class TestController {
@PostMapping("/resource/abc")
public void testSecureEndpoint() {
}
}
我正在尝试使用 MockMvc 测试自定义过滤器,它总是 returns 状态 404
而不是从过滤器 class 返回的实际状态。
过滤器 class 如下所示:
public class CustomFilter extends GenericFilterBean {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
// some logic
chain.doFilter(servletRequest, servletResponse);
}
}
这是使用 FilterRegistrationBean
SpringBootApplication
中注册的
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
try {
ConfigurableApplicationContext ctx = SpringApplication.run(MyApplication.class, args);
} catch (Throwable e) {
}
@Bean
public FilterRegistrationBean<CustomFilter> customFilter() {
FilterRegistrationBean<CustomFilter> filterRegistration = new FilterRegistrationBean<>();
filterRegistration.setFilter(new CustomFilter());
filterRegistration.addUrlPatterns("/test/*");
return filterRegistration;
}
}
对此的测试编写如下:
@RunWith(SpringRunner.class)
@SpringBootTest(CustomFilter.class)
@EnableAutoConfiguration
@WebAppConfiguration
public class CustomFilterTest {
@Autowired
private CustomFilter filter;
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
@Before
public void setup() {
this.mvc = MockMvcBuilders.webAppContextSetup(context)
.addFilters(filter)
.build();
}
@Test
public void testCustomFilter() throws Throwable {
MockHttpServletRequestBuilder request = MockMvcRequestBuilders
.request(HttpMethod.POST, "/resource/abc")
.header("SomeHeader", "xxx")
.content("{}");
MockHttpServletResponse response = mvc.perform(request).andReturn().getResponse();
assertEquals(response.getStatus(), HttpServletResponse.SC_CONTINUE);
}
}
断言总是returns状态值为404。
我是不是漏掉了什么?
将 urlPatterns 添加到 addFilter(filter, "/resource/abc")
并为上面的代码片段添加一个测试控制器,如下所示:
@RestController
public class TestController {
@PostMapping("/resource/abc")
public void testSecureEndpoint() {
}
}