Search in sources :

Example 11 with MockHttpServletRequest

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method typeConversionError.

@PathPatternsParameterizedTest
void typeConversionError(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(SimpleUriTemplateController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).as("Invalid response status code").isEqualTo(HttpServletResponse.SC_BAD_REQUEST);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 12 with MockHttpServletRequest

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method crud.

@PathPatternsParameterizedTest
void crud(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(CrudController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("list");
    request = new MockHttpServletRequest("GET", "/hotels/");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("list");
    request = new MockHttpServletRequest("POST", "/hotels");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("create");
    request = new MockHttpServletRequest("GET", "/hotels/42");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("show-42");
    request = new MockHttpServletRequest("GET", "/hotels/42/");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("show-42");
    request = new MockHttpServletRequest("PUT", "/hotels/42");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("createOrUpdate-42");
    request = new MockHttpServletRequest("DELETE", "/hotels/42");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("remove-42");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 13 with MockHttpServletRequest

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method pathVarsInModel.

@PathPatternsParameterizedTest
void pathVarsInModel(boolean usePathPatterns) throws Exception {
    final Map<String, Object> pathVars = new HashMap<>();
    pathVars.put("hotel", "42");
    pathVars.put("booking", 21);
    pathVars.put("other", "other");
    WebApplicationContext wac = initDispatcherServlet(ViewRenderingController.class, usePathPatterns, context -> {
        RootBeanDefinition beanDef = new RootBeanDefinition(ModelValidatingViewResolver.class);
        beanDef.getConstructorArgumentValues().addGenericArgumentValue(pathVars);
        context.registerBeanDefinition("viewResolver", beanDef);
    });
    HttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42;q=1,2/bookings/21-other;q=3;r=R");
    getServlet().service(request, new MockHttpServletResponse());
    ModelValidatingViewResolver resolver = wac.getBean(ModelValidatingViewResolver.class);
    assertThat(resolver.validatedAttrCount).isEqualTo(3);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) WebApplicationContext(org.springframework.web.context.WebApplicationContext) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 14 with MockHttpServletRequest

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method literalMappingWithPathParams.

// gh-25864
@PathPatternsParameterizedTest
void literalMappingWithPathParams(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MultipleUriTemplateController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/data");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsString()).isEqualTo("test");
    if (!usePathPatterns) {
        request = new MockHttpServletRequest("GET", "/data;foo=bar");
        response = new MockHttpServletResponse();
        getServlet().service(request, response);
        assertThat(response.getStatus()).isEqualTo(404);
    }
    request = new MockHttpServletRequest("GET", "/data;jsessionid=123");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsString()).isEqualTo("test");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 15 with MockHttpServletRequest

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method simple.

@PathPatternsParameterizedTest
void simple(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(SimpleUriTemplateController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/42");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("test-42-7");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

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