Search in sources :

Example 16 with MockHttpServletRequest

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

the class ViewMethodReturnValueHandlerTests method setup.

@BeforeEach
public void setup() {
    this.handler = new ViewMethodReturnValueHandler();
    this.mavContainer = new ModelAndViewContainer();
    this.webRequest = new ServletWebRequest(new MockHttpServletRequest());
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 17 with MockHttpServletRequest

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

the class DefaultHandlerExceptionResolverTests method handleNoHandlerFoundException.

@Test
public void handleNoHandlerFoundException() throws Exception {
    ServletServerHttpRequest req = new ServletServerHttpRequest(new MockHttpServletRequest("GET", "/resource"));
    NoHandlerFoundException ex = new NoHandlerFoundException(req.getMethod().name(), req.getServletRequest().getRequestURI(), req.getHeaders());
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertThat(mav).as("No ModelAndView returned").isNotNull();
    assertThat(mav.isEmpty()).as("No Empty ModelAndView returned").isTrue();
    assertThat(response.getStatus()).as("Invalid status code").isEqualTo(404);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) Test(org.junit.jupiter.api.Test)

Example 18 with MockHttpServletRequest

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

the class FlashMapManagerTests method saveOutputFlashMapDecodeParameters.

// SPR-9657, SPR-11504
@Test
public void saveOutputFlashMapDecodeParameters() throws Exception {
    FlashMap flashMap = new FlashMap();
    flashMap.put("key", "value");
    flashMap.setTargetRequestPath("/path");
    flashMap.addTargetRequestParam("param", "%D0%90%D0%90");
    flashMap.addTargetRequestParam("param", "%D0%91%D0%91");
    flashMap.addTargetRequestParam("param", "%D0%92%D0%92");
    flashMap.addTargetRequestParam("%3A%2F%3F%23%5B%5D%40", "value");
    this.request.setCharacterEncoding("UTF-8");
    this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);
    MockHttpServletRequest requestAfterRedirect = new MockHttpServletRequest("GET", "/path");
    requestAfterRedirect.setQueryString("param=%D0%90%D0%90&param=%D0%91%D0%91&param=%D0%92%D0%92&%3A%2F%3F%23%5B%5D%40=value");
    requestAfterRedirect.addParameter("param", "\u0410\u0410");
    requestAfterRedirect.addParameter("param", "\u0411\u0411");
    requestAfterRedirect.addParameter("param", "\u0412\u0412");
    requestAfterRedirect.addParameter(":/?#[]@", "value");
    flashMap = this.flashMapManager.retrieveAndUpdate(requestAfterRedirect, new MockHttpServletResponse());
    assertThatFlashMap(flashMap).isNotNull();
    assertThat(flashMap.size()).isEqualTo(1);
    assertThat(flashMap.get("key")).isEqualTo("value");
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 19 with MockHttpServletRequest

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

the class CachingResourceResolverTests method resolveResourceAcceptEncodingInCacheKey.

@Test
public void resolveResourceAcceptEncodingInCacheKey(GzippedFiles gzippedFiles) throws IOException {
    String file = "bar.css";
    gzippedFiles.create(file);
    // 1. Resolve plain resource
    MockHttpServletRequest request = new MockHttpServletRequest("GET", file);
    Resource expected = this.chain.resolveResource(request, file, this.locations);
    String cacheKey = resourceKey(file);
    assertThat(this.cache.get(cacheKey).get()).isSameAs(expected);
    // 2. Resolve with Accept-Encoding
    request = new MockHttpServletRequest("GET", file);
    request.addHeader("Accept-Encoding", "gzip ; a=b  , deflate ,  br  ; c=d ");
    expected = this.chain.resolveResource(request, file, this.locations);
    cacheKey = resourceKey(file + "+encoding=br,gzip");
    assertThat(this.cache.get(cacheKey).get()).isSameAs(expected);
    // 3. Resolve with Accept-Encoding but no matching codings
    request = new MockHttpServletRequest("GET", file);
    request.addHeader("Accept-Encoding", "deflate");
    expected = this.chain.resolveResource(request, file, this.locations);
    cacheKey = resourceKey(file);
    assertThat(this.cache.get(cacheKey).get()).isSameAs(expected);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) Test(org.junit.jupiter.api.Test)

Example 20 with MockHttpServletRequest

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

the class CachingResourceResolverTests method resolveResourceMatchingEncoding.

@Test
public void resolveResourceMatchingEncoding() {
    Resource resource = Mockito.mock(Resource.class);
    Resource gzipped = Mockito.mock(Resource.class);
    this.cache.put(resourceKey("bar.css"), resource);
    this.cache.put(resourceKey("bar.css+encoding=gzip"), gzipped);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "bar.css");
    assertThat(this.chain.resolveResource(request, "bar.css", this.locations)).isSameAs(resource);
    request = new MockHttpServletRequest("GET", "bar.css");
    request.addHeader("Accept-Encoding", "gzip");
    assertThat(this.chain.resolveResource(request, "bar.css", this.locations)).isSameAs(gzipped);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) 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