Search in sources :

Example 11 with MockHttpOutputMessage

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."));
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) HttpHeaders(org.springframework.http.HttpHeaders) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 12 with MockHttpOutputMessage

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"));
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) HttpHeaders(org.springframework.http.HttpHeaders) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 13 with MockHttpOutputMessage

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\"")));
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) Test(org.junit.Test)

Example 14 with MockHttpOutputMessage

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"));
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) Test(org.junit.Test)

Example 15 with MockHttpOutputMessage

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"));
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)49 Test (org.junit.Test)48 MediaType (org.springframework.http.MediaType)24 ClassPathResource (org.springframework.core.io.ClassPathResource)12 Resource (org.springframework.core.io.Resource)11 ByteArrayResource (org.springframework.core.io.ByteArrayResource)5 InputStreamResource (org.springframework.core.io.InputStreamResource)5 HttpHeaders (org.springframework.http.HttpHeaders)5 ArrayList (java.util.ArrayList)4 ResourceRegion (org.springframework.core.io.support.ResourceRegion)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 StringReader (java.io.StringReader)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)3 DifferenceEvaluator (org.xmlunit.diff.DifferenceEvaluator)3 Feed (com.rometools.rome.feed.atom.Feed)2 Channel (com.rometools.rome.feed.rss.Channel)2 Item (com.rometools.rome.feed.rss.Item)2 BufferedImage (java.awt.image.BufferedImage)2 InputStream (java.io.InputStream)2 Result (javax.xml.transform.Result)2