Search in sources :

Example 36 with MockServletContext

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

the class AuthenticationViaFormActionTests method verifyFailedAuthenticationWithNoService.

@Test
public void verifyFailedAuthenticationWithNoService() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockRequestContext context = new MockRequestContext();
    request.addParameter(USERNAME_PARAM, TEST);
    request.addParameter(PASSWORD_PARAM, "test2");
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    final Credential c = CoreAuthenticationTestUtils.getCredentialsWithDifferentUsernameAndPassword();
    putCredentialInRequestScope(context, c);
    context.getRequestScope().put("org.springframework.validation.BindException.credentials", new BindException(c, "credential"));
    assertEquals(CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE, this.action.execute(context).getId());
}
Also used : Credential(org.apereo.cas.authentication.Credential) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) BindException(org.springframework.validation.BindException) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 37 with MockServletContext

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

the class GenerateServiceTicketActionTests method verifyTicketGrantingTicketNotTgtButGateway.

@Test
public void verifyTicketGrantingTicketNotTgtButGateway() throws Exception {
    final MockRequestContext context = new MockRequestContext();
    context.getFlowScope().put(SERVICE_PARAM, RegisteredServiceTestUtils.getService());
    final MockHttpServletRequest request = new MockHttpServletRequest();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, SERVICE_PARAM);
    request.addParameter(CasProtocolConstants.PARAMETER_GATEWAY, "true");
    final TicketGrantingTicket tgt = mock(TicketGrantingTicket.class);
    when(tgt.getId()).thenReturn("bleh");
    WebUtils.putTicketGrantingTicketInScopes(context, tgt);
    assertEquals(CasWebflowConstants.STATE_ID_GATEWAY, this.action.execute(context).getId());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 38 with MockServletContext

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

the class SendTicketGrantingTicketActionTests method verifyTgtToSet.

@Test
public void verifyTgtToSet() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr(LOCALHOST_IP);
    request.setLocalAddr(LOCALHOST_IP);
    ClientInfoHolder.setClientInfo(new ClientInfo(request));
    final MockHttpServletResponse response = new MockHttpServletResponse();
    request.addHeader("User-Agent", "Test");
    final TicketGrantingTicket tgt = mock(TicketGrantingTicket.class);
    when(tgt.getId()).thenReturn(TEST_STRING);
    WebUtils.putTicketGrantingTicketInScopes(this.context, tgt);
    this.context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    assertEquals(SUCCESS, this.action.execute(this.context).getId());
    request.setCookies(response.getCookies());
    assertEquals(tgt.getId(), this.ticketGrantingTicketCookieGenerator.retrieveCookieValue(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 39 with MockServletContext

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

the class SendTicketGrantingTicketActionTests method verifySsoSessionCookieOnServiceSsoDisallowed.

@Test
public void verifySsoSessionCookieOnServiceSsoDisallowed() throws Exception {
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final WebApplicationService svc = mock(WebApplicationService.class);
    when(svc.getId()).thenReturn("TestSsoFalse");
    final TicketGrantingTicket tgt = mock(TicketGrantingTicket.class);
    when(tgt.getId()).thenReturn(TEST_STRING);
    request.setCookies(new Cookie("TGT", "test5"));
    WebUtils.putTicketGrantingTicketInScopes(this.context, tgt);
    this.context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    this.context.getFlowScope().put("service", svc);
    final SendTicketGrantingTicketAction action = new SendTicketGrantingTicketAction(centralAuthenticationService, servicesManager, ticketGrantingTicketCookieGenerator, false);
    assertEquals(SUCCESS, action.execute(this.context).getId());
    assertEquals(0, response.getCookies().length);
}
Also used : Cookie(javax.servlet.http.Cookie) WebApplicationService(org.apereo.cas.authentication.principal.WebApplicationService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 40 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project JessMA by ldcsaa.

the class TestRest method main.

public static void main(String[] args) throws Exception {
    MockServletContext servletContext = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setCharacterEncoding(GeneralHelper.DEFAULT_ENCODING);
    RestContext context = new RestContext(RequestType.PUT, "/aaa/111;12|34|56,伤神小怪兽", RenderType.XML);
    MyAction action = new MyAction();
    action.setServletContext(servletContext);
    action.setRequest(request);
    action.setResponse(response);
    action.setRequestAttribute(Rest.Constant.REQ_ATTR_REST_CONTEXT, context);
    action.execute();
    response.getWriter().println("\n-------------------------");
    context = new RestContext(RequestType.PUT, "/大家好/12|34|56", RenderType.JSON);
    action.setRequestAttribute(Rest.Constant.REQ_ATTR_REST_CONTEXT, context);
    action.execute();
    response.getWriter().println("\n-------------------------");
    context = new RestContext(RequestType.GET, "/173/edit", RenderType.XML);
    action.setRequestAttribute(Rest.Constant.REQ_ATTR_REST_CONTEXT, context);
    action.execute();
    String result = response.getContentAsString();
    System.out.println(result);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RestContext(org.jessma.ext.rest.RestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Aggregations

MockServletContext (org.springframework.mock.web.MockServletContext)186 Test (org.junit.Test)135 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)82 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)80 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)74 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)54 MockRequestContext (org.springframework.webflow.test.MockRequestContext)46 Event (org.springframework.webflow.execution.Event)18 Before (org.junit.Before)16 EventFactorySupport (org.springframework.webflow.action.EventFactorySupport)14 MockMvc (org.springframework.test.web.servlet.MockMvc)13 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)12 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8 MockFilterChain (org.springframework.mock.web.MockFilterChain)8 MockHttpSession (org.springframework.mock.web.MockHttpSession)8 Map (java.util.Map)7 Credential (org.apereo.cas.authentication.Credential)7 Cookie (javax.servlet.http.Cookie)6 FilterChainProxy (org.springframework.security.web.FilterChainProxy)6