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

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

Introduction

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

Prototype

public String getContentAsString() throws UnsupportedEncodingException 

Source Link

Document

Get the content of the response body as a String , using the charset specified for the response by the application, either through HttpServletResponse methods or through a charset parameter on the Content-Type .

Usage

From source file:org.jasig.cas.web.view.Cas10ResponseViewTests.java

public void testFailureView() throws Exception {
    final MockHttpServletResponse response = new MockHttpServletResponse();
    this.view.setSuccessResponse(false);
    this.view.render(this.model, new MockHttpServletRequest(), response);
    assertEquals("no\n\n", response.getContentAsString());
}

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

@Test
public void logfile() throws IOException {
    try {//from www . ja  v  a  2 s .com
        FileCopyUtils.copy("--TEST--".getBytes(), new File("test.log"));
        controller.setLogfile("test.log");

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

        MockHttpServletResponse response = new MockHttpServletResponse();
        controller.invoke(response);
        assertEquals(HttpStatus.OK.value(), response.getStatus());
        assertEquals("--TEST--", response.getContentAsString());
    } finally {
        new File("test.log").delete();
    }
}

From source file:org.openmrs.module.emrapi.web.controller.BaseEmrControllerTest.java

/**
 * Deserializes the JSON response.//w  w w .  j  av  a  2s. c om
 *
 * @param response
 * @param typeReference
 * @return
 * @throws Exception
 */
public <T> T deserialize(MockHttpServletResponse response, final TypeReference<T> typeReference)
        throws Exception {
    return objectMapper.readValue(response.getContentAsString(), typeReference);
}

From source file:org.jasig.cas.web.view.Cas10ResponseViewTests.java

public void testSuccessView() throws Exception {
    final MockHttpServletResponse response = new MockHttpServletResponse();
    this.view.setSuccessResponse(true);
    this.view.render(this.model, new MockHttpServletRequest(), response);
    assertEquals("yes\ntest\n", response.getContentAsString());
}

From source file:org.bahmni.module.bahmnicore.web.v1_0.search.BahmniMainResourceControllerTest.java

/**
 * Deserializes the JSON response./*from   w  w  w .j av  a  2s.  c om*/
 * 
 * @param response
 * @return
 * @throws Exception
 */
public SimpleObject deserialize(MockHttpServletResponse response) throws Exception {
    return new ObjectMapper().readValue(response.getContentAsString(), SimpleObject.class);
}

From source file:com.gu.management.spring.ManagementUrlDiscoveryControllerTest.java

@Test
public void shouldShowSuppliedUrls() throws Exception {
    ManagementUrlDiscoveryController controller = new ManagementUrlDiscoveryController(service);

    MockHttpServletResponse response = new MockHttpServletResponse();

    List<String> urls = asList("/url1");
    when(service.getManagementUrls()).thenReturn(urls);

    controller.handleRequestInternal(null, response);

    assertThat(response.getContentAsString(), containsString("<a href=\"management/url1\">/url1</a>"));
}

From source file:com.xemantic.tadedon.guice.servlet.mock.FakeServletContainerTest.java

/**
 * Tests {@link UriReturningServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
 *
 * @throws IOException if an input or output error is detected during request handling
 * @throws ServletException if container cannot handle request. 
 *//*from  ww w .  ja v  a 2  s.com*/
@Test
public void shouldReturnUriAndAddFooHeader() throws IOException, ServletException {
    // given
    MockHttpServletRequest request = servletContainer.newRequest("GET", "/UriReturningServlet");
    MockHttpServletResponse response = new MockHttpServletResponse();

    // when
    servletContainer.service(request, response);

    // then
    assertThat(response.getContentAsString(), is("/UriReturningServlet"));
    assertThat((String) response.getHeader("Foo"), is("foo"));
}

From source file:com.xemantic.tadedon.guice.servlet.mock.FakeServletContainerTest.java

/**
 * Tests {@link SessionIdReturningServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
 *
 * @throws IOException if an input or output error is detected during request handling
 * @throws ServletException if container cannot handle request. 
 *//*from   w ww . j  av  a  2 s .  c  o m*/
@Test
public void shouldReturnSessionIdAndAddFooHeader() throws IOException, ServletException {
    // given
    servletContainer.newSession("42"); // magic number
    MockHttpServletRequest request = servletContainer.newRequest("GET", "/SessionIdReturningServlet");
    MockHttpServletResponse response = new MockHttpServletResponse();

    // when
    servletContainer.service(request, response);

    // then
    assertThat(response.getContentAsString(), is("42"));
    assertThat((String) response.getHeader("Foo"), is("foo"));
}

From source file:org.jasig.cas.client.authentication.FacesCompatibleAuthenticationRedirectStrategyTests.java

@Test
public void facesPartialResponse() throws Exception {
    final String redirectUrl = "http://www.jasig.org";
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    request.setParameter("javax.faces.partial.ajax", "true");
    this.strategy.redirect(request, response, redirectUrl);
    assertNull(response.getRedirectedUrl());
    assertTrue(response.getContentAsString().contains(redirectUrl));
}

From source file:com.doitnext.http.router.responsehandlers.DefaultSuccessHandlerTest.java

@Test
public void testHandleResponseYes() throws UnsupportedEncodingException {
    DefaultSuccessHandler h = new DefaultSuccessHandler();
    String responseData = "Hidey Ho!!";
    MockHttpServletResponse response = new MockHttpServletResponse();
    boolean handled = h.handleResponse(null, null, response, responseData);
    Assert.assertTrue(handled);//ww w .j  a  va 2s  .c  om

    Assert.assertEquals("\"Hidey Ho!!\"", response.getContentAsString());
}