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:
		
							parent
							
								
									5d8c5b0d9b
								
							
						
					
					
						commit
						a234b90abb
					
				|  | @ -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<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); | ||||
| 		}; | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
|  | @ -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<Resource, ReactiveHttpOutputMessage> inserter = BodyInserters.fromResource(body); | ||||
| 		Resource resource = new ClassPathResource("response.txt", getClass()); | ||||
| 
 | ||||
| 		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(); | ||||
| 
 | ||||
| 		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<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 | ||||
| 	public void ofResourceRange() throws IOException { | ||||
| 		final int rangeStart = 10; | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue