Search in sources :

Example 46 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest 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 47 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest 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 48 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest 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)

Example 49 with MockHttpServletRequest

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

the class MvcEndpointSecurityInterceptorTests method setup.

@Before
public void setup() throws Exception {
    this.roles = Arrays.asList("SUPER_HERO");
    this.securityInterceptor = new MvcEndpointSecurityInterceptor(true, this.roles);
    this.endpoint = new TestEndpoint("a");
    this.mvcEndpoint = new TestMvcEndpoint(this.endpoint);
    this.handlerMethod = new HandlerMethod(this.mvcEndpoint, "invoke");
    this.servletContext = new MockServletContext();
    this.request = new MockHttpServletRequest(this.servletContext);
    this.response = mock(HttpServletResponse.class);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerMethod(org.springframework.web.method.HandlerMethod) MockServletContext(org.springframework.mock.web.MockServletContext) Before(org.junit.Before)

Example 50 with MockHttpServletRequest

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

the class NoSpringSecurityHealthMvcEndpointIntegrationTests method getRequestPostProcessor.

private RequestPostProcessor getRequestPostProcessor() {
    return new RequestPostProcessor() {

        @Override
        public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
            Principal principal = mock(Principal.class);
            request.setUserPrincipal(principal);
            return request;
        }
    };
}
Also used : RequestPostProcessor(org.springframework.test.web.servlet.request.RequestPostProcessor) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Principal(java.security.Principal)

Aggregations

MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3144 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1989 Test (org.junit.jupiter.api.Test)1907 lombok.val (lombok.val)1124 Test (org.junit.Test)752 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)482 MockServletContext (org.springframework.mock.web.MockServletContext)471 MockRequestContext (org.springframework.webflow.test.MockRequestContext)468 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)245 MockFilterChain (org.springframework.mock.web.MockFilterChain)238 JEEContext (org.pac4j.core.context.JEEContext)156 Authentication (org.springframework.security.core.Authentication)144 BeforeEach (org.junit.jupiter.api.BeforeEach)132 HashMap (java.util.HashMap)123 FilterChain (jakarta.servlet.FilterChain)117 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)108 MockHttpSession (org.springframework.mock.web.MockHttpSession)98 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)96 Before (org.junit.Before)82 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)78