Explicit content-type for resources

BodyInserters.ofResource now respects the Content-Type, if specified on
the client request or server response.

Closes gh-24366
This commit is contained in:
Rossen Stoyanchev 2020-01-20 17:09:47 +00:00
parent 5d8c5b0d9b
commit a234b90abb
2 changed files with 31 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -218,7 +218,8 @@ public abstract class BodyInserters {
return (outputMessage, context) -> { return (outputMessage, context) -> {
ResolvableType elementType = RESOURCE_TYPE; ResolvableType elementType = RESOURCE_TYPE;
HttpMessageWriter<Resource> writer = findWriter(context, elementType, null); HttpMessageWriter<Resource> writer = findWriter(context, elementType, null);
return write(Mono.just(resource), elementType, null, outputMessage, context, writer); MediaType contentType = outputMessage.getHeaders().getContentType();
return write(Mono.just(resource), elementType, contentType, outputMessage, context, writer);
}; };
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -48,6 +48,7 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.core.testfixture.io.buffer.DataBufferTestUtils; import org.springframework.core.testfixture.io.buffer.DataBufferTestUtils;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRange; import org.springframework.http.HttpRange;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage; import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.http.client.reactive.ClientHttpRequest; import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.http.codec.EncoderHttpMessageWriter; import org.springframework.http.codec.EncoderHttpMessageWriter;
@ -224,14 +225,13 @@ public class BodyInsertersTests {
@Test @Test
public void ofResource() throws IOException { public void ofResource() throws IOException {
Resource body = new ClassPathResource("response.txt", getClass()); Resource resource = new ClassPathResource("response.txt", getClass());
BodyInserter<Resource, ReactiveHttpOutputMessage> inserter = BodyInserters.fromResource(body);
MockServerHttpResponse response = new MockServerHttpResponse(); MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context); Mono<Void> result = BodyInserters.fromResource(resource).insert(response, this.context);
StepVerifier.create(result).expectComplete().verify(); StepVerifier.create(result).expectComplete().verify();
byte[] expectedBytes = Files.readAllBytes(body.getFile().toPath()); byte[] expectedBytes = Files.readAllBytes(resource.getFile().toPath());
StepVerifier.create(response.getBody()) StepVerifier.create(response.getBody())
.consumeNextWith(dataBuffer -> { .consumeNextWith(dataBuffer -> {
@ -244,6 +244,29 @@ public class BodyInsertersTests {
.verify(); .verify();
} }
@Test // gh-24366
public void ofResourceWithExplicitMediaType() throws IOException {
Resource resource = new ClassPathResource("response.txt", getClass());
MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.POST, "/");
request.getHeaders().setContentType(MediaType.TEXT_MARKDOWN);
Mono<Void> result = BodyInserters.fromResource(resource).insert(request, this.context);
StepVerifier.create(result).expectComplete().verify();
byte[] expectedBytes = Files.readAllBytes(resource.getFile().toPath());
assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_MARKDOWN);
StepVerifier.create(request.getBody())
.consumeNextWith(dataBuffer -> {
byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(resultBytes);
DataBufferUtils.release(dataBuffer);
assertThat(resultBytes).isEqualTo(expectedBytes);
})
.expectComplete()
.verify();
}
@Test @Test
public void ofResourceRange() throws IOException { public void ofResourceRange() throws IOException {
final int rangeStart = 10; final int rangeStart = 10;