diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java index 56f1b41d78a..15f6ad99fa0 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java @@ -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"); * you may not use this file except in compliance with the License. @@ -218,7 +218,8 @@ public abstract class BodyInserters { return (outputMessage, context) -> { ResolvableType elementType = RESOURCE_TYPE; HttpMessageWriter 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); }; } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java index 11141262083..bc68dc96d9a 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java @@ -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"); * 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.http.HttpMethod; import org.springframework.http.HttpRange; +import org.springframework.http.MediaType; import org.springframework.http.ReactiveHttpOutputMessage; import org.springframework.http.client.reactive.ClientHttpRequest; import org.springframework.http.codec.EncoderHttpMessageWriter; @@ -224,14 +225,13 @@ public class BodyInsertersTests { @Test public void ofResource() throws IOException { - Resource body = new ClassPathResource("response.txt", getClass()); - BodyInserter inserter = BodyInserters.fromResource(body); + Resource resource = new ClassPathResource("response.txt", getClass()); MockServerHttpResponse response = new MockServerHttpResponse(); - Mono result = inserter.insert(response, this.context); + Mono result = BodyInserters.fromResource(resource).insert(response, this.context); StepVerifier.create(result).expectComplete().verify(); - byte[] expectedBytes = Files.readAllBytes(body.getFile().toPath()); + byte[] expectedBytes = Files.readAllBytes(resource.getFile().toPath()); StepVerifier.create(response.getBody()) .consumeNextWith(dataBuffer -> { @@ -244,6 +244,29 @@ public class BodyInsertersTests { .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 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 public void ofResourceRange() throws IOException { final int rangeStart = 10;