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.google.api.server.spi.response.CollectionResponseTest.java

@Test
public void testCollectionResponse() throws IOException {
    MockHttpServletResponse servletResponse = new MockHttpServletResponse();
    ServletResponseResultWriter writer = new ServletResponseResultWriter(servletResponse, null);
    writer.write(getBeans(2));/* ww w  .j a v  a  2  s . c  om*/

    ObjectNode json = new ObjectMapper().readValue(servletResponse.getContentAsString(), ObjectNode.class);
    assertThat(json.path("items")).hasSize(2);
    assertThat(json.path("nextPageToken").asText()).isEqualTo("next");
}

From source file:de.fau.amos4.test.integration.EmployeeTest.java

@Test
public void testEmployeeFormHasTheNeededFields() throws Exception {
    // Get the employee/edit page content using a valid Employee Token.
    String ValidToken = this.employeeRepository.findAll().iterator().next().getToken();
    final MockHttpServletResponse response = mockMvc
            .perform(get("/employee/token/submit").param("token", ValidToken)).andExpect(status().isOk())
            .andReturn().getResponse();//from  w ww  .j a v  a 2 s .  com
    String Content = response.getContentAsString();

    // Get all the needed field names using an empty Employee and CheckDataInput class.
    Employee emptyEmployee = new Employee();
    CheckDataInput cdi = new CheckDataInput();
    List<String> ExpectedFields = cdi.listEmptyFields(emptyEmployee); // Will contain all the annotated empty fields.

    // Check each field, that the content contains the corresponding input element.
    for (String ExpectedField : ExpectedFields) {
        System.out.println("Checking field... " + ExpectedField);
        Boolean FieldIsFound = Content.contains(ExpectedField);
        Assert.assertTrue(FieldIsFound);
        System.out.println("[OK] Field found: " + ExpectedField);
    }
}

From source file:com.fiveamsolutions.nci.commons.kmplot.KMPlotServiceTest.java

@Test
public void testGeneratePlot() throws Exception {
    KMPlot plot = getPlot();/*from  w w  w.  j  a v  a 2 s .  com*/
    MockHttpServletResponse response = new MockHttpServletResponse();
    plot.writePlotImage(response.getOutputStream());
    assertNotNull(response.getContentAsString());
    assertTrue(StringUtils.isNotBlank(response.getContentAsString()));
}

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

@Test
public void testHandleResponseYes() throws IOException {
    DefaultErrorHandler h = new DefaultErrorHandler();
    Exception responseData = new IllegalArgumentException("Hidey Ho!!", new NumberFormatException("3r3"));
    MockHttpServletResponse response = new MockHttpServletResponse();
    boolean handled = h.handleResponse(null, null, response, responseData);
    Assert.assertTrue(handled);//from ww  w  .j a v  a 2  s  . c om

    String content = response.getContentAsString();
    String expectedContent = IOUtils.toString(this.getClass().getResourceAsStream("error1.dat"), "UTF-8");
    Assert.assertEquals(expectedContent, content);
}

From source file:be.dnsbelgium.rdap.spring.security.RDAPErrorHandlerTest.java

@Test
public void testHandlerNullDescription() throws IOException, ServletException {
    RDAPErrorHandler errorHandler = new RDAPErrorHandler();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RDAPErrorException exception = new RDAPErrorException(499, "title", (String[]) null);
    errorHandler.handle(request, response, exception);
    Assert.assertEquals("{\"errorCode\":499,\"title\":\"title\"}", response.getContentAsString());
}

From source file:com.google.api.server.spi.handlers.ApiProxyHandlerTest.java

private void testWithServletPath(String servletPath) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("localhost");
    request.setServerPort(8080);/*from www .  j  a v  a2  s . co m*/
    request.setServletPath(servletPath);
    MockHttpServletResponse response = new MockHttpServletResponse();
    ApiProxyHandler handler = new ApiProxyHandler();
    EndpointsContext context = new EndpointsContext("GET", "static/proxy.html", request, response);

    handler.handle(context);

    assertThat(response.getContentAsString()).contains(servletPath);
}

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

/**
 * Verifies the {@link ErrorDTO} contents produced by the action.
 * @param action/*from   www  .  j a v  a  2  s  . com*/
 * @param code
 * @throws UnsupportedEncodingException
 * @throws ComponentInitializationException
 */
protected void verifyErrorDTO(BuildMetaRestResponse action, int code)
        throws UnsupportedEncodingException, ComponentInitializationException {
    action.initialize();
    ActionTestingSupport.assertProceedEvent(action.execute((RequestContext) null));
    final MockHttpServletResponse httpResponse = (MockHttpServletResponse) action.getHttpServletResponse();
    Assert.assertNotNull(httpResponse.getContentAsString());
    final Gson gson = new Gson();
    final ErrorDTO errorDTO = gson.fromJson(httpResponse.getContentAsString(), ErrorDTO.class);
    Assert.assertEquals(httpResponse.getStatus(), code);
    Assert.assertNotNull(errorDTO);
    Assert.assertEquals(errorDTO.getCode(), code);
}

From source file:org.openmrs.module.idgen.web.controller.IdentifierSourceControllerTest.java

@Test
public void exportIdentifiers_shouldReturnJson() throws Exception {
    Mockito.stub(iss.generateIdentifiers(Mockito.any(IdentifierSource.class), Mockito.any(Integer.class),
            Mockito.any(String.class))).toReturn(Arrays.asList("1", "2", "3"));

    SequentialIdentifierGenerator generator = new SequentialIdentifierGenerator();

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    controller.exportIdentifiers(null, mockRequest, mockResponse, generator, 3, "Mirebalais", null, null);

    Assert.assertEquals("{\"identifiers\":[\"1\",\"2\",\"3\"]}", mockResponse.getContentAsString());
}