use of jakarta.servlet.http.HttpServletRequest 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.http.HttpServletRequest 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.http.HttpServletRequest in project spring-framework by spring-projects.
the class ForwardedHeaderFilterTests method forwardedRequestWithServletForward.
// SPR-16983
@Test
public void forwardedRequestWithServletForward() throws Exception {
this.request.setRequestURI("/foo");
this.request.addHeader(X_FORWARDED_PROTO, "https");
this.request.addHeader(X_FORWARDED_HOST, "www.mycompany.example");
this.request.addHeader(X_FORWARDED_PORT, "443");
this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain);
HttpServletRequest wrappedRequest = (HttpServletRequest) this.filterChain.getRequest();
this.request.setDispatcherType(DispatcherType.FORWARD);
this.request.setRequestURI("/bar");
this.filterChain.reset();
this.filter.doFilter(wrappedRequest, new MockHttpServletResponse(), this.filterChain);
HttpServletRequest actual = (HttpServletRequest) this.filterChain.getRequest();
assertThat(actual).isNotNull();
assertThat(actual.getRequestURI()).isEqualTo("/bar");
assertThat(actual.getRequestURL().toString()).isEqualTo("https://www.mycompany.example/bar");
}
use of jakarta.servlet.http.HttpServletRequest 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);
}
use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.
the class ControllerTests method servletWrappingController.
@Test
public void servletWrappingController() throws Exception {
HttpServletRequest request = new MockHttpServletRequest("GET", "/somePath");
HttpServletResponse response = new MockHttpServletResponse();
ServletWrappingController swc = new ServletWrappingController();
swc.setServletClass(TestServlet.class);
swc.setServletName("action");
Properties props = new Properties();
props.setProperty("config", "myValue");
swc.setInitParameters(props);
swc.afterPropertiesSet();
assertThat(TestServlet.config).isNotNull();
assertThat(TestServlet.config.getServletName()).isEqualTo("action");
assertThat(TestServlet.config.getInitParameter("config")).isEqualTo("myValue");
assertThat(TestServlet.request).isNull();
assertThat(TestServlet.destroyed).isFalse();
assertThat(swc.handleRequest(request, response)).isNull();
assertThat(TestServlet.request).isEqualTo(request);
assertThat(TestServlet.response).isEqualTo(response);
assertThat(TestServlet.destroyed).isFalse();
swc.destroy();
assertThat(TestServlet.destroyed).isTrue();
}
Aggregations