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