Search in sources :

Example 96 with MockServletContext

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

the class KotlinScriptTemplateTests method setup.

@BeforeEach
public void setup() {
    this.webAppContext = mock(WebApplicationContext.class);
    this.servletContext = new MockServletContext();
    this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
}
Also used : MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 97 with MockServletContext

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

the class ScriptTemplateViewTests method contentType.

// SPR-13379
@Test
public void contentType() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    this.wac.setServletContext(servletContext);
    this.wac.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    MockHttpServletResponse response = new MockHttpServletResponse();
    Map<String, Object> model = new HashMap<>();
    this.view.setEngine(mock(InvocableScriptEngine.class));
    this.view.setRenderFunction("render");
    this.view.setResourceLoaderPath("classpath:org/springframework/web/servlet/view/script/");
    this.view.setUrl("empty.txt");
    this.view.setApplicationContext(this.wac);
    this.view.render(model, request, response);
    assertThat(response.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo((MediaType.TEXT_HTML_VALUE + ";charset=" + StandardCharsets.UTF_8));
    response = new MockHttpServletResponse();
    this.view.setContentType(MediaType.TEXT_PLAIN_VALUE);
    this.view.render(model, request, response);
    assertThat(response.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo((MediaType.TEXT_PLAIN_VALUE + ";charset=" + StandardCharsets.UTF_8));
    response = new MockHttpServletResponse();
    this.view.setCharset(StandardCharsets.ISO_8859_1);
    this.view.render(model, request, response);
    assertThat(response.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo((MediaType.TEXT_PLAIN_VALUE + ";charset=" + StandardCharsets.ISO_8859_1));
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 98 with MockServletContext

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

the class ScriptTemplateViewTests method missingTemplate.

@Test
public void missingTemplate() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    this.wac.setServletContext(servletContext);
    this.wac.refresh();
    this.view.setResourceLoaderPath("classpath:org/springframework/web/servlet/view/script/");
    this.view.setUrl("missing.txt");
    this.view.setEngine(mock(InvocableScriptEngine.class));
    this.configurer.setRenderFunction("render");
    this.view.setApplicationContext(this.wac);
    assertThat(this.view.checkResource(Locale.ENGLISH)).isFalse();
}
Also used : MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 99 with MockServletContext

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

the class InternalResourceViewTests method forward.

@Test
public void forward() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myservlet/handler.do");
    request.setContextPath("/mycontext");
    request.setServletPath("/myservlet");
    request.setPathInfo(";mypathinfo");
    request.setQueryString("?param1=value1");
    view.setUrl(url);
    view.setServletContext(new MockServletContext() {

        @Override
        public int getMinorVersion() {
            return 4;
        }
    });
    view.render(model, request, response);
    assertThat(response.getForwardedUrl()).isEqualTo(url);
    model.forEach((key, value) -> assertThat(request.getAttribute(key)).as("Values for model key '" + key + "' must match").isEqualTo(value));
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 100 with MockServletContext

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

the class ResourceBundleViewResolverTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    rb.setBasename(PROPS_FILE);
    rb.setCache(getCache());
    rb.setDefaultParentView("testParent");
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    // This will be propagated to views, so we need it.
    rb.setApplicationContext(wac);
}
Also used : MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)138 Test (org.junit.jupiter.api.Test)112 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)44 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)38 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)24 ServletContext (jakarta.servlet.ServletContext)22 ServletContextEvent (jakarta.servlet.ServletContextEvent)21 BeforeEach (org.junit.jupiter.api.BeforeEach)17 WebApplicationContext (org.springframework.web.context.WebApplicationContext)17 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)14 MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)13 Resource (org.springframework.core.io.Resource)12 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)12 HashMap (java.util.HashMap)11 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 TestBean (org.springframework.beans.testfixture.beans.TestBean)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)9 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)7