Polish StringHttpMessageConverterTests

This commit is contained in:
Sam Brannen 2023-08-04 15:53:37 +03:00
parent c050642290
commit 169392e132
1 changed files with 24 additions and 30 deletions

View File

@ -19,7 +19,6 @@ package org.springframework.http.converter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
@ -30,38 +29,33 @@ import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link StringHttpMessageConverter}.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
public class StringHttpMessageConverterTests {
class StringHttpMessageConverterTests {
public static final MediaType TEXT_PLAIN_UTF_8 = new MediaType("text", "plain", StandardCharsets.UTF_8);
private static final MediaType TEXT_PLAIN_UTF_8 = new MediaType("text", "plain", StandardCharsets.UTF_8);
private StringHttpMessageConverter converter;
private final StringHttpMessageConverter converter = new StringHttpMessageConverter();
private MockHttpOutputMessage outputMessage;
@BeforeEach
public void setUp() {
this.converter = new StringHttpMessageConverter();
this.outputMessage = new MockHttpOutputMessage();
}
private final MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
@Test
public void canRead() {
void canRead() {
assertThat(this.converter.canRead(String.class, MediaType.TEXT_PLAIN)).isTrue();
}
@Test
public void canWrite() {
void canWrite() {
assertThat(this.converter.canWrite(String.class, MediaType.TEXT_PLAIN)).isTrue();
assertThat(this.converter.canWrite(String.class, MediaType.ALL)).isTrue();
}
@Test
public void read() throws IOException {
void read() throws IOException {
String body = "Hello World";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(TEXT_PLAIN_UTF_8);
@ -71,7 +65,7 @@ public class StringHttpMessageConverterTests {
}
@Test
public void readWithContentLengthHeader() throws IOException {
void readWithContentLengthHeader() throws IOException {
String body = "Hello World";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentLength(body.length());
@ -82,7 +76,7 @@ public class StringHttpMessageConverterTests {
}
@Test // gh-24123
public void readJson() throws IOException {
void readJson() throws IOException {
String body = "{\"result\":\"\u0414\u0410\"}";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
@ -92,7 +86,7 @@ public class StringHttpMessageConverterTests {
}
@Test // gh-25328
public void readJsonApi() throws IOException {
void readJsonApi() throws IOException {
String body = "{\"result\":\"\u0414\u0410\"}";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(new MediaType("application", "vnd.api.v1+json"));
@ -102,7 +96,7 @@ public class StringHttpMessageConverterTests {
}
@Test
public void writeDefaultCharset() throws IOException {
void writeDefaultCharset() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, null, this.outputMessage);
@ -110,11 +104,11 @@ public class StringHttpMessageConverterTests {
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.ISO_8859_1)).isEqualTo(body);
assertThat(headers.getContentType()).isEqualTo(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.ISO_8859_1).length);
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
assertThat(headers.getAcceptCharset()).isEmpty();
}
@Test // gh-24123
public void writeJson() throws IOException {
void writeJson() throws IOException {
String body = "{\"føø\":\"bår\"}";
this.converter.write(body, MediaType.APPLICATION_JSON, this.outputMessage);
@ -122,11 +116,11 @@ public class StringHttpMessageConverterTests {
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo(body);
assertThat(headers.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
assertThat(headers.getAcceptCharset()).isEmpty();
}
@Test // gh-25328
public void writeJsonApi() throws IOException {
void writeJsonApi() throws IOException {
String body = "{\"føø\":\"bår\"}";
MediaType contentType = new MediaType("application", "vnd.api.v1+json");
this.converter.write(body, contentType, this.outputMessage);
@ -135,11 +129,11 @@ public class StringHttpMessageConverterTests {
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo(body);
assertThat(headers.getContentType()).isEqualTo(contentType);
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
assertThat(headers.getAcceptCharset()).isEmpty();
}
@Test
public void writeUTF8() throws IOException {
void writeUTF8() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, TEXT_PLAIN_UTF_8, this.outputMessage);
@ -147,11 +141,11 @@ public class StringHttpMessageConverterTests {
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo(body);
assertThat(headers.getContentType()).isEqualTo(TEXT_PLAIN_UTF_8);
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
assertThat(headers.getAcceptCharset()).isEmpty();
}
@Test // SPR-8867
public void writeOverrideRequestedContentType() throws IOException {
void writeOverrideRequestedContentType() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
MediaType requestedContentType = new MediaType("text", "html");
@ -162,11 +156,11 @@ public class StringHttpMessageConverterTests {
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo(body);
assertThat(headers.getContentType()).isEqualTo(TEXT_PLAIN_UTF_8);
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
assertThat(headers.getAcceptCharset()).isEmpty();
}
@Test // gh-24283
public void writeWithWildCardMediaType() throws IOException {
void writeWithWildCardMediaType() throws IOException {
String body = "Hello World";
this.converter.write(body, MediaType.ALL, this.outputMessage);
@ -174,7 +168,7 @@ public class StringHttpMessageConverterTests {
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.US_ASCII)).isEqualTo(body);
assertThat(headers.getContentType()).isEqualTo(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
assertThat(headers.getContentLength()).isEqualTo(body.getBytes().length);
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
assertThat(headers.getAcceptCharset()).isEmpty();
}
}