Skip to content

Commit

Permalink
Add FilterRegistration's defensively
Browse files Browse the repository at this point in the history
Follow-up to d2225c, which broke Boot tests because the ServletContext can
be the one of the embedded Servlet container if initializing a live server
and as well as MockMvc (e.g. via `@AutoConfigureMockMvc`).

See gh-33252
  • Loading branch information
rstoyanchev committed Jul 31, 2024
1 parent f1a99cd commit 31a3119
Showing 1 changed file with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,18 @@ private static Function<ServletContext, FilterConfig> getFilterConfigInitializer
String className = delegate.getClass().getName();

return servletContext -> {
MockServletContext mockServletContext = (MockServletContext) servletContext;
MockFilterConfig filterConfig;
if (filterName != null) {
filterConfig = new MockFilterConfig(servletContext, filterName);
mockServletContext.addFilterRegistration(new MockFilterRegistration(className, filterName));
}
else {
filterConfig = new MockFilterConfig(servletContext);
mockServletContext.addFilterRegistration(new MockFilterRegistration(className));
}
MockFilterConfig filterConfig = (filterName != null ?
new MockFilterConfig(servletContext, filterName) : new MockFilterConfig(servletContext));

if (initParams != null) {
initParams.forEach(filterConfig::addInitParameter);
}

if (servletContext instanceof MockServletContext mockServletContext) {
mockServletContext.addFilterRegistration(filterName != null ?
new MockFilterRegistration(className, filterName) : new MockFilterRegistration(className));
}

return filterConfig;
};
}
Expand Down

0 comments on commit 31a3119

Please sign in to comment.