Example usage for org.springframework.mock.web MockHttpServletResponse MockHttpServletResponse

List of usage examples for org.springframework.mock.web MockHttpServletResponse MockHttpServletResponse

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse MockHttpServletResponse.

Prototype

MockHttpServletResponse

Source Link

Usage

From source file:org.openmrs.web.controller.PortletControllerTest.java

/**
 * Convenience method to get the "model" from the controller's handleRequest method
 * //  ww  w .  j  a v  a2  s.c om
 * @param patientId the patient id to fetch
 * @return the Map from string to object of everything in the generated "model"
 * @throws Exception
 */
private Map<String, Object> getModelFromController(Integer patientId) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
    HttpServletResponse response = new MockHttpServletResponse();

    request.setAttribute(WebConstants.INIT_REQ_UNIQUE_ID, "1");
    request.getSession().setAttribute(WebConstants.OPENMRS_PORTLET_LAST_REQ_ID, "0");
    request.setAttribute("javax.servlet.include.servlet_path", "testPortlet");
    request.setAttribute("org.openmrs.portlet.parameters", new HashMap());
    request.setAttribute("org.openmrs.portlet.patientId", patientId);

    ModelAndView modelAndView = new PortletController().handleRequest(request, response);

    return (Map<String, Object>) modelAndView.getModel().get("model");
}

From source file:de.codecentric.boot.admin.actuate.LogfileMvcEndpointTest.java

@Test
public void logfile_noFile() throws IOException {
    controller.setLogfile("does_not_exist.log");

    assertEquals(HttpStatus.NOT_FOUND, controller.available().getStatusCode());

    MockHttpServletResponse response = new MockHttpServletResponse();
    controller.invoke(response);/*  w w  w. j  a va 2 s  . co  m*/
    assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatus());
}

From source file:org.openmrs.contrib.metadatarepository.webapp.filter.LocaleFilterTest.java

public void testSetLocaleInSessionWhenSessionIsNull() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("locale", "es");

    MockHttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(request, response, new MockFilterChain());

    // no session, should result in null
    assertNull(request.getSession().getAttribute(Constants.PREFERRED_LOCALE_KEY));
    // thread locale should always have it, regardless of session
    assertNotNull(LocaleContextHolder.getLocale());
}

From source file:com.carlos.projects.billing.ui.controllers.ShowComponentsControllerTest.java

@Test
public void shouldForwardToNewDocumentPage() throws Exception {
    //given//from ww  w .j  av  a 2 s.c  om
    ShowComponentsController controller = new ShowComponentsController();
    controller.setViewName("showComponents");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    MockHttpServletResponse response = new MockHttpServletResponse();

    //when
    ModelAndView modelAndView = controller.handleRequest(request, response);

    //then
    assertThat(modelAndView.getViewName(), is("showComponents"));
}

From source file:org.ngrinder.common.util.FileDownloadUtilTest.java

@Test
public void testDownloadNotExistFile() throws IOException {
    File downFile = null;//from w  ww . ja v  a 2s  . co  m
    HttpServletResponse resp = new MockHttpServletResponse();
    boolean result = FileDownloadUtil.downloadFile(resp, downFile);
    assertThat(result, is(false));

    downFile = new File("Not-existed-file");
    result = FileDownloadUtil.downloadFile(resp, downFile);
    assertThat(result, is(false));
}

From source file:org.ngrinder.common.util.FileDownloadUtilsTest.java

@Test
public void testDownloadNotExistFile() throws IOException {
    File downFile = null;/*from  w  w  w .j a v  a  2  s  . c om*/
    HttpServletResponse resp = new MockHttpServletResponse();
    boolean result = FileDownloadUtils.downloadFile(resp, downFile);
    assertThat(result, is(false));

    downFile = new File("Not-existed-file");
    result = FileDownloadUtils.downloadFile(resp, downFile);
    assertThat(result, is(false));
}

From source file:mailjimp.webhook.TestWebHookSecurityInterceptor.java

@Test
public void testPreHandle() throws Exception {
    MyTestWebHookAdapter adapter = new MyTestWebHookAdapter();
    WebHookSecurityInterceptor interceptor = new WebHookSecurityInterceptor();
    // set the adapter like spring would do.
    Field field = WebHookSecurityInterceptor.class.getDeclaredField("webHookAdapter");
    field.setAccessible(true);//  w ww .ja va 2  s .  c  o  m
    field.set(interceptor, adapter);
    field.setAccessible(false);
    // call the interceptor.
    interceptor.preHandle(new MockHttpServletRequest(), new MockHttpServletResponse(), null);
    // all we have to check is if the adapter was called.
    assertTrue(adapter.wasCalled("isValidRequest"));
}

From source file:com.carlos.projects.billing.ui.controllers.NewDocumentControllerTest.java

@Test
public void shouldForwardToNewDocumentPage() throws Exception {
    //given/*w w  w  .ja v  a  2  s. co m*/
    NewDocumentController controller = new NewDocumentController();
    controller.setViewName("newDocument");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    MockHttpServletResponse response = new MockHttpServletResponse();

    //when
    ModelAndView modelAndView = controller.handleRequest(request, response);

    //then
    assertThat(modelAndView.getViewName(), is("newDocument"));
}

From source file:org.ngrinder.security.PluggablePreAuthFilterTest.java

@Test
public void test() throws IOException, ServletException {
    PluggablePreAuthFilter filter = new PluggablePreAuthFilter();
    ReflectionTestUtils.setField(filter, "pluginManager", pluginManager);

    filter.init();/*from w  w w. j  a  va  2s  .  co  m*/
    filter.onPluginEnabled(null);
    filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(),
            new PassThroughFilterChain(new FreemarkerServlet()));
    filter.onPluginDisabled(null);
}

From source file:org.cloudfoundry.identity.uaa.authentication.AuthzAuthenticationFilterTests.java

@Test
public void authenticatesValidUser() throws Exception {

    String msg = "{ \"username\":\"marissa\", \"password\":\"koala\"}";

    AuthenticationManager am = mock(AuthenticationManager.class);
    Authentication result = mock(Authentication.class);
    when(am.authenticate(any(AuthzAuthenticationRequest.class))).thenReturn(result);
    AuthzAuthenticationFilter filter = new AuthzAuthenticationFilter(am);

    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/oauth/authorize");
    request.setParameter("credentials", msg);
    MockHttpServletResponse response = new MockHttpServletResponse();

    filter.doFilter(request, response, new MockFilterChain());

}