Search in sources :

Example 1 with MockHttpServletResponse

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

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

use of org.springframework.mock.web.MockHttpServletResponse in project head by mifos.

the class RedirectionControllerTest method testHandleRequest.

@Test
public void testHandleRequest() throws ServletException, IOException {
    String expectedPageToRedirectTo = "foopage";
    RedirectionController controller = new RedirectionController();
    controller.setViewToRedirectTo(expectedPageToRedirectTo);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(mockRequest, mockResponse);
    Assert.assertEquals(expectedPageToRedirectTo, modelAndView.getViewName());
    Assert.assertNotNull(modelAndView.getModel());
    Map<String, Object> modelMap = (Map<String, Object>) modelAndView.getModel().get("model");
    Object response = modelMap.get("response");
    Assert.assertNotNull(response);
    Assert.assertEquals(MockHttpServletResponse.class, response.getClass());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project pinpoint by naver.

the class SpringWebMvcIT method testRequest.

@Test
public void testRequest() throws Exception {
    MockServletConfig config = new MockServletConfig();
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    config.addInitParameter("contextConfigLocation", "classpath:spring-web-test.xml");
    req.setMethod("GET");
    req.setRequestURI("/");
    req.setRemoteAddr("1.2.3.4");
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.init(config);
    servlet.service(req, res);
    Method method = FrameworkServlet.class.getDeclaredMethod("doGet", HttpServletRequest.class, HttpServletResponse.class);
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    verifier.verifyTrace(Expectations.event(SPRING_MVC, method));
    verifier.verifyTraceCount(0);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.MockServletConfig) Method(java.lang.reflect.Method) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 5 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project opennms by OpenNMS.

the class RequisitionRestServiceIT method testDuplicateNodes.

@Test
public void testDuplicateNodes() throws Exception {
    MockLogAppender.setupLogging(true, "DEBUG");
    String req = "<model-import xmlns=\"http://xmlns.opennms.org/xsd/config/model-import\" date-stamp=\"2006-03-09T00:03:09\" foreign-source=\"test\">" + "<node node-label=\"a\" foreign-id=\"a\" />" + "<node node-label=\"b\" foreign-id=\"c\" />" + "<node node-label=\"c\" foreign-id=\"c\" />" + "</model-import>";
    final MockHttpServletResponse response = sendPost("/requisitions", req, 400, null);
    assertTrue("response should say 'c' has duplicates", response.getContentAsString().contains("Duplicate nodes found on foreign source test: c (2 found)"));
}
Also used : MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2358 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2001 Test (org.junit.jupiter.api.Test)1413 lombok.val (lombok.val)946 Test (org.junit.Test)567 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 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)67 MockHttpSession (org.springframework.mock.web.MockHttpSession)67 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)66