Java Code Examples for org.springframework.http.MockHttpOutputMessage#getHeaders()

The following examples show how to use org.springframework.http.MockHttpOutputMessage#getHeaders() . 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: ResourceRegionHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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"));
}
 
Example 2
Source File: ResourceRegionHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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."));
}
 
Example 3
Source File: ResourceRegionHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 4
Source File: ResourceRegionHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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"));
}
 
Example 5
Source File: ResourceRegionHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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."));
}
 
Example 6
Source File: ResourceRegionHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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."));
}