Search in sources :

Example 31 with MockHttpServletRequest

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

the class RequestMappingInfoTests method matchProducesCondition.

@Test
void matchProducesCondition() {
    MockHttpServletRequest request = PathPatternsTestUtils.initRequest("GET", "/foo", false);
    request.addHeader("Accept", "text/plain");
    RequestMappingInfo info = RequestMappingInfo.paths("/foo").produces("text/plain").build();
    RequestMappingInfo match = info.getMatchingCondition(request);
    assertThat(match).isNotNull();
    info = RequestMappingInfo.paths("/foo").produces("application/xml").build();
    match = info.getMatchingCondition(request);
    assertThat(match).isNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 32 with MockHttpServletRequest

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

the class RequestMappingInfoTests method preFlightRequest.

@Test
void preFlightRequest() {
    MockHttpServletRequest request = PathPatternsTestUtils.initRequest("OPTIONS", "/foo", false);
    request.addHeader(HttpHeaders.ORIGIN, "https://domain.com");
    request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST");
    RequestMappingInfo info = RequestMappingInfo.paths("/foo").methods(RequestMethod.POST).build();
    RequestMappingInfo match = info.getMatchingCondition(request);
    assertThat(match).isNotNull();
    info = RequestMappingInfo.paths("/foo").methods(RequestMethod.OPTIONS).build();
    match = info.getMatchingCondition(request);
    assertThat(match).as("Pre-flight should match the ACCESS_CONTROL_REQUEST_METHOD").isNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 33 with MockHttpServletRequest

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

the class PatternsRequestConditionTests method matchTrailingSlash.

@Test
void matchTrailingSlash() {
    MockHttpServletRequest request = initRequest("/foo/");
    PatternsRequestCondition condition = new PatternsRequestCondition("/foo");
    PatternsRequestCondition match = condition.getMatchingCondition(request);
    assertThat(match).isNotNull();
    assertThat(match.getPatterns().iterator().next()).as("Should match by default").isEqualTo("/foo/");
    condition = new PatternsRequestCondition(new String[] { "/foo" }, true, null);
    match = condition.getMatchingCondition(request);
    assertThat(match).isNotNull();
    assertThat(match.getPatterns().iterator().next()).as("Trailing slash should be insensitive to useSuffixPatternMatch settings (SPR-6164, SPR-5636)").isEqualTo("/foo/");
    condition = new PatternsRequestCondition(new String[] { "/foo" }, false, null);
    match = condition.getMatchingCondition(request);
    assertThat(match).isNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 34 with MockHttpServletRequest

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

the class PatternsRequestConditionTests method matchSuffixPatternUsingFileExtensions2.

@Test
@SuppressWarnings("deprecation")
void matchSuffixPatternUsingFileExtensions2() {
    PatternsRequestCondition condition1 = new PatternsRequestCondition(new String[] { "/prefix" }, null, null, true, false, Collections.singletonList("json"));
    PatternsRequestCondition condition2 = new PatternsRequestCondition(new String[] { "/suffix" }, null, null, true, false, null);
    PatternsRequestCondition combined = condition1.combine(condition2);
    MockHttpServletRequest request = initRequest("/prefix/suffix.json");
    PatternsRequestCondition match = combined.getMatchingCondition(request);
    assertThat(match).isNotNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 35 with MockHttpServletRequest

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

the class ProducesRequestConditionTests method matchNegatedWithoutAcceptHeader.

@Test
public void matchNegatedWithoutAcceptHeader() {
    ProducesRequestCondition condition = new ProducesRequestCondition("!text/plain");
    assertThat(condition.getMatchingCondition(new MockHttpServletRequest())).isNotNull();
    assertThat(condition.getProducibleMediaTypes()).isEqualTo(Collections.emptySet());
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

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