Search in sources :

Example 66 with MockHttpServletRequest

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

the class DefaultServerRequestTests method checkNotModifiedUnpaddedETag.

@ParameterizedHttpMethodTest
void checkNotModifiedUnpaddedETag(String method) {
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest(method, "/", true);
    String eTag = "Foo";
    String paddedEtag = String.format("\"%s\"", eTag);
    servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, paddedEtag);
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    Optional<ServerResponse> result = request.checkNotModified(eTag);
    assertThat(result).hasValueSatisfying(serverResponse -> {
        assertThat(serverResponse.statusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
        assertThat(serverResponse.headers().getETag()).isEqualTo(paddedEtag);
    });
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest)

Example 67 with MockHttpServletRequest

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

the class DefaultServerRequestTests method checkNotModifiedETagAndModifiedTimestamp.

@ParameterizedHttpMethodTest
void checkNotModifiedETagAndModifiedTimestamp(String method) {
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest(method, "/", true);
    String eTag = "\"Foo\"";
    Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
    Instant oneMinuteAgo = now.minus(1, ChronoUnit.MINUTES);
    servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, eTag);
    servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, oneMinuteAgo.toEpochMilli());
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    Optional<ServerResponse> result = request.checkNotModified(now, eTag);
    assertThat(result).hasValueSatisfying(serverResponse -> {
        assertThat(serverResponse.statusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
        assertThat(serverResponse.headers().getETag()).isEqualTo(eTag);
        assertThat(serverResponse.headers().getLastModified()).isEqualTo(now.toEpochMilli());
    });
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Instant(java.time.Instant)

Example 68 with MockHttpServletRequest

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

the class DefaultServerRequestTests method absentQueryParam.

@Test
void absentQueryParam() {
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
    servletRequest.setParameter("foo", "");
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    assertThat(request.param("bar")).isEqualTo(Optional.empty());
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 69 with MockHttpServletRequest

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

the class DefaultServerRequestTests method principal.

@Test
void principal() {
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
    Principal principal = () -> "foo";
    servletRequest.setUserPrincipal(principal);
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    assertThat(request.principal().get()).isEqualTo(principal);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Principal(java.security.Principal) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 70 with MockHttpServletRequest

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

the class DefaultServerRequestTests method checkNotModifiedETag.

@ParameterizedHttpMethodTest
void checkNotModifiedETag(String method) {
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest(method, "/", true);
    String eTag = "\"Foo\"";
    servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, eTag);
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    Optional<ServerResponse> result = request.checkNotModified(eTag);
    assertThat(result).hasValueSatisfying(serverResponse -> {
        assertThat(serverResponse.statusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
        assertThat(serverResponse.headers().getETag()).isEqualTo(eTag);
    });
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest)

Aggregations

MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)752 Test (org.junit.jupiter.api.Test)458 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)359 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)180 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)83 BeforeEach (org.junit.jupiter.api.BeforeEach)73 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)57 ModelAndView (org.springframework.web.servlet.ModelAndView)45 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)45 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)39 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)38 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)38 TestBean (org.springframework.beans.testfixture.beans.TestBean)36 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)35 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)31 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)29 Cookie (jakarta.servlet.http.Cookie)27 HttpRequest (org.springframework.http.HttpRequest)27 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)26 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)25