ResourceHttpMessageWriterTests.java 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.http.codec;

import java.nio.charset.StandardCharsets;
import java.util.Collections;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import org.springframework.core.ResolvableType;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRange;
import org.springframework.http.MediaType;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
import org.springframework.tests.TestSubscriber;
import org.springframework.util.MimeTypeUtils;
44 45 46

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
47 48 49

/**
 * Unit tests for {@link ResourceHttpMessageWriter}.
50
 *
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
 * @author Brian Clozel
 */
public class ResourceHttpMessageWriterTests {

	private ResourceHttpMessageWriter writer = new ResourceHttpMessageWriter();

	private MockServerHttpRequest request = new MockServerHttpRequest();

	private MockServerHttpResponse response = new MockServerHttpResponse();

	private Resource resource;

	@Rule
	public ExpectedException expectedException = ExpectedException.none();


	@Before
	public void setUp() throws Exception {
		String content = "Spring Framework test resource content.";
		this.resource = new ByteArrayResource(content.getBytes(StandardCharsets.UTF_8));
	}

73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
	@Test
	public void writableMediaTypes() throws Exception {
		assertThat(this.writer.getWritableMediaTypes(),
				containsInAnyOrder(MimeTypeUtils.APPLICATION_OCTET_STREAM, MimeTypeUtils.ALL));
	}

	@Test
	public void shouldWriteResource() throws Exception {
		TestSubscriber.subscribe(this.writer.write(Mono.just(resource), null, ResolvableType.forClass(Resource.class),
				MediaType.TEXT_PLAIN, this.request, this.response, Collections.emptyMap())).assertComplete();

		assertThat(this.response.getHeaders().getContentType(), is(MediaType.TEXT_PLAIN));
		assertThat(this.response.getHeaders().getContentLength(), is(39L));

		Mono<String> result = reduceToString(this.response.getBody(), this.response.bufferFactory());
		TestSubscriber.subscribe(result).assertComplete().assertValues("Spring Framework test resource content.");
	}

	@Test
	public void shouldWriteResourceRange() throws Exception {
		this.request.getHeaders().setRange(Collections.singletonList(HttpRange.createByteRange(0, 5)));

		TestSubscriber.subscribe(this.writer.write(Mono.just(resource), null, ResolvableType.forClass(Resource.class),
				MediaType.TEXT_PLAIN, this.request, this.response, Collections.emptyMap())).assertComplete();

		assertThat(this.response.getHeaders().getContentType(), is(MediaType.TEXT_PLAIN));
		assertThat(this.response.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE), is("bytes 0-5/39"));
		assertThat(this.response.getHeaders().getContentLength(), is(6L));

		Mono<String> result = reduceToString(this.response.getBody(), this.response.bufferFactory());
		TestSubscriber.subscribe(result).assertComplete().assertValues("Spring");
	}

	@Test
	public void shouldThrowErrorInvalidRange() throws Exception {
		this.request.getHeaders().set(HttpHeaders.RANGE, "invalid");

111
		this.expectedException.expect(IllegalArgumentException.class);
112 113 114 115 116 117 118 119 120 121 122 123 124 125
		TestSubscriber.subscribe(this.writer.write(Mono.just(resource), null, ResolvableType.forClass(Resource.class),
				MediaType.TEXT_PLAIN, this.request, this.response, Collections.emptyMap()));
	}

	private Mono<String> reduceToString(Publisher<DataBuffer> buffers, DataBufferFactory bufferFactory) {

		return Flux.from(buffers)
				.reduce(bufferFactory.allocateBuffer(), (previous, current) -> {
					previous.write(current);
					DataBufferUtils.release(current);
					return previous;
				})
				.map(buffer -> DataBufferTestUtils.dumpString(buffer, StandardCharsets.UTF_8));
	}
126

127
}