use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.
the class CharacterEncodingFilterTests method encodingIfEmptyAndNotForced.
@Test
public void encodingIfEmptyAndNotForced() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
given(request.getCharacterEncoding()).willReturn(null);
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = mock(FilterChain.class);
CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING);
filter.init(new MockFilterConfig(FILTER_NAME));
filter.doFilter(request, response, filterChain);
verify(request).setCharacterEncoding(ENCODING);
verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
verify(request).removeAttribute(filteredName(FILTER_NAME));
verify(filterChain).doFilter(request, response);
}
use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.
the class CharacterEncodingFilterTests method doesNotIfEncodingIsNotEmptyAndNotForced.
@Test
public void doesNotIfEncodingIsNotEmptyAndNotForced() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
given(request.getCharacterEncoding()).willReturn(ENCODING);
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = mock(FilterChain.class);
CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING);
filter.init(new MockFilterConfig(FILTER_NAME));
filter.doFilter(request, response, filterChain);
verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
verify(request).removeAttribute(filteredName(FILTER_NAME));
verify(filterChain).doFilter(request, response);
}
use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.
the class CorsFilterTests method nonCorsRequest.
@Test
void nonCorsRequest() throws ServletException, IOException {
MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(), "/test.html");
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = (filterRequest, filterResponse) -> {
assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isNull();
assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS)).isNull();
};
filter.doFilter(request, response, filterChain);
}
use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.
the class RequestContextFilterTests method testFilterInvocation.
private void testFilterInvocation(final ServletException sex) throws Exception {
final MockHttpServletRequest req = new MockHttpServletRequest();
req.setAttribute("myAttr", "myValue");
final MockHttpServletResponse resp = new MockHttpServletResponse();
// Expect one invocation by the filter being tested
class DummyFilterChain implements FilterChain {
public int invocations = 0;
@Override
public void doFilter(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
++invocations;
if (invocations == 1) {
assertThat(RequestContextHolder.currentRequestAttributes().getAttribute("myAttr", RequestAttributes.SCOPE_REQUEST)).isSameAs("myValue");
if (sex != null) {
throw sex;
}
} else {
throw new IllegalStateException("Too many invocations");
}
}
}
DummyFilterChain fc = new DummyFilterChain();
MockFilterConfig mfc = new MockFilterConfig(new MockServletContext(), "foo");
RequestContextFilter rbf = new RequestContextFilter();
rbf.init(mfc);
try {
rbf.doFilter(req, resp, fc);
assertThat(sex).isNull();
} catch (ServletException ex) {
assertThat(sex).isNotNull();
}
assertThatIllegalStateException().isThrownBy(RequestContextHolder::currentRequestAttributes);
assertThat(fc.invocations).isEqualTo(1);
}
use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.
the class HiddenHttpMethodFilterTests method filterWithParameterForMethod.
private void filterWithParameterForMethod(String methodParam, String expectedMethod) throws IOException, ServletException {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
if (methodParam != null) {
request.addParameter("_method", methodParam);
}
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = (filterRequest, filterResponse) -> assertThat(((HttpServletRequest) filterRequest).getMethod()).as("Invalid method").isEqualTo(expectedMethod);
this.filter.doFilter(request, response, filterChain);
}
Aggregations