Search in sources :

Example 71 with MockHttpServletRequest

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

the class DefaultServerRequestTests method pathVariableNotFound.

@Test
void pathVariableNotFound() {
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
    Map<String, String> pathVariables = Collections.singletonMap("foo", "bar");
    servletRequest.setAttribute(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE, pathVariables);
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    assertThatIllegalArgumentException().isThrownBy(() -> request.pathVariable("baz"));
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 72 with MockHttpServletRequest

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

the class DefaultServerRequestTests method checkNotModifiedWildcardIsIgnored.

@ParameterizedHttpMethodTest
void checkNotModifiedWildcardIsIgnored(String method) {
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest(method, "/", true);
    String eTag = "\"Foo\"";
    servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, "*");
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    Optional<ServerResponse> result = request.checkNotModified(eTag);
    assertThat(result).isEmpty();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest)

Example 73 with MockHttpServletRequest

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

the class DefaultServerRequestTests method checkNotModifiedETagWithSeparatorChars.

@ParameterizedHttpMethodTest
void checkNotModifiedETagWithSeparatorChars(String method) {
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest(method, "/", true);
    String eTag = "\"Foo, Bar\"";
    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)

Example 74 with MockHttpServletRequest

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

the class DefaultServerRequestTests method cookies.

@Test
void cookies() {
    Cookie cookie = new Cookie("foo", "bar");
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
    servletRequest.setCookies(cookie);
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    MultiValueMap<String, Cookie> expected = new LinkedMultiValueMap<>();
    expected.add("foo", cookie);
    assertThat(request.cookies()).isEqualTo(expected);
}
Also used : Cookie(jakarta.servlet.http.Cookie) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 75 with MockHttpServletRequest

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

the class DefaultServerRequestTests method method.

@Test
void method() {
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("HEAD", "/", true);
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    assertThat(request.method()).isEqualTo(HttpMethod.HEAD);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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