Polishing external contribution

See gh-32622
This commit is contained in:
Arjen Poutsma
2024-04-23 14:23:35 +02:00
parent 3f8a10c19f
commit 6c5ef9715b
2 changed files with 36 additions and 44 deletions

View File

@@ -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<T> implements HttpMessageWriter<T> {
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());
}))

View File

@@ -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<CharSequence> encoder = CharSequenceEncoder.textPlainOnly();
EncoderHttpMessageWriter<CharSequence> writer = new EncoderHttpMessageWriter<>(encoder);
ReactiveHttpOutputMessage outputMessage = new MockClientHttpRequest(HttpMethod.POST, "/");
HttpMessageWriter<CharSequence> writer = new EncoderHttpMessageWriter<>(encoder);
Mono<Void> 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<CharSequence> encoder = CharSequenceEncoder.textPlainOnly();
EncoderHttpMessageWriter<CharSequence> writer = new EncoderHttpMessageWriter<>(encoder);
ReactiveHttpOutputMessage outputMessage = new MockClientHttpRequest(HttpMethod.POST, "/");
HttpMessageWriter<CharSequence> writer = new EncoderHttpMessageWriter<>(encoder);
Mono<Void> writerMono = writer.write(Mono.empty(), ResolvableType.forClass(String.class),
null, outputMessage, NO_HINTS);
null, this.response, NO_HINTS);
StepVerifier.create(writerMono)
.verifyComplete();
List<String> 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) {