diff --git a/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java b/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java index 82896bb240..fc791caab7 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java +++ b/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 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. @@ -16,15 +16,20 @@ package org.springframework.http.codec; +import java.util.List; +import java.util.Map; + import org.apache.commons.logging.Log; import org.reactivestreams.Publisher; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + import org.springframework.core.ResolvableType; import org.springframework.core.codec.AbstractEncoder; import org.springframework.core.codec.Encoder; import org.springframework.core.codec.Hints; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferUtils; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpLogging; import org.springframework.http.MediaType; import org.springframework.http.ReactiveHttpOutputMessage; @@ -33,11 +38,6 @@ import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.StringUtils; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -import java.util.List; -import java.util.Map; /** * {@code HttpMessageWriter} that wraps and delegates to an {@link Encoder}. @@ -127,7 +127,7 @@ public class EncoderHttpMessageWriter implements HttpMessageWriter { return body .singleOrEmpty() .switchIfEmpty(Mono.defer(() -> { - message.getHeaders().remove(HttpHeaders.CONTENT_TYPE); + message.getHeaders().setContentType(null); message.getHeaders().setContentLength(0); return message.setComplete().then(Mono.empty()); })) diff --git a/spring-web/src/test/java/org/springframework/http/codec/EncoderHttpMessageWriterTests.java b/spring-web/src/test/java/org/springframework/http/codec/EncoderHttpMessageWriterTests.java index c1b65fc465..58479195e1 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/EncoderHttpMessageWriterTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/EncoderHttpMessageWriterTests.java @@ -16,29 +16,6 @@ package org.springframework.http.codec; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoSettings; -import org.mockito.quality.Strictness; -import org.springframework.core.ResolvableType; -import org.springframework.core.codec.CharSequenceEncoder; -import org.springframework.core.codec.Encoder; -import org.springframework.core.io.buffer.DataBuffer; -import org.springframework.core.io.buffer.DefaultDataBufferFactory; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.MediaType; -import org.springframework.http.ReactiveHttpOutputMessage; -import org.springframework.util.MimeType; -import org.springframework.util.MimeTypeUtils; -import org.springframework.util.ReflectionUtils; -import org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest; -import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; -import reactor.test.StepVerifier; - import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; @@ -47,6 +24,26 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +import org.springframework.core.ResolvableType; +import org.springframework.core.codec.CharSequenceEncoder; +import org.springframework.core.codec.Encoder; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.core.io.buffer.DefaultDataBufferFactory; +import org.springframework.http.MediaType; +import org.springframework.util.MimeType; +import org.springframework.util.MimeTypeUtils; +import org.springframework.util.ReflectionUtils; +import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse; + import static java.nio.charset.StandardCharsets.ISO_8859_1; import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; @@ -205,32 +202,27 @@ class EncoderHttpMessageWriterTests { } @Test - public void ifBodyPublisherEmpty_noContentTypeHeader() { + public void noContentTypeWithEmptyBody() { Encoder encoder = CharSequenceEncoder.textPlainOnly(); - EncoderHttpMessageWriter writer = new EncoderHttpMessageWriter<>(encoder); - ReactiveHttpOutputMessage outputMessage = new MockClientHttpRequest(HttpMethod.POST, "/"); + HttpMessageWriter writer = new EncoderHttpMessageWriter<>(encoder); Mono writerMono = writer.write(Mono.empty(), ResolvableType.forClass(String.class), - null, outputMessage, NO_HINTS); + null, this.response, NO_HINTS); StepVerifier.create(writerMono) .verifyComplete(); - assertThat(outputMessage.getHeaders()).doesNotContainKey(HttpHeaders.CONTENT_TYPE); + assertThat(response.getHeaders().getContentType()).isNull(); } @Test - public void ifBodyPublisherEmpty_contentLengthZero() { + public void zeroContentLengthWithEmptyBody() { Encoder encoder = CharSequenceEncoder.textPlainOnly(); - EncoderHttpMessageWriter writer = new EncoderHttpMessageWriter<>(encoder); - ReactiveHttpOutputMessage outputMessage = new MockClientHttpRequest(HttpMethod.POST, "/"); + HttpMessageWriter writer = new EncoderHttpMessageWriter<>(encoder); Mono writerMono = writer.write(Mono.empty(), ResolvableType.forClass(String.class), - null, outputMessage, NO_HINTS); + null, this.response, NO_HINTS); StepVerifier.create(writerMono) .verifyComplete(); - List contentLengthValues = outputMessage.getHeaders().get(HttpHeaders.CONTENT_LENGTH); - assertThat(contentLengthValues).hasSize(1); - int contentLength = Integer.parseInt(contentLengthValues.get(0)); - assertThat(contentLength).isEqualTo(0); + assertThat(this.response.getHeaders().getContentLength()).isEqualTo(0); } private void configureEncoder(MimeType... mimeTypes) {