Search in sources :

Example 26 with MockHttpServletRequest

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method variableNamesWithUrlExtension.

// gh-13187
@PathPatternsParameterizedTest
void variableNamesWithUrlExtension(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(VariableNamesController.class, usePathPatterns, wac -> {
        if (!usePathPatterns) {
            RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
            mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
            wac.registerBeanDefinition("handlerMapping", mappingDef);
        }
    });
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test/foo.json");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo(!usePathPatterns ? "foo-foo" : "foo-foo.json");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 27 with MockHttpServletRequest

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

the class EncodedResourceResolverTests method resolveGzippedWithVersion.

@Test
public void resolveGzippedWithVersion(GzippedFiles gzippedFiles) {
    gzippedFiles.create("foo.css");
    String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Accept-Encoding", "gzip");
    Resource resolved = this.resolver.resolveResource(request, file, this.locations);
    assertThat(resolved.getDescription()).isEqualTo(getResource("foo.css.gz").getDescription());
    assertThat(resolved.getFilename()).isEqualTo(getResource("foo.css").getFilename());
    boolean condition = resolved instanceof HttpResource;
    assertThat(condition).isTrue();
}
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 28 with MockHttpServletRequest

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

the class EncodedResourceResolverTests method resolveGzipped.

@Test
public void resolveGzipped(GzippedFiles gzippedFiles) {
    String file = "js/foo.js";
    gzippedFiles.create(file);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Accept-Encoding", "gzip");
    Resource actual = this.resolver.resolveResource(request, file, this.locations);
    assertThat(actual.getDescription()).isEqualTo(getResource(file + ".gz").getDescription());
    assertThat(actual.getFilename()).isEqualTo(getResource(file).getFilename());
    boolean condition = actual instanceof HttpResource;
    assertThat(condition).isTrue();
    HttpHeaders headers = ((HttpResource) actual).getResponseHeaders();
    assertThat(headers.getFirst(HttpHeaders.CONTENT_ENCODING)).isEqualTo("gzip");
    assertThat(headers.getFirst(HttpHeaders.VARY)).isEqualTo("Accept-Encoding");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) 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 29 with MockHttpServletRequest

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

the class EncodedResourceResolverTests method resolveFromCacheWithEncodingVariants.

@Test
public void resolveFromCacheWithEncodingVariants(GzippedFiles gzippedFiles) {
    // 1. Resolve, and cache .gz variant
    String file = "js/foo.js";
    gzippedFiles.create(file);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/js/foo.js");
    request.addHeader("Accept-Encoding", "gzip");
    Resource resolved = this.resolver.resolveResource(request, file, this.locations);
    assertThat(resolved.getDescription()).isEqualTo(getResource(file + ".gz").getDescription());
    assertThat(resolved.getFilename()).isEqualTo(getResource(file).getFilename());
    boolean condition = resolved instanceof HttpResource;
    assertThat(condition).isTrue();
    // 2. Resolve unencoded resource
    request = new MockHttpServletRequest("GET", "/js/foo.js");
    resolved = this.resolver.resolveResource(request, file, this.locations);
    assertThat(resolved.getDescription()).isEqualTo(getResource(file).getDescription());
    assertThat(resolved.getFilename()).isEqualTo(getResource(file).getFilename());
    boolean condition1 = resolved instanceof HttpResource;
    assertThat(condition1).isFalse();
}
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 30 with MockHttpServletRequest

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

the class ResourceHttpRequestHandlerIntegrationTests method initRequest.

private MockHttpServletRequest initRequest(String path) {
    path = UriUtils.encodePath(path, StandardCharsets.UTF_8);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", path);
    request.setCharacterEncoding(StandardCharsets.UTF_8.name());
    return request;
}
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