Skip to content

Commit df41bb1

Browse files
committed
Merge pull request #32622 from NadChel:bugfix/GH-32620_remove_content_type_if_no_body
* gh-32622: Polishing external contribution Remove Content-Type when body is empty
2 parents adc7f73 + 6c5ef97 commit df41bb1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -127,6 +127,7 @@ public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType eleme
127127
return body
128128
.singleOrEmpty()
129129
.switchIfEmpty(Mono.defer(() -> {
130+
message.getHeaders().setContentType(null);
130131
message.getHeaders().setContentLength(0);
131132
return message.setComplete().then(Mono.empty());
132133
}))

spring-web/src/test/java/org/springframework/http/codec/EncoderHttpMessageWriterTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import reactor.core.publisher.Mono;
3434
import reactor.test.StepVerifier;
3535

36+
import org.springframework.core.ResolvableType;
3637
import org.springframework.core.codec.CharSequenceEncoder;
38+
import org.springframework.core.codec.Encoder;
3739
import org.springframework.core.io.buffer.DataBuffer;
3840
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
3941
import org.springframework.http.MediaType;
@@ -199,6 +201,30 @@ void isStreamingMediaType() throws InvocationTargetException, IllegalAccessExcep
199201
assertThat((Boolean) method.invoke(writer, TEXT_HTML)).isFalse();
200202
}
201203

204+
@Test
205+
public void noContentTypeWithEmptyBody() {
206+
Encoder<CharSequence> encoder = CharSequenceEncoder.textPlainOnly();
207+
HttpMessageWriter<CharSequence> writer = new EncoderHttpMessageWriter<>(encoder);
208+
Mono<Void> writerMono = writer.write(Mono.empty(), ResolvableType.forClass(String.class),
209+
null, this.response, NO_HINTS);
210+
211+
StepVerifier.create(writerMono)
212+
.verifyComplete();
213+
assertThat(response.getHeaders().getContentType()).isNull();
214+
}
215+
216+
@Test
217+
public void zeroContentLengthWithEmptyBody() {
218+
Encoder<CharSequence> encoder = CharSequenceEncoder.textPlainOnly();
219+
HttpMessageWriter<CharSequence> writer = new EncoderHttpMessageWriter<>(encoder);
220+
Mono<Void> writerMono = writer.write(Mono.empty(), ResolvableType.forClass(String.class),
221+
null, this.response, NO_HINTS);
222+
223+
StepVerifier.create(writerMono)
224+
.verifyComplete();
225+
assertThat(this.response.getHeaders().getContentLength()).isEqualTo(0);
226+
}
227+
202228
private void configureEncoder(MimeType... mimeTypes) {
203229
configureEncoder(Flux.empty(), mimeTypes);
204230
}

0 commit comments

Comments
 (0)