Search in sources :

Example 26 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-boot by spring-projects.

the class FreeMarkerAutoConfigurationTests method customTemplateLoaderPath.

@Test
public void customTemplateLoaderPath() throws Exception {
    registerAndRefreshContext("spring.freemarker.templateLoaderPath:classpath:/custom-templates/");
    MockHttpServletResponse response = render("custom");
    String result = response.getContentAsString();
    assertThat(result).contains("custom");
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 27 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-boot by spring-projects.

the class ThymeleafAutoConfigurationTests method createLayoutFromConfigClass.

@Test
public void createLayoutFromConfigClass() throws Exception {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    MockServletContext servletContext = new MockServletContext();
    context.setServletContext(servletContext);
    context.refresh();
    ThymeleafView view = (ThymeleafView) context.getBean(ThymeleafViewResolver.class).resolveViewName("view", Locale.UK);
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
    view.render(Collections.singletonMap("foo", "bar"), request, response);
    String result = response.getContentAsString();
    assertThat(result).contains("<title>Content</title>");
    assertThat(result).contains("<span>bar</span>");
    context.close();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ThymeleafView(org.thymeleaf.spring5.view.ThymeleafView) Matchers.containsString(org.hamcrest.Matchers.containsString) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 28 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-boot by spring-projects.

the class LogFileMvcEndpointTests method notAvailableIfDisabled.

@Test
public void notAvailableIfDisabled() throws Exception {
    this.environment.setProperty("logging.file", this.logFile.getAbsolutePath());
    this.mvc.setEnabled(false);
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.HEAD.name(), "/logfile");
    this.mvc.invoke(request, response);
    assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 29 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-boot by spring-projects.

the class LogFileMvcEndpointTests method availableWithLogFile.

@Test
public void availableWithLogFile() throws Exception {
    this.environment.setProperty("logging.file", this.logFile.getAbsolutePath());
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.HEAD.name(), "/logfile");
    this.mvc.invoke(request, response);
    assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 30 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-boot by spring-projects.

the class LogFileMvcEndpointTests method invokeGetsContentExternalFile.

@Test
public void invokeGetsContentExternalFile() throws Exception {
    this.mvc.setExternalFile(this.logFile);
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(), "/logfile");
    this.mvc.invoke(request, response);
    assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
    assertThat("--TEST--").isEqualTo(response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2376 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2002 Test (org.junit.jupiter.api.Test)1416 lombok.val (lombok.val)946 Test (org.junit.Test)582 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)484 MockServletContext (org.springframework.mock.web.MockServletContext)462 MockRequestContext (org.springframework.webflow.test.MockRequestContext)460 MockFilterChain (org.springframework.mock.web.MockFilterChain)240 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)185 JEEContext (org.pac4j.core.context.JEEContext)159 FilterChain (jakarta.servlet.FilterChain)117 Authentication (org.springframework.security.core.Authentication)116 BeforeEach (org.junit.jupiter.api.BeforeEach)107 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)97 HashMap (java.util.HashMap)84 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)83 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)74 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)67 MockHttpSession (org.springframework.mock.web.MockHttpSession)67