Search in sources :

Example 91 with MockHttpServletRequest

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

the class ServletAnnotationControllerHandlerMethodTests method requestParamMap.

@PathPatternsParameterizedTest
void requestParamMap(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(RequestParamMapController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/map");
    request.addParameter("key1", "value1");
    request.addParameter("key2", "value21", "value22");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("key1=value1,key2=value21");
    request.setRequestURI("/multiValueMap");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("key1=[value1],key2=[value21,value22]");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 92 with MockHttpServletRequest

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

the class ServletAnnotationControllerHandlerMethodTests method emptyValueMapping.

@PathPatternsParameterizedTest
void emptyValueMapping(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(ControllerWithEmptyValueMapping.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    request.setContextPath("/foo");
    request.setServletPath("");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    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 93 with MockHttpServletRequest

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

the class ServletAnnotationControllerHandlerMethodTests method httpPatch.

@PathPatternsParameterizedTest
void httpPatch(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("PATCH", "/something");
    String requestBody = "Hello world!";
    request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
    request.addHeader("Content-Type", "text/plain; charset=utf-8");
    request.addHeader("Accept", "text/*, */*");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsString()).isEqualTo(requestBody);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 94 with MockHttpServletRequest

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

the class ServletAnnotationControllerHandlerMethodTests method optionalParamMissing.

@PathPatternsParameterizedTest
void optionalParamMissing(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(OptionalParamController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("null-false-null");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 95 with MockHttpServletRequest

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

the class ServletAnnotationControllerHandlerMethodTests method sessionAttributeExposureWithInterface.

@SuppressWarnings("rawtypes")
@PathPatternsParameterizedTest
void sessionAttributeExposureWithInterface(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MySessionAttributesControllerImpl.class, usePathPatterns, wac -> {
        wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(ModelExposingViewResolver.class));
        DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        autoProxyCreator.setBeanFactory(wac.getBeanFactory());
        wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
        wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
    });
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPage");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(request.getAttribute("viewName")).isEqualTo("page1");
    HttpSession session = request.getSession();
    assertThat(session).isNotNull();
    assertThat(session.getAttribute("object1") != null).isTrue();
    assertThat(session.getAttribute("object2") != null).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
    request = new MockHttpServletRequest("POST", "/myPage");
    request.setSession(session);
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(request.getAttribute("viewName")).isEqualTo("page2");
    assertThat(session.getAttribute("object1") != null).isTrue();
    assertThat(session.getAttribute("object2") != null).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
}
Also used : DefaultAdvisorAutoProxyCreator(org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator) SimpleTraceInterceptor(org.springframework.aop.interceptor.SimpleTraceInterceptor) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) ModelMap(org.springframework.ui.ModelMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) 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