Search in sources :

Example 41 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project cas by apereo.

the class RegisteredServiceSimpleFormControllerTests method verifyAddRegexRegisteredService.

@Test
public void verifyAddRegexRegisteredService() throws Exception {
    final RegexRegisteredService svc = new RegexRegisteredService();
    svc.setDescription(DESCRIPTION);
    svc.setServiceId("^serviceId");
    svc.setName(NAME);
    svc.setId(1000);
    svc.setEvaluationOrder(1000);
    final RegisteredServiceEditBean.ServiceData data = registeredServiceFactory.createServiceData(svc);
    this.controller.saveService(new MockHttpServletRequest(), new MockHttpServletResponse(), data, mock(BindingResult.class));
    final Collection<RegisteredService> services = this.manager.getAllServices();
    assertEquals(1, services.size());
    this.manager.getAllServices().forEach(rs -> assertTrue(rs instanceof RegexRegisteredService));
}
Also used : BindingResult(org.springframework.validation.BindingResult) RegexRegisteredService(org.apereo.cas.services.RegexRegisteredService) AbstractRegisteredService(org.apereo.cas.services.AbstractRegisteredService) RegisteredService(org.apereo.cas.services.RegisteredService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RegexRegisteredService(org.apereo.cas.services.RegexRegisteredService) RegisteredServiceEditBean(org.apereo.cas.mgmt.services.web.beans.RegisteredServiceEditBean) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 42 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project cas by apereo.

the class RegisteredServiceSimpleFormControllerTests method verifyAddMockRegisteredService.

@Test
public void verifyAddMockRegisteredService() throws Exception {
    this.registeredServiceFactory = new DefaultRegisteredServiceFactory(new DefaultAccessStrategyMapper(), policyMapper, new DefaultProxyPolicyMapper(), new MockRegisteredServiceMapper(), new DefaultUsernameAttributeProviderMapper(), Collections.singletonList(new AttributeFormDataPopulator(this.repository)));
    this.controller = new RegisteredServiceSimpleFormController(this.manager, this.registeredServiceFactory);
    final MockRegisteredService svc = new MockRegisteredService();
    svc.setDescription(DESCRIPTION);
    svc.setServiceId("^serviceId");
    svc.setName(NAME);
    svc.setId(1000);
    svc.setEvaluationOrder(1000);
    final RegisteredServiceEditBean.ServiceData data = registeredServiceFactory.createServiceData(svc);
    this.controller.saveService(new MockHttpServletRequest(), new MockHttpServletResponse(), data, mock(BindingResult.class));
    final Collection<RegisteredService> services = this.manager.getAllServices();
    assertEquals(1, services.size());
    this.manager.getAllServices().forEach(rs -> assertTrue(rs instanceof MockRegisteredService));
}
Also used : BindingResult(org.springframework.validation.BindingResult) RegexRegisteredService(org.apereo.cas.services.RegexRegisteredService) AbstractRegisteredService(org.apereo.cas.services.AbstractRegisteredService) RegisteredService(org.apereo.cas.services.RegisteredService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultAccessStrategyMapper(org.apereo.cas.mgmt.services.web.factory.DefaultAccessStrategyMapper) RegisteredServiceEditBean(org.apereo.cas.mgmt.services.web.beans.RegisteredServiceEditBean) DefaultProxyPolicyMapper(org.apereo.cas.mgmt.services.web.factory.DefaultProxyPolicyMapper) RegisteredServiceSimpleFormController(org.apereo.cas.mgmt.services.web.RegisteredServiceSimpleFormController) DefaultRegisteredServiceFactory(org.apereo.cas.mgmt.services.web.factory.DefaultRegisteredServiceFactory) DefaultUsernameAttributeProviderMapper(org.apereo.cas.mgmt.services.web.factory.DefaultUsernameAttributeProviderMapper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) AttributeFormDataPopulator(org.apereo.cas.mgmt.services.web.factory.AttributeFormDataPopulator) Test(org.junit.Test)

Example 43 with MockHttpServletResponse

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

the class OAuth2ClientContextFilterTests method testRedirectUri.

public void testRedirectUri(String redirect, Map<String, String> params, String result) throws Exception {
    OAuth2ClientContextFilter filter = new OAuth2ClientContextFilter();
    RedirectStrategy redirectStrategy = Mockito.mock(RedirectStrategy.class);
    filter.setRedirectStrategy(redirectStrategy);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    UserRedirectRequiredException exception = new UserRedirectRequiredException(redirect, params);
    filter.redirectUser(exception, request, response);
    Mockito.verify(redirectStrategy).sendRedirect(request, response, result);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RedirectStrategy(org.springframework.security.web.RedirectStrategy) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)

Example 44 with MockHttpServletResponse

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

the class OpenIDAuthenticationFilterTests method testFilterOperation.

@Test
public void testFilterOperation() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setServletPath(REQUEST_PATH);
    req.setRequestURI(REQUEST_PATH);
    req.setServerPort(8080);
    MockHttpServletResponse response = new MockHttpServletResponse();
    req.setParameter("openid_identifier", " " + CLAIMED_IDENTITY_URL);
    req.setRemoteHost("www.example.com");
    filter.setConsumer(new MockOpenIDConsumer() {

        public String beginConsumption(HttpServletRequest req, String claimedIdentity, String returnToUrl, String realm) throws OpenIDConsumerException {
            assertThat(claimedIdentity).isEqualTo(CLAIMED_IDENTITY_URL);
            assertThat(returnToUrl).isEqualTo(DEFAULT_TARGET_URL);
            assertThat(realm).isEqualTo("http://localhost:8080/");
            return REDIRECT_URL;
        }
    });
    FilterChain fc = mock(FilterChain.class);
    filter.doFilter(req, response, fc);
    assertThat(response.getRedirectedUrl()).isEqualTo(REDIRECT_URL);
    // Filter chain shouldn't proceed
    verify(fc, never()).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 45 with MockHttpServletResponse

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

the class ClientCredentialsTokenEndpointFilterTests method testFailedAuthentication.

@Test(expected = BadCredentialsException.class)
public void testFailedAuthentication() throws Exception {
    filter.setAuthenticationManager(authenticationManager);
    filter.afterPropertiesSet();
    filter.attemptAuthentication(new MockHttpServletRequest(), new MockHttpServletResponse());
}
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