Search in sources :

Example 1 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class RequestLoggingFilterTests method allOptions.

@Test
void allOptions() throws Exception {
    filter.setIncludeQueryString(true);
    filter.setIncludeClientInfo(true);
    filter.setIncludeHeaders(true);
    filter.setIncludePayload(true);
    request.setQueryString("booking=42");
    request.setRemoteAddr("4.2.2.2");
    request.setSession(new MockHttpSession(null, "42"));
    request.setRemoteUser("Arthur");
    request.setContentType("application/json");
    String requestBody = "{\"msg\": \"Hello World\"}";
    request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
    FilterChain filterChain = (filterRequest, filterResponse) -> {
        ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
        String buf = FileCopyUtils.copyToString(filterRequest.getReader());
        assertThat(buf).isEqualTo(requestBody);
    };
    filter.doFilter(request, response, filterChain);
    assertThat(filter.beforeRequestMessage).isEqualTo("Before request [" + "POST /hotels?booking=42" + ", client=4.2.2.2" + ", session=42" + ", user=Arthur" + ", headers=[Content-Type:\"application/json;charset=ISO-8859-1\", Content-Length:\"22\"]" + "]");
    assertThat(filter.afterRequestMessage).isEqualTo("After request [" + "POST /hotels?booking=42" + ", client=4.2.2.2" + ", session=42" + ", user=Arthur" + ", headers=[Content-Type:\"application/json;charset=ISO-8859-1\", Content-Length:\"22\"]" + ", payload={\"msg\": \"Hello World\"}" + "]");
}
Also used : Test(org.junit.jupiter.api.Test) WebUtils(org.springframework.web.util.WebUtils) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) FileCopyUtils(org.springframework.util.FileCopyUtils) FilterChain(jakarta.servlet.FilterChain) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 2 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class HttpSessionHandshakeInterceptorTests method defaultConstructor.

@Test
public void defaultConstructor() throws Exception {
    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");
    this.servletRequest.getSession().setAttribute("bar", "baz");
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
    assertThat(attributes.size()).isEqualTo(3);
    assertThat(attributes.get("foo")).isEqualTo("bar");
    assertThat(attributes.get("bar")).isEqualTo("baz");
    assertThat(attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME)).isEqualTo("123");
}
Also used : MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 3 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class HttpSessionHandshakeInterceptorTests method doNotCopyAttributes.

@Test
public void doNotCopyAttributes() throws Exception {
    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    interceptor.setCopyAllAttributes(false);
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
    assertThat(attributes.size()).isEqualTo(1);
    assertThat(attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME)).isEqualTo("123");
}
Also used : MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 4 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class HttpSessionHandshakeInterceptorTests method constructorWithAttributeNames.

@Test
public void constructorWithAttributeNames() throws Exception {
    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");
    this.servletRequest.getSession().setAttribute("bar", "baz");
    Set<String> names = Collections.singleton("foo");
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor(names);
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
    assertThat(attributes.size()).isEqualTo(2);
    assertThat(attributes.get("foo")).isEqualTo("bar");
    assertThat(attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME)).isEqualTo("123");
}
Also used : MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 5 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class HttpSessionHandshakeInterceptorTests method doNotCopyHttpSessionId.

@Test
public void doNotCopyHttpSessionId() throws Exception {
    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    interceptor.setCopyHttpSessionId(false);
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
    assertThat(attributes.size()).isEqualTo(1);
    assertThat(attributes.get("foo")).isEqualTo("bar");
}
Also used : MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpSession (org.springframework.web.testfixture.servlet.MockHttpSession)15 Test (org.junit.jupiter.api.Test)14 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)9 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TestBean (org.springframework.beans.testfixture.beans.TestBean)2 FilterChain (jakarta.servlet.FilterChain)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 Serializable (java.io.Serializable)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodParameter (org.springframework.core.MethodParameter)1 FileCopyUtils (org.springframework.util.FileCopyUtils)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1 ContentCachingRequestWrapper (org.springframework.web.util.ContentCachingRequestWrapper)1 WebUtils (org.springframework.web.util.WebUtils)1