Java Code Examples for org.springframework.http.MockHttpOutputMessage#getBodyAsString()
The following examples show how to use
org.springframework.http.MockHttpOutputMessage#getBodyAsString() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MappingJackson2HttpMessageConverterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test // SPR-13318 public void writeSubTypeList() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); List<MyBean> beans = new ArrayList<>(); MyBean foo = new MyBean(); foo.setString("Foo"); foo.setNumber(42); beans.add(foo); MyBean bar = new MyBean(); bar.setString("Bar"); bar.setNumber(123); beans.add(bar); ParameterizedTypeReference<List<MyInterface>> typeReference = new ParameterizedTypeReference<List<MyInterface>>() {}; this.converter.writeInternal(beans, typeReference.getType(), outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertTrue(result.contains("\"string\":\"Foo\"")); assertTrue(result.contains("\"number\":42")); assertTrue(result.contains("\"string\":\"Bar\"")); assertTrue(result.contains("\"number\":123")); }
Example 2
Source File: MappingJackson2XmlHttpMessageConverterTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void jsonView() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); JacksonViewBean bean = new JacksonViewBean(); bean.setWithView1("with"); bean.setWithView2("with"); bean.setWithoutView("without"); MappingJacksonValue jacksonValue = new MappingJacksonValue(bean); jacksonValue.setSerializationView(MyJacksonView1.class); this.converter.write(jacksonValue, null, outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertThat(result, containsString("<withView1>with</withView1>")); assertThat(result, not(containsString("<withView2>with</withView2>"))); assertThat(result, not(containsString("<withoutView>without</withoutView>"))); }
Example 3
Source File: MappingJackson2HttpMessageConverterTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void write() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MyBean body = new MyBean(); body.setString("Foo"); body.setNumber(42); body.setFraction(42F); body.setArray(new String[] {"Foo", "Bar"}); body.setBool(true); body.setBytes(new byte[] {0x1, 0x2}); converter.write(body, null, outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertTrue(result.contains("\"string\":\"Foo\"")); assertTrue(result.contains("\"number\":42")); assertTrue(result.contains("fraction\":42.0")); assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]")); assertTrue(result.contains("\"bool\":true")); assertTrue(result.contains("\"bytes\":\"AQI=\"")); assertEquals("Invalid content-type", new MediaType("application", "json", StandardCharsets.UTF_8), outputMessage.getHeaders().getContentType()); }
Example 4
Source File: MappingJackson2HttpMessageConverterTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void classLevelJsonView() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); JacksonViewBean bean = new JacksonViewBean(); bean.setWithView1("with"); bean.setWithView2("with"); bean.setWithoutView("without"); MappingJacksonValue jacksonValue = new MappingJacksonValue(bean); jacksonValue.setSerializationView(MyJacksonView3.class); this.converter.writeInternal(jacksonValue, null, outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertThat(result, not(containsString("\"withView1\":\"with\""))); assertThat(result, not(containsString("\"withView2\":\"with\""))); assertThat(result, containsString("\"withoutView\":\"without\"")); }
Example 5
Source File: MappingJackson2HttpMessageConverterTests.java From java-technology-stack with MIT License | 6 votes |
@Test // SPR-13318 public void writeSubTypeList() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); List<MyBean> beans = new ArrayList<>(); MyBean foo = new MyBean(); foo.setString("Foo"); foo.setNumber(42); beans.add(foo); MyBean bar = new MyBean(); bar.setString("Bar"); bar.setNumber(123); beans.add(bar); ParameterizedTypeReference<List<MyInterface>> typeReference = new ParameterizedTypeReference<List<MyInterface>>() {}; this.converter.writeInternal(beans, typeReference.getType(), outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertTrue(result.contains("\"string\":\"Foo\"")); assertTrue(result.contains("\"number\":42")); assertTrue(result.contains("\"string\":\"Bar\"")); assertTrue(result.contains("\"number\":123")); }
Example 6
Source File: GsonHttpMessageConverterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void writeWithBaseType() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MyBean body = new MyBean(); body.setString("Foo"); body.setNumber(42); body.setFraction(42F); body.setArray(new String[] {"Foo", "Bar"}); body.setBool(true); body.setBytes(new byte[] {0x1, 0x2}); this.converter.write(body, MyBase.class, null, outputMessage); Charset utf8 = StandardCharsets.UTF_8; String result = outputMessage.getBodyAsString(utf8); assertTrue(result.contains("\"string\":\"Foo\"")); assertTrue(result.contains("\"number\":42")); assertTrue(result.contains("fraction\":42.0")); assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]")); assertTrue(result.contains("\"bool\":true")); assertTrue(result.contains("\"bytes\":[1,2]")); assertEquals("Invalid content-type", new MediaType("application", "json", utf8), outputMessage.getHeaders().getContentType()); }
Example 7
Source File: MappingJackson2HttpMessageConverterTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void jsonpAndJsonView() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); JacksonViewBean bean = new JacksonViewBean(); bean.setWithView1("with"); bean.setWithView2("with"); bean.setWithoutView("without"); MappingJacksonValue jacksonValue = new MappingJacksonValue(bean); jacksonValue.setSerializationView(MyJacksonView1.class); jacksonValue.setJsonpFunction("callback"); this.converter.writeInternal(jacksonValue, null, outputMessage); String result = outputMessage.getBodyAsString(Charset.forName("UTF-8")); assertThat(result, startsWith("/**/callback(")); assertThat(result, endsWith(");")); assertThat(result, containsString("\"withView1\":\"with\"")); assertThat(result, not(containsString("\"withView2\":\"with\""))); assertThat(result, not(containsString("\"withoutView\":\"without\""))); }
Example 8
Source File: MappingJackson2HttpMessageConverterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void classLevelJsonView() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); JacksonViewBean bean = new JacksonViewBean(); bean.setWithView1("with"); bean.setWithView2("with"); bean.setWithoutView("without"); MappingJacksonValue jacksonValue = new MappingJacksonValue(bean); jacksonValue.setSerializationView(MyJacksonView3.class); this.converter.writeInternal(jacksonValue, null, outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertThat(result, not(containsString("\"withView1\":\"with\""))); assertThat(result, not(containsString("\"withView2\":\"with\""))); assertThat(result, containsString("\"withoutView\":\"without\"")); }
Example 9
Source File: MappingJackson2HttpMessageConverterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void fieldLevelJsonView() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); JacksonViewBean bean = new JacksonViewBean(); bean.setWithView1("with"); bean.setWithView2("with"); bean.setWithoutView("without"); MappingJacksonValue jacksonValue = new MappingJacksonValue(bean); jacksonValue.setSerializationView(MyJacksonView1.class); this.converter.writeInternal(jacksonValue, null, outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertThat(result, containsString("\"withView1\":\"with\"")); assertThat(result, not(containsString("\"withView2\":\"with\""))); assertThat(result, not(containsString("\"withoutView\":\"without\""))); }
Example 10
Source File: JsonbHttpMessageConverterTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void writeWithBaseType() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MyBean body = new MyBean(); body.setString("Foo"); body.setNumber(42); body.setFraction(42F); body.setArray(new String[] {"Foo", "Bar"}); body.setBool(true); body.setBytes(new byte[] {0x1, 0x2}); this.converter.write(body, MyBase.class, null, outputMessage); Charset utf8 = StandardCharsets.UTF_8; String result = outputMessage.getBodyAsString(utf8); assertTrue(result.contains("\"string\":\"Foo\"")); assertTrue(result.contains("\"number\":42")); assertTrue(result.contains("fraction\":42.0")); assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]")); assertTrue(result.contains("\"bool\":true")); assertTrue(result.contains("\"bytes\":[1,2]")); assertEquals("Invalid content-type", new MediaType("application", "json", utf8), outputMessage.getHeaders().getContentType()); }
Example 11
Source File: GsonHttpMessageConverterTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void write() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MyBean body = new MyBean(); body.setString("Foo"); body.setNumber(42); body.setFraction(42F); body.setArray(new String[] {"Foo", "Bar"}); body.setBool(true); body.setBytes(new byte[] {0x1, 0x2}); this.converter.write(body, null, outputMessage); Charset utf8 = StandardCharsets.UTF_8; String result = outputMessage.getBodyAsString(utf8); assertTrue(result.contains("\"string\":\"Foo\"")); assertTrue(result.contains("\"number\":42")); assertTrue(result.contains("fraction\":42.0")); assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]")); assertTrue(result.contains("\"bool\":true")); assertTrue(result.contains("\"bytes\":[1,2]")); assertEquals("Invalid content-type", new MediaType("application", "json", utf8), outputMessage.getHeaders().getContentType()); }
Example 12
Source File: MappingJackson2HttpMessageConverterTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void fieldLevelJsonView() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); JacksonViewBean bean = new JacksonViewBean(); bean.setWithView1("with"); bean.setWithView2("with"); bean.setWithoutView("without"); MappingJacksonValue jacksonValue = new MappingJacksonValue(bean); jacksonValue.setSerializationView(MyJacksonView1.class); this.converter.writeInternal(jacksonValue, null, outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertThat(result, containsString("\"withView1\":\"with\"")); assertThat(result, not(containsString("\"withView2\":\"with\""))); assertThat(result, not(containsString("\"withoutView\":\"without\""))); }
Example 13
Source File: GsonHttpMessageConverterTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void write() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MyBean body = new MyBean(); body.setString("Foo"); body.setNumber(42); body.setFraction(42F); body.setArray(new String[]{"Foo", "Bar"}); body.setBool(true); body.setBytes(new byte[]{0x1, 0x2}); this.converter.write(body, null, outputMessage); Charset utf8 = Charset.forName("UTF-8"); String result = outputMessage.getBodyAsString(utf8); assertTrue(result.contains("\"string\":\"Foo\"")); assertTrue(result.contains("\"number\":42")); assertTrue(result.contains("fraction\":42.0")); assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]")); assertTrue(result.contains("\"bool\":true")); assertTrue(result.contains("\"bytes\":[1,2]")); assertEquals("Invalid content-type", new MediaType("application", "json", utf8), outputMessage.getHeaders().getContentType()); }
Example 14
Source File: ProtobufHttpMessageConverterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void writeJsonWithJavaFormat() throws IOException { this.converter = new ProtobufHttpMessageConverter( new ProtobufHttpMessageConverter.ProtobufJavaFormatSupport(), this.extensionRegistry); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MediaType contentType = MediaType.APPLICATION_JSON_UTF8; this.converter.write(this.testMsg, contentType, outputMessage); assertEquals(contentType, outputMessage.getHeaders().getContentType()); final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8")); assertFalse("body is empty", body.isEmpty()); Msg.Builder builder = Msg.newBuilder(); JsonFormat.parser().merge(body, builder); assertEquals(this.testMsg, builder.build()); assertNull(outputMessage.getHeaders().getFirst( ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER)); assertNull(outputMessage.getHeaders().getFirst( ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER)); }
Example 15
Source File: ProtobufHttpMessageConverterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void writeJsonWithGoogleProtobuf() throws IOException { this.converter = new ProtobufHttpMessageConverter( new ProtobufHttpMessageConverter.ProtobufJavaUtilSupport(null, null), this.extensionRegistry); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MediaType contentType = MediaType.APPLICATION_JSON; this.converter.write(this.testMsg, contentType, outputMessage); assertEquals(contentType, outputMessage.getHeaders().getContentType()); final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8")); assertFalse("body is empty", body.isEmpty()); Msg.Builder builder = Msg.newBuilder(); JsonFormat.parser().merge(body, builder); assertEquals(this.testMsg, builder.build()); assertNull(outputMessage.getHeaders().getFirst( ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER)); assertNull(outputMessage.getHeaders().getFirst( ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER)); }
Example 16
Source File: MappingJackson2HttpMessageConverterTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void prettyPrint() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); PrettyPrintBean bean = new PrettyPrintBean(); bean.setName("Jason"); this.converter.setPrettyPrint(true); this.converter.writeInternal(bean, null, outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertEquals("{" + NEWLINE_SYSTEM_PROPERTY + " \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}", result); }
Example 17
Source File: ResourceRegionHttpMessageConverterTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void partialContentMultipleByteRanges() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Resource body = new ClassPathResource("byterangeresource.txt", getClass()); List<HttpRange> rangeList = HttpRange.parseRanges("bytes=0-5,7-15,17-20,22-38"); List<ResourceRegion> regions = new ArrayList<>(); for(HttpRange range : rangeList) { regions.add(range.toResourceRegion(body)); } converter.write(regions, MediaType.TEXT_PLAIN, outputMessage); HttpHeaders headers = outputMessage.getHeaders(); assertThat(headers.getContentType().toString(), Matchers.startsWith("multipart/byteranges;boundary=")); String boundary = "--" + headers.getContentType().toString().substring(30); String content = outputMessage.getBodyAsString(StandardCharsets.UTF_8); String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true); assertThat(ranges[0], is(boundary)); assertThat(ranges[1], is("Content-Type: text/plain")); assertThat(ranges[2], is("Content-Range: bytes 0-5/39")); assertThat(ranges[3], is("Spring")); assertThat(ranges[4], is(boundary)); assertThat(ranges[5], is("Content-Type: text/plain")); assertThat(ranges[6], is("Content-Range: bytes 7-15/39")); assertThat(ranges[7], is("Framework")); assertThat(ranges[8], is(boundary)); assertThat(ranges[9], is("Content-Type: text/plain")); assertThat(ranges[10], is("Content-Range: bytes 17-20/39")); assertThat(ranges[11], is("test")); assertThat(ranges[12], is(boundary)); assertThat(ranges[13], is("Content-Type: text/plain")); assertThat(ranges[14], is("Content-Range: bytes 22-38/39")); assertThat(ranges[15], is("resource content.")); }
Example 18
Source File: MappingJackson2HttpMessageConverterTests.java From java-technology-stack with MIT License | 5 votes |
@Test // SPR-13318 public void writeSubType() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MyBean bean = new MyBean(); bean.setString("Foo"); bean.setNumber(42); this.converter.writeInternal(bean, MyInterface.class, outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertTrue(result.contains("\"string\":\"Foo\"")); assertTrue(result.contains("\"number\":42")); }
Example 19
Source File: MappingJackson2HttpMessageConverterTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test // SPR-13318 public void writeSubType() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MyBean bean = new MyBean(); bean.setString("Foo"); bean.setNumber(42); this.converter.writeInternal(bean, MyInterface.class, outputMessage); String result = outputMessage.getBodyAsString(Charset.forName("UTF-8")); assertTrue(result.contains("\"string\":\"Foo\"")); assertTrue(result.contains("\"number\":42")); }
Example 20
Source File: MappingJackson2HttpMessageConverterTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void prettyPrint() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); PrettyPrintBean bean = new PrettyPrintBean(); bean.setName("Jason"); this.converter.setPrettyPrint(true); this.converter.writeInternal(bean, null, outputMessage); String result = outputMessage.getBodyAsString(Charset.forName("UTF-8")); assertEquals("{" + NEWLINE_SYSTEM_PROPERTY + " \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}", result); }