Example usage for org.springframework.http MockHttpOutputMessage getBodyAsString

List of usage examples for org.springframework.http MockHttpOutputMessage getBodyAsString

Introduction

In this page you can find the example usage for org.springframework.http MockHttpOutputMessage getBodyAsString.

Prototype

public String getBodyAsString(Charset charset) 

Source Link

Usage

From source file:org.springframework.http.converter.FormHttpMessageConverterTests.java

@Test
public void writeForm() throws IOException {
    MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    body.set("name 1", "value 1");
    body.add("name 2", "value 2+1");
    body.add("name 2", "value 2+2");
    body.add("name 3", null);
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    this.converter.write(body, MediaType.APPLICATION_FORM_URLENCODED, outputMessage);

    assertEquals("Invalid result", "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3",
            outputMessage.getBodyAsString(StandardCharsets.UTF_8));
    assertEquals("Invalid content-type", new MediaType("application", "x-www-form-urlencoded"),
            outputMessage.getHeaders().getContentType());
    assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length,
            outputMessage.getHeaders().getContentLength());
}

From source file:org.springframework.http.converter.json.MappingJackson2HttpMessageConverterTests.java

public void testPrettyPrint() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    PrettyPrintBean bean = new PrettyPrintBean();
    bean.setName("Jason");

    getConverter().setPrettyPrint(true);
    getConverter().writeInternal(bean, outputMessage);
    String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));

    assertEquals("{" + NEWLINE_SYSTEM_PROPERTY + "  \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}",
            result);//  w  ww  .  j  av  a  2s  . co m
}

From source file:org.springframework.http.converter.json.MappingJackson2HttpMessageConverterTests.java

public void testPrefixJson() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    getConverter().setPrefixJson(true);/* w ww .j a  v  a 2  s.  c  o m*/
    getConverter().writeInternal("foo", outputMessage);

    assertEquals("{} && \"foo\"", outputMessage.getBodyAsString(Charset.forName("UTF-8")));
}

From source file:org.springframework.http.converter.json.MappingJackson2HttpMessageConverterTests.java

public void testPrefixJsonCustom() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    getConverter().setJsonPrefix(")]}',");
    getConverter().writeInternal("foo", outputMessage);

    assertEquals(")]}',\"foo\"", outputMessage.getBodyAsString(Charset.forName("UTF-8")));
}