Search in sources :

Example 96 with MockHttpServletRequest

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

the class CasAuthenticationProviderTests method authenticateAllAuthenticationIsSuccessful.

@Test
public void authenticateAllAuthenticationIsSuccessful() throws Exception {
    String serviceUrl = "https://service/context";
    ServiceAuthenticationDetails details = mock(ServiceAuthenticationDetails.class);
    when(details.getServiceUrl()).thenReturn(serviceUrl);
    TicketValidator validator = mock(TicketValidator.class);
    when(validator.validate(any(String.class), any(String.class))).thenReturn(new AssertionImpl("rod"));
    ServiceProperties serviceProperties = makeServiceProperties();
    serviceProperties.setAuthenticateAllArtifacts(true);
    CasAuthenticationProvider cap = new CasAuthenticationProvider();
    cap.setAuthenticationUserDetailsService(new MockAuthoritiesPopulator());
    cap.setKey("qwerty");
    cap.setTicketValidator(validator);
    cap.setServiceProperties(serviceProperties);
    cap.afterPropertiesSet();
    String ticket = "ST-456";
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(CasAuthenticationFilter.CAS_STATELESS_IDENTIFIER, ticket);
    Authentication result = cap.authenticate(token);
    verify(validator).validate(ticket, serviceProperties.getService());
    serviceProperties.setAuthenticateAllArtifacts(true);
    result = cap.authenticate(token);
    verify(validator, times(2)).validate(ticket, serviceProperties.getService());
    token.setDetails(details);
    result = cap.authenticate(token);
    verify(validator).validate(ticket, serviceUrl);
    serviceProperties.setAuthenticateAllArtifacts(false);
    serviceProperties.setService(null);
    cap.setServiceProperties(serviceProperties);
    cap.afterPropertiesSet();
    result = cap.authenticate(token);
    verify(validator, times(2)).validate(ticket, serviceUrl);
    token.setDetails(new WebAuthenticationDetails(new MockHttpServletRequest()));
    try {
        cap.authenticate(token);
        fail("Expected Exception");
    } catch (IllegalStateException success) {
    }
    cap.setServiceProperties(null);
    cap.afterPropertiesSet();
    try {
        cap.authenticate(token);
        fail("Expected Exception");
    } catch (IllegalStateException success) {
    }
}
Also used : ServiceAuthenticationDetails(org.springframework.security.cas.web.authentication.ServiceAuthenticationDetails) AssertionImpl(org.jasig.cas.client.validation.AssertionImpl) ServiceProperties(org.springframework.security.cas.ServiceProperties) TicketValidator(org.jasig.cas.client.validation.TicketValidator) Authentication(org.springframework.security.core.Authentication) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 97 with MockHttpServletRequest

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

the class DefaultLoginPageGeneratingFilterTests method generatingPageWithOpenIdFilterOnlyIsSuccessFul.

@Test
public void generatingPageWithOpenIdFilterOnlyIsSuccessFul() throws Exception {
    DefaultLoginPageGeneratingFilter filter = new DefaultLoginPageGeneratingFilter(new MockProcessingFilter());
    filter.doFilter(new MockHttpServletRequest("GET", "/login"), new MockHttpServletResponse(), chain);
}
Also used : DefaultLoginPageGeneratingFilter(org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 98 with MockHttpServletRequest

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

the class RequestCacheAwareFilterTests method savedRequestIsRemovedAfterMatch.

@Test
public void savedRequestIsRemovedAfterMatch() throws Exception {
    RequestCacheAwareFilter filter = new RequestCacheAwareFilter();
    HttpSessionRequestCache cache = new HttpSessionRequestCache();
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/destination");
    MockHttpServletResponse response = new MockHttpServletResponse();
    cache.saveRequest(request, response);
    assertThat(request.getSession().getAttribute(HttpSessionRequestCache.SAVED_REQUEST)).isNotNull();
    filter.doFilter(request, response, new MockFilterChain());
    assertThat(request.getSession().getAttribute(HttpSessionRequestCache.SAVED_REQUEST)).isNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 99 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest in project gocd by gocd.

the class AgentRegistrationControllerIntegrationTest method shouldRegisterAgent.

@Test
public void shouldRegisterAgent() throws Exception {
    String uuid = UUID.randomUUID().toString();
    MockHttpServletRequest request = new MockHttpServletRequest();
    controller.agentRequest("hostname", uuid, "sandbox", "100", null, null, null, null, null, null, null, false, request);
    AgentConfig agentConfig = goConfigService.agentByUuid(uuid);
    assertThat(agentConfig.getHostname(), is("hostname"));
}
Also used : AgentConfig(com.thoughtworks.go.config.AgentConfig) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Example 100 with MockHttpServletRequest

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

the class ClassPathBeanDefinitionScannerScopeIntegrationTests method setUp.

@Before
public void setUp() {
    MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
    oldRequestWithSession.setSession(new MockHttpSession());
    this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);
    MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
    newRequestWithSession.setSession(new MockHttpSession());
    this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) Before(org.junit.Before)

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