use of org.springframework.http.MockHttpOutputMessage in project spring-framework by spring-projects.
the class ResourceRegionHttpMessageConverterTests method shouldWritePartialContentByteRangeNoEnd.
@Test
public void shouldWritePartialContentByteRangeNoEnd() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource body = new ClassPathResource("byterangeresource.txt", getClass());
ResourceRegion region = HttpRange.createByteRange(7).toResourceRegion(body);
converter.write(region, MediaType.TEXT_PLAIN, outputMessage);
HttpHeaders headers = outputMessage.getHeaders();
assertThat(headers.getContentType(), is(MediaType.TEXT_PLAIN));
assertThat(headers.getContentLength(), is(32L));
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).size(), is(1));
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).get(0), is("bytes 7-38/39"));
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Framework test resource content."));
}
use of org.springframework.http.MockHttpOutputMessage in project spring-framework by spring-projects.
the class ResourceRegionHttpMessageConverterTests method shouldWritePartialContentByteRange.
@Test
public void shouldWritePartialContentByteRange() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource body = new ClassPathResource("byterangeresource.txt", getClass());
ResourceRegion region = HttpRange.createByteRange(0, 5).toResourceRegion(body);
converter.write(region, MediaType.TEXT_PLAIN, outputMessage);
HttpHeaders headers = outputMessage.getHeaders();
assertThat(headers.getContentType(), is(MediaType.TEXT_PLAIN));
assertThat(headers.getContentLength(), is(6L));
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).size(), is(1));
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).get(0), is("bytes 0-5/39"));
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Spring"));
}
use of org.springframework.http.MockHttpOutputMessage in project spring-framework by spring-projects.
the class MappingJackson2HttpMessageConverterTests method jsonView.
@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.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\"")));
}
use of org.springframework.http.MockHttpOutputMessage in project spring-framework by spring-projects.
the class MappingJackson2HttpMessageConverterTests method writeSubType.
// SPR-13318
@Test
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"));
}
use of org.springframework.http.MockHttpOutputMessage in project spring-framework by spring-projects.
the class MappingJackson2HttpMessageConverterTests method writeSubTypeList.
// SPR-13318
@Test
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"));
}
Aggregations