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:com.ns.retailmgr.controller.ShopControllerTest.java

@Test
public void test_getNearByShops_Success() throws Exception {
    List<ShopDetails> mockShopList = new ArrayList<ShopDetails>();
    mockShopList.add(mockShopDetails);/*w  ww.ja v a  2  s .c o m*/

    when(shopService.findShopNearByLatLng(anyString(), anyString())).thenReturn(mockShopList);
    RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/shop").param("customerLatitude", "00")
            .param("customerLongitude", "00").accept(MediaType.APPLICATION_JSON);

    MvcResult result = mockMvc.perform(requestBuilder).andReturn();
    MockHttpServletResponse response = result.getResponse();
    String responseString = response.getContentAsString();
    org.json.JSONArray myObject = new org.json.JSONArray(responseString);

    assertEquals(1, myObject.length());
}

From source file:localdomain.localhost.MyServletTest.java

@Ignore
@Test//  w w  w  . j  a  va  2s. c om
public void testService() throws Exception {
    InputStream credentials = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("credentials.properties");
    if (credentials == null) {
        System.out.println("No credentials found, use default ones");
    } else {
        Properties props = new Properties();
        props.load(credentials);
        System.setProperties(props);
    }

    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse resp = new MockHttpServletResponse();
    MyServlet myServlet = new MyServlet();
    myServlet.service(req, resp);

    System.out.println(resp.getContentAsString());

}

From source file:ar.com.zauber.commons.web.proxy.HttpClientRequestProxyTest.java

/** test */
public final void noNullPathInfo() throws Exception {
    final HttpClientRequestProxy p = new HttpClientRequestProxy(
            new InmutableURLRequestMapper(new InmutableURLResult(new URL("http://www.zauber.com.ar/"))),
            new HttpClient());
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    request.setPathInfo(null);/*w  ww.j a v  a  2s.  c om*/
    assertNotNull(request.getServletPath());
    request.setPathInfo("/test/landing.html");
    final MockHttpServletResponse response = new MockHttpServletResponse();
    p.handleRequestInternal(request, response);
    System.out.println(response.getContentAsString());
}

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

/**
 * Deserializes the JSON response./*ww w . j av a  2 s  . c  o  m*/
 *
 * @param response
 * @param type
 * @return
 * @throws Exception
 */
public <T> T deserialize(MockHttpServletResponse response, Class<T> type) throws Exception {
    return objectMapper.readValue(response.getContentAsString(), type);
}

From source file:fi.okm.mpass.shibboleth.profile.impl.WriteMonitoringResultTest.java

@Test
public void testNoContext() throws Exception {
    action.execute(src);// ww  w.  ja va2 s . c o m
    MockHttpServletResponse httpResponse = (MockHttpServletResponse) action.getHttpServletResponse();
    Assert.assertEquals(httpResponse.getContentAsString(), WriteMonitoringResult.ERROR_MSG_NO_CONTEXT);
}

From source file:fi.okm.mpass.shibboleth.profile.impl.WriteMonitoringResultTest.java

@Test
public void testNoResults() throws Exception {
    prc.addSubcontext(new MonitoringResultContext());
    action.execute(src);/*from w  ww.  ja va 2 s .c o m*/
    MockHttpServletResponse httpResponse = (MockHttpServletResponse) action.getHttpServletResponse();
    Assert.assertEquals(httpResponse.getContentAsString(), WriteMonitoringResult.ERROR_MSG_NO_RESULTS);
}

From source file:fi.okm.mpass.shibboleth.profile.impl.WriteMonitoringResultTest.java

@Test
public void testWithoutError() throws Exception {
    final MonitoringResultContext monitoringCtx = new MonitoringResultContext();
    monitoringCtx.addResult(initSeqResult(null));
    prc.addSubcontext(monitoringCtx);// w ww .  j av a2 s. c om
    action.execute(src);
    MockHttpServletResponse httpResponse = (MockHttpServletResponse) action.getHttpServletResponse();
    Assert.assertTrue(httpResponse.getContentAsString().contains("OK:"));
}

From source file:net.sf.json.spring.web.servlet.view.JsonViewTest.java

private String toJsScript(MockHttpServletResponse response) throws Exception {
    // looks like MockHttpServletResponse is broken
    String json = response.getContentAsString();
    if (json.startsWith("[{") && !json.endsWith("}]")) {
        json += "}]";
    }/* w  ww . ja va 2s . com*/
    if (json.startsWith("{") && !json.endsWith("}")) {
        json += "}";
    }
    if (json.startsWith("[") && !json.endsWith("]")) {
        json += "]";
    }
    return "var json = eval('(" + json + ")');";
}

From source file:fi.okm.mpass.shibboleth.profile.impl.WriteMonitoringResultTest.java

@Test
public void testWithError() throws Exception {
    final MonitoringResultContext monitoringCtx = new MonitoringResultContext();
    monitoringCtx.addResult(initSeqResult(errorMessage));
    prc.addSubcontext(monitoringCtx);/* w  ww  .  j  ava 2s.c  o  m*/
    action.execute(src);
    MockHttpServletResponse httpResponse = (MockHttpServletResponse) action.getHttpServletResponse();
    Assert.assertEquals(httpResponse.getContentAsString(), errorMessage);
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8.CohortController1_8Test.java

@Test
public void getCohort_shouldGetADefaultRepresentationInXML() throws Exception {

    MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid());
    req.addHeader("Accept", "application/xml");
    MockHttpServletResponse result = handle(req);

    String xml = result.getContentAsString();

    Assert.assertEquals(getUuid(), evaluateXPath(xml, "//uuid"));
}