EncoderHttpMessageWriter tries to send wildcard type in response header

This commit is contained in:
soldierkam 2016-09-28 22:27:55 +02:00 committed by Rossen Stoyanchev
parent ced6559941
commit e5158d47e0
5 changed files with 80 additions and 40 deletions

View File

@ -110,13 +110,15 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
* Used when {@link #write} is called without a concrete content type.
*
* <p>By default returns the first of {@link Encoder#getEncodableMimeTypes()
* encodableMimeTypes}, if any.
* encodableMimeTypes} that is concrete({@link MediaType#isConcrete()}), if any.
*
* @param elementType the type of element for encoding
* @return the content type, or {@code null}
*/
protected MediaType getDefaultContentType(ResolvableType elementType) {
return (!this.writableMediaTypes.isEmpty() ? this.writableMediaTypes.get(0) : null);
return writableMediaTypes.stream()
.filter(MediaType::isConcrete)
.findFirst().orElse(null);
}
}

View File

@ -0,0 +1,55 @@
package org.springframework.http.codec;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.junit.Test;
import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.core.codec.ByteBufferEncoder;
import org.springframework.http.MediaType;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
import org.springframework.tests.TestSubscriber;
import org.springframework.util.MimeTypeUtils;
/**
* Unit tests for {@link EncoderHttpMessageWriter}.
*
* @author Marcin Kamionowski
*/
public class EncoderHttpMessageWriterTest {
private EncoderHttpMessageWriter<ByteBuffer> writer = new EncoderHttpMessageWriter<>(new ByteBufferEncoder());
private MockServerHttpResponse response = new MockServerHttpResponse();
@Test
public void writableMediaTypes() throws Exception {
assertThat(writer.getWritableMediaTypes(), containsInAnyOrder(MimeTypeUtils.ALL));
}
@Test
public void supportedMediaTypes() throws Exception {
assertTrue(writer.canWrite(ResolvableType.forClass(ByteBuffer.class),
MediaType.ALL));
assertTrue(writer.canWrite(ResolvableType.forClass(ByteBuffer.class),
MediaType.TEXT_PLAIN));
}
@Test
public void encodeByteBuffer(){
String payload = "Buffer payload";
Mono<ByteBuffer> source = Mono.just(ByteBuffer.wrap(payload.getBytes(StandardCharsets.UTF_8)));
writer.write(source, ResolvableType.forClass(ByteBuffer.class),
MediaType.APPLICATION_OCTET_STREAM, response, Collections.emptyMap());
assertThat(this.response.getHeaders().getContentType(), is(MediaType.APPLICATION_OCTET_STREAM));
TestSubscriber.subscribe(response.getBodyAsString()).assertComplete().assertValues(payload);
}
}

View File

@ -21,17 +21,11 @@ import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
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.HttpStatus;
@ -82,7 +76,7 @@ public class ResourceHttpMessageWriterTests {
assertThat(this.response.getHeaders().getContentLength(), is(39L));
assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES), is("bytes"));
Mono<String> result = reduceToString(this.response.getBody(), this.response.bufferFactory());
Mono<String> result = response.getBodyAsString();
TestSubscriber.subscribe(result).assertComplete().assertValues("Spring Framework test resource content.");
}
@ -98,7 +92,7 @@ public class ResourceHttpMessageWriterTests {
assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES), is("bytes"));
assertThat(this.response.getHeaders().getContentLength(), is(6L));
Mono<String> result = reduceToString(this.response.getBody(), this.response.bufferFactory());
Mono<String> result = response.getBodyAsString();
TestSubscriber.subscribe(result).assertComplete().assertValues("Spring");
}
@ -113,15 +107,4 @@ public class ResourceHttpMessageWriterTests {
assertThat(this.response.getStatusCode(), is(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE));
}
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));
}
}

View File

@ -16,8 +16,8 @@
package org.springframework.http.codec;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
@ -28,17 +28,12 @@ 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.core.io.support.ResourceRegion;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@ -94,7 +89,7 @@ public class ResourceRegionHttpMessageWriterTests {
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());
Mono<String> result = response.getBodyAsString();
TestSubscriber.subscribe(result).assertComplete().assertValues("Spring");
}
@ -118,7 +113,7 @@ public class ResourceRegionHttpMessageWriterTests {
HttpHeaders headers = this.response.getHeaders();
assertThat(headers.getContentType().toString(), startsWith("multipart/byteranges;boundary=" + boundary));
Mono<String> result = reduceToString(this.response.getBody(), this.response.bufferFactory());
Mono<String> result = response.getBodyAsString();
TestSubscriber
.subscribe(result).assertNoError()
.assertComplete()
@ -149,15 +144,4 @@ public class ResourceRegionHttpMessageWriterTests {
});
}
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));
}
}

View File

@ -16,6 +16,7 @@
package org.springframework.mock.http.server.reactive.test;
import java.nio.charset.StandardCharsets;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
@ -24,7 +25,9 @@ import reactor.core.publisher.Mono;
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.DefaultDataBufferFactory;
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
@ -57,6 +60,7 @@ public class MockServerHttpResponse implements ServerHttpResponse {
return true;
}
@Override
public HttpStatus getStatusCode() {
return this.status;
}
@ -105,4 +109,16 @@ public class MockServerHttpResponse implements ServerHttpResponse {
return this.bufferFactory;
}
public Mono<String> getBodyAsString() {
return Flux.from(body)
.reduce(bufferFactory.allocateBuffer(), (previous, current) -> {
previous.write(current);
DataBufferUtils.release(current);
return previous;
})
.map(buffer -> DataBufferTestUtils.dumpString(buffer, StandardCharsets.UTF_8));
}
}