Here are the examples of the java api org.springframework.http.MockHttpOutputMessage taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
171 Examples
19
View Source File : ResourceHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
// SPR-12999
@Test
public void writeContentNotClosingInputStream() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource resource = mock(Resource.clreplaced);
InputStream inputStream = mock(InputStream.clreplaced);
given(resource.getInputStream()).willReturn(inputStream);
given(inputStream.read(any())).willReturn(-1);
willThrow(new NullPointerException()).given(inputStream).close();
converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage);
replacedertEquals(0, outputMessage.getHeaders().getContentLength());
}
19
View Source File : ResourceHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
// SPR-10848
@Test
public void writeByteArrayNullMediaType() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
byte[] byteArray = { 1, 2, 3 };
Resource body = new ByteArrayResource(byteArray);
converter.write(body, null, outputMessage);
replacedertTrue(Arrays.equals(byteArray, outputMessage.getBodyAsBytes()));
}
19
View Source File : MarshallingHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void write() throws Exception {
String body = "<root>Hello World</root>";
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Marshaller marshaller = mock(Marshaller.clreplaced);
willDoNothing().given(marshaller).marshal(eq(body), isA(Result.clreplaced));
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller);
converter.write(body, null, outputMessage);
replacedertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml"));
}
19
View Source File : ResourceHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-12999
@Test
public void writeContentNotClosingInputStream() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource resource = mock(Resource.clreplaced);
InputStream inputStream = mock(InputStream.clreplaced);
given(resource.getInputStream()).willReturn(inputStream);
given(inputStream.read(any())).willReturn(-1);
willThrow(new NullPointerException()).given(inputStream).close();
converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage);
replacedertThat(outputMessage.getHeaders().getContentLength()).isEqualTo(0);
}
19
View Source File : ResourceHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-12999
@Test
public void writeContentNotGettingInputStream() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource resource = mock(Resource.clreplaced);
given(resource.getInputStream()).willThrow(FileNotFoundException.clreplaced);
converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage);
replacedertThat(outputMessage.getHeaders().getContentLength()).isEqualTo(0);
}
19
View Source File : ResourceHttpMessageConverterTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
// SPR-12999
@Test
public void writeContentNotClosingInputStream() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource resource = mock(Resource.clreplaced);
InputStream inputStream = mock(InputStream.clreplaced);
given(resource.getInputStream()).willReturn(inputStream);
given(inputStream.read(any())).willReturn(-1);
doThrow(new NullPointerException()).when(inputStream).close();
converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage);
replacedertEquals(0, outputMessage.getHeaders().getContentLength());
}
19
View Source File : MappingJackson2HttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
// SPR-13318
@Test
public void writeSubType() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MyBean bean = new MyBean();
bean.setString("Foo");
bean.setNumber(42);
this.converter.writeInternal(bean, MyInterface.clreplaced, outputMessage);
String result = outputMessage.getBodyreplacedtring(Charset.forName("UTF-8"));
replacedertTrue(result.contains("\"string\":\"Foo\""));
replacedertTrue(result.contains("\"number\":42"));
}
18
View Source File : MarshallingHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void write() throws Exception {
String body = "<root>Hello World</root>";
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Marshaller marshaller = mock(Marshaller.clreplaced);
willDoNothing().given(marshaller).marshal(eq(body), isA(Result.clreplaced));
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller);
converter.write(body, null, outputMessage);
replacedertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
}
18
View Source File : StringHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
public clreplaced StringHttpMessageConverterTests {
public static final MediaType TEXT_PLAIN_UTF_8 = new MediaType("text", "plain", StandardCharsets.UTF_8);
private StringHttpMessageConverter converter;
private MockHttpOutputMessage outputMessage;
@Before
public void setUp() {
this.converter = new StringHttpMessageConverter();
this.outputMessage = new MockHttpOutputMessage();
}
@Test
public void canRead() {
replacedertTrue(this.converter.canRead(String.clreplaced, MediaType.TEXT_PLAIN));
}
@Test
public void canWrite() {
replacedertTrue(this.converter.canWrite(String.clreplaced, MediaType.TEXT_PLAIN));
replacedertTrue(this.converter.canWrite(String.clreplaced, MediaType.ALL));
}
@Test
public void read() throws IOException {
String body = "Hello World";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(TEXT_PLAIN_UTF_8);
String result = this.converter.read(String.clreplaced, inputMessage);
replacedertEquals("Invalid result", body, result);
}
@Test
public void writeDefaultCharset() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, null, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.ISO_8859_1));
replacedertEquals(new MediaType("text", "plain", StandardCharsets.ISO_8859_1), headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.ISO_8859_1).length, headers.getContentLength());
replacedertTrue(headers.getAcceptCharset().isEmpty());
}
@Test
public void writeUTF8() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, TEXT_PLAIN_UTF_8, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8));
replacedertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
replacedertTrue(headers.getAcceptCharset().isEmpty());
}
// SPR-8867
@Test
public void writeOverrideRequestedContentType() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
MediaType requestedContentType = new MediaType("text", "html");
HttpHeaders headers = this.outputMessage.getHeaders();
headers.setContentType(TEXT_PLAIN_UTF_8);
this.converter.write(body, requestedContentType, this.outputMessage);
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8));
replacedertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
replacedertTrue(headers.getAcceptCharset().isEmpty());
}
}
18
View Source File : ResourceHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
// SPR-12999
@Test
@SuppressWarnings("unchecked")
public void writeContentNotGettingInputStream() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource resource = mock(Resource.clreplaced);
given(resource.getInputStream()).willThrow(FileNotFoundException.clreplaced);
converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage);
replacedertEquals(0, outputMessage.getHeaders().getContentLength());
}
18
View Source File : MappingJackson2HttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
// SPR-13318
@Test
public void writeSubType() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MyBean bean = new MyBean();
bean.setString("Foo");
bean.setNumber(42);
this.converter.writeInternal(bean, MyInterface.clreplaced, outputMessage);
String result = outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8);
replacedertTrue(result.contains("\"string\":\"Foo\""));
replacedertTrue(result.contains("\"number\":42"));
}
18
View Source File : MarshallingHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void writeWithMarshallingFailureException() throws Exception {
String body = "<root>Hello World</root>";
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MarshallingFailureException ex = new MarshallingFailureException("forced");
Marshaller marshaller = mock(Marshaller.clreplaced);
willThrow(ex).given(marshaller).marshal(eq(body), isA(Result.clreplaced));
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller);
replacedertThatExceptionOfType(HttpMessageNotWritableException.clreplaced).isThrownBy(() -> converter.write(body, null, outputMessage)).withCause(ex);
}
18
View Source File : Jaxb2RootElementHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-11488
@Test
public void customizeMarshaller() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter();
myConverter.write(new MyRootElement(new MyCustomElement("a", "b")), null, outputMessage);
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
replacedertThat(XmlContent.of(outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8))).isSimilarTo("<myRootElement><element>a|||b</element></myRootElement>", ev);
}
18
View Source File : Jaxb2RootElementHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void writeXmlRootElementSubclreplaced() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(rootElementCglib, null, outputMessage);
replacedertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml"));
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
replacedertThat(XmlContent.of(outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8))).isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>", ev);
}
18
View Source File : Jaxb2RootElementHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void writeXmlRootElement() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(rootElement, null, outputMessage);
replacedertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml"));
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
replacedertThat(XmlContent.of(outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8))).isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>", ev);
}
18
View Source File : StringHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
/**
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
public clreplaced StringHttpMessageConverterTests {
public static final MediaType TEXT_PLAIN_UTF_8 = new MediaType("text", "plain", StandardCharsets.UTF_8);
private StringHttpMessageConverter converter;
private MockHttpOutputMessage outputMessage;
@BeforeEach
public void setUp() {
this.converter = new StringHttpMessageConverter();
this.outputMessage = new MockHttpOutputMessage();
}
@Test
public void canRead() {
replacedertThat(this.converter.canRead(String.clreplaced, MediaType.TEXT_PLAIN)).isTrue();
}
@Test
public void canWrite() {
replacedertThat(this.converter.canWrite(String.clreplaced, MediaType.TEXT_PLAIN)).isTrue();
replacedertThat(this.converter.canWrite(String.clreplaced, MediaType.ALL)).isTrue();
}
@Test
public void read() throws IOException {
String body = "Hello World";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(TEXT_PLAIN_UTF_8);
String result = this.converter.read(String.clreplaced, inputMessage);
replacedertThat(result).as("Invalid result").isEqualTo(body);
}
// gh-24123
@Test
public void readJson() throws IOException {
String body = "{\"result\":\"\u0414\u0410\"}";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
String result = this.converter.read(String.clreplaced, inputMessage);
replacedertThat(result).as("Invalid result").isEqualTo(body);
}
@Test
public void writeDefaultCharset() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, null, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertThat(this.outputMessage.getBodyreplacedtring(StandardCharsets.ISO_8859_1)).isEqualTo(body);
replacedertThat(headers.getContentType()).isEqualTo(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
replacedertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.ISO_8859_1).length);
replacedertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}
// gh-24123
@Test
public void writeJson() throws IOException {
String body = "{\"foo\":\"bar\"}";
this.converter.write(body, MediaType.APPLICATION_JSON, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertThat(this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8)).isEqualTo(body);
replacedertThat(headers.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
replacedertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
replacedertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}
@Test
public void writeUTF8() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, TEXT_PLAIN_UTF_8, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertThat(this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8)).isEqualTo(body);
replacedertThat(headers.getContentType()).isEqualTo(TEXT_PLAIN_UTF_8);
replacedertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
replacedertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}
// SPR-8867
@Test
public void writeOverrideRequestedContentType() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
MediaType requestedContentType = new MediaType("text", "html");
HttpHeaders headers = this.outputMessage.getHeaders();
headers.setContentType(TEXT_PLAIN_UTF_8);
this.converter.write(body, requestedContentType, this.outputMessage);
replacedertThat(this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8)).isEqualTo(body);
replacedertThat(headers.getContentType()).isEqualTo(TEXT_PLAIN_UTF_8);
replacedertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
replacedertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}
// gh-24283
@Test
public void writeWithWildCardMediaType() throws IOException {
String body = "Hello World";
this.converter.write(body, MediaType.ALL, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertThat(this.outputMessage.getBodyreplacedtring(StandardCharsets.US_ASCII)).isEqualTo(body);
replacedertThat(headers.getContentType()).isEqualTo(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
replacedertThat(headers.getContentLength()).isEqualTo(body.getBytes().length);
replacedertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}
}
18
View Source File : ResourceHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void shouldWriteImageResource() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource body = new ClreplacedPathResource("logo.jpg", getClreplaced());
converter.write(body, null, outputMessage);
replacedertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(MediaType.IMAGE_JPEG);
replacedertThat(outputMessage.getHeaders().getContentLength()).as("Invalid content-length").isEqualTo(body.getFile().length());
}
18
View Source File : ResourceHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-13620
@Test
public void writeContentInputStreamThrowingNullPointerException() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource resource = mock(Resource.clreplaced);
InputStream in = mock(InputStream.clreplaced);
given(resource.getInputStream()).willReturn(in);
given(in.read(any())).willThrow(NullPointerException.clreplaced);
converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage);
replacedertThat(outputMessage.getHeaders().getContentLength()).isEqualTo(0);
}
18
View Source File : ResourceHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-10848
@Test
public void writeByteArrayNullMediaType() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
byte[] byteArray = { 1, 2, 3 };
Resource body = new ByteArrayResource(byteArray);
converter.write(body, null, outputMessage);
replacedertThat(Arrays.equals(byteArray, outputMessage.getBodyAsBytes())).isTrue();
}
18
View Source File : JsonbHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void prefixJsonCustom() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setJsonPrefix(")))");
this.converter.writeInternal("foo", null, outputMessage);
replacedertThat(outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8)).isEqualTo(")))foo");
}
18
View Source File : JsonbHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void prefixJson() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setPrefixJson(true);
this.converter.writeInternal("foo", null, outputMessage);
replacedertThat(outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8)).isEqualTo(")]}', foo");
}
18
View Source File : JsonbHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void write() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MyBean body = new MyBean();
body.setString("Foo");
body.setNumber(42);
body.setFraction(42F);
body.setArray(new String[] { "Foo", "Bar" });
body.setBool(true);
body.setBytes(new byte[] { 0x1, 0x2 });
this.converter.write(body, null, outputMessage);
Charset utf8 = StandardCharsets.UTF_8;
String result = outputMessage.getBodyreplacedtring(utf8);
replacedertThat(result.contains("\"string\":\"Foo\"")).isTrue();
replacedertThat(result.contains("\"number\":42")).isTrue();
replacedertThat(result.contains("fraction\":42.0")).isTrue();
replacedertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue();
replacedertThat(result.contains("\"bool\":true")).isTrue();
replacedertThat(result.contains("\"bytes\":[1,2]")).isTrue();
replacedertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8));
}
18
View Source File : JsonbHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void writeUTF16() throws IOException {
MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, contentType, outputMessage);
replacedertThat(outputMessage.getBodyreplacedtring(StandardCharsets.UTF_16BE)).as("Invalid result").isEqualTo(body);
replacedertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(contentType);
}
18
View Source File : JsonbHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void writeWithBaseType() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MyBean body = new MyBean();
body.setString("Foo");
body.setNumber(42);
body.setFraction(42F);
body.setArray(new String[] { "Foo", "Bar" });
body.setBool(true);
body.setBytes(new byte[] { 0x1, 0x2 });
this.converter.write(body, MyBase.clreplaced, null, outputMessage);
Charset utf8 = StandardCharsets.UTF_8;
String result = outputMessage.getBodyreplacedtring(utf8);
replacedertThat(result.contains("\"string\":\"Foo\"")).isTrue();
replacedertThat(result.contains("\"number\":42")).isTrue();
replacedertThat(result.contains("fraction\":42.0")).isTrue();
replacedertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue();
replacedertThat(result.contains("\"bool\":true")).isTrue();
replacedertThat(result.contains("\"bytes\":[1,2]")).isTrue();
replacedertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8));
}
18
View Source File : GsonHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void prefixJson() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setPrefixJson(true);
this.converter.writeInternal("foo", null, outputMessage);
replacedertThat(outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8)).isEqualTo(")]}', \"foo\"");
}
18
View Source File : GsonHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void prefixJsonCustom() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setJsonPrefix(")))");
this.converter.writeInternal("foo", null, outputMessage);
replacedertThat(outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8)).isEqualTo(")))\"foo\"");
}
18
View Source File : GsonHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void writeUTF16() throws IOException {
MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, contentType, outputMessage);
replacedertThat(outputMessage.getBodyreplacedtring(StandardCharsets.UTF_16BE)).as("Invalid result").isEqualTo(("\"" + body + "\""));
replacedertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(contentType);
}
18
View Source File : StringHttpMessageConverterTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
/**
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
public clreplaced StringHttpMessageConverterTests {
public static final MediaType TEXT_PLAIN_UTF_8 = new MediaType("text", "plain", StandardCharsets.UTF_8);
private StringHttpMessageConverter converter;
private MockHttpOutputMessage outputMessage;
@Before
public void setUp() {
this.converter = new StringHttpMessageConverter();
this.outputMessage = new MockHttpOutputMessage();
}
@Test
public void canRead() {
replacedertTrue(this.converter.canRead(String.clreplaced, MediaType.TEXT_PLAIN));
}
@Test
public void canWrite() {
replacedertTrue(this.converter.canWrite(String.clreplaced, MediaType.TEXT_PLAIN));
replacedertTrue(this.converter.canWrite(String.clreplaced, MediaType.ALL));
}
@Test
public void read() throws IOException {
String body = "Hello World";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(TEXT_PLAIN_UTF_8);
String result = this.converter.read(String.clreplaced, inputMessage);
replacedertEquals("Invalid result", body, result);
}
@Test
public void writeDefaultCharset() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, null, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.ISO_8859_1));
replacedertEquals(new MediaType("text", "plain", StandardCharsets.ISO_8859_1), headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.ISO_8859_1).length, headers.getContentLength());
replacedertFalse(headers.getAcceptCharset().isEmpty());
}
@Test
public void writeUTF8() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, TEXT_PLAIN_UTF_8, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8));
replacedertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
replacedertFalse(headers.getAcceptCharset().isEmpty());
}
// SPR-8867
@Test
public void writeOverrideRequestedContentType() throws IOException {
String body = "H\u00e9llo W\u00f6rld";
MediaType requestedContentType = new MediaType("text", "html");
HttpHeaders headers = this.outputMessage.getHeaders();
headers.setContentType(TEXT_PLAIN_UTF_8);
this.converter.write(body, requestedContentType, this.outputMessage);
replacedertEquals(body, this.outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8));
replacedertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
replacedertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
replacedertFalse(headers.getAcceptCharset().isEmpty());
}
}
18
View Source File : MappingJackson2XmlHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void jsonView() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
JacksonViewBean bean = new JacksonViewBean();
bean.setWithView1("with");
bean.setWithView2("with");
bean.setWithoutView("without");
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
jacksonValue.setSerializationView(MyJacksonView1.clreplaced);
this.writeInternal(jacksonValue, outputMessage);
String result = outputMessage.getBodyreplacedtring(Charset.forName("UTF-8"));
replacedertThat(result, containsString("<withView1>with</withView1>"));
replacedertThat(result, not(containsString("<withView2>with</withView2>")));
replacedertThat(result, not(containsString("<withoutView>without</withoutView>")));
}
18
View Source File : MappingJackson2XmlHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void write() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MyBean body = new MyBean();
body.setString("Foo");
body.setNumber(42);
body.setFraction(42F);
body.setArray(new String[] { "Foo", "Bar" });
body.setBool(true);
body.setBytes(new byte[] { 0x1, 0x2 });
converter.write(body, null, outputMessage);
Charset utf8 = Charset.forName("UTF-8");
String result = outputMessage.getBodyreplacedtring(utf8);
replacedertTrue(result.contains("<string>Foo</string>"));
replacedertTrue(result.contains("<number>42</number>"));
replacedertTrue(result.contains("<fraction>42.0</fraction>"));
replacedertTrue(result.contains("<array><array>Foo</array><array>Bar</array></array>"));
replacedertTrue(result.contains("<bool>true</bool>"));
replacedertTrue(result.contains("<bytes>AQI=</bytes>"));
replacedertEquals("Invalid content-type", new MediaType("application", "xml", utf8), outputMessage.getHeaders().getContentType());
}
18
View Source File : Jaxb2RootElementHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void writeXmlRootElement() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(rootElement, null, outputMessage);
replacedertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
replacedertXMLEqual("Invalid result", "<rootElement><type s=\"Hello World\"/></rootElement>", outputMessage.getBodyreplacedtring(Charset.forName("UTF-8")));
}
18
View Source File : Jaxb2RootElementHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
// SPR-11488
@Test
public void customizeMarshaller() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter();
myConverter.write(new MyRootElement(new MyCustomElement("a", "b")), null, outputMessage);
replacedertXMLEqual("Invalid result", "<myRootElement><element>a|||b</element></myRootElement>", outputMessage.getBodyreplacedtring(Charset.forName("UTF-8")));
}
18
View Source File : Jaxb2RootElementHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void writeXmlRootElementSubclreplaced() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(rootElementCglib, null, outputMessage);
replacedertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
replacedertXMLEqual("Invalid result", "<rootElement><type s=\"Hello World\"/></rootElement>", outputMessage.getBodyreplacedtring(Charset.forName("UTF-8")));
}
18
View Source File : MappingJackson2HttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void jsonp() throws Exception {
MappingJacksonValue jacksonValue = new MappingJacksonValue("foo");
jacksonValue.setSerializationView(MyJacksonView1.clreplaced);
jacksonValue.setJsonpFunction("callback");
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.writeInternal(jacksonValue, null, outputMessage);
replacedertEquals("/**/callback(\"foo\");", outputMessage.getBodyreplacedtring(Charset.forName("UTF-8")));
}
18
View Source File : MappingJackson2HttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void prettyPrint() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
PrettyPrintBean bean = new PrettyPrintBean();
bean.setName("Jason");
this.converter.setPrettyPrint(true);
this.converter.writeInternal(bean, null, outputMessage);
String result = outputMessage.getBodyreplacedtring(Charset.forName("UTF-8"));
replacedertEquals("{" + NEWLINE_SYSTEM_PROPERTY + " \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}", result);
}
18
View Source File : MappingJackson2HttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
// SPR-13318
@Test
public void writeSubTypeList() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
List<MyBean> beans = new ArrayList<MyBean>();
MyBean foo = new MyBean();
foo.setString("Foo");
foo.setNumber(42);
beans.add(foo);
MyBean bar = new MyBean();
bar.setString("Bar");
bar.setNumber(123);
beans.add(bar);
ParameterizedTypeReference<List<MyInterface>> typeReference = new ParameterizedTypeReference<List<MyInterface>>() {
};
this.converter.writeInternal(beans, typeReference.getType(), outputMessage);
String result = outputMessage.getBodyreplacedtring(Charset.forName("UTF-8"));
replacedertTrue(result.contains("\"string\":\"Foo\""));
replacedertTrue(result.contains("\"number\":42"));
replacedertTrue(result.contains("\"string\":\"Bar\""));
replacedertTrue(result.contains("\"number\":123"));
}
18
View Source File : MappingJackson2HttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void jsonpAndJsonView() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
JacksonViewBean bean = new JacksonViewBean();
bean.setWithView1("with");
bean.setWithView2("with");
bean.setWithoutView("without");
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
jacksonValue.setSerializationView(MyJacksonView1.clreplaced);
jacksonValue.setJsonpFunction("callback");
this.converter.writeInternal(jacksonValue, null, outputMessage);
String result = outputMessage.getBodyreplacedtring(Charset.forName("UTF-8"));
replacedertThat(result, startsWith("/**/callback("));
replacedertThat(result, endsWith(");"));
replacedertThat(result, containsString("\"withView1\":\"with\""));
replacedertThat(result, not(containsString("\"withView2\":\"with\"")));
replacedertThat(result, not(containsString("\"withoutView\":\"without\"")));
}
18
View Source File : MappingJackson2HttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void jsonView() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
JacksonViewBean bean = new JacksonViewBean();
bean.setWithView1("with");
bean.setWithView2("with");
bean.setWithoutView("without");
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
jacksonValue.setSerializationView(MyJacksonView1.clreplaced);
this.converter.writeInternal(jacksonValue, null, outputMessage);
String result = outputMessage.getBodyreplacedtring(Charset.forName("UTF-8"));
replacedertThat(result, containsString("\"withView1\":\"with\""));
replacedertThat(result, not(containsString("\"withView2\":\"with\"")));
replacedertThat(result, not(containsString("\"withoutView\":\"without\"")));
}
18
View Source File : MappingJackson2HttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void prefixJson() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setPrefixJson(true);
this.converter.writeInternal("foo", null, outputMessage);
replacedertEquals(")]}', \"foo\"", outputMessage.getBodyreplacedtring(Charset.forName("UTF-8")));
}
18
View Source File : MappingJackson2HttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void write() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MyBean body = new MyBean();
body.setString("Foo");
body.setNumber(42);
body.setFraction(42F);
body.setArray(new String[] { "Foo", "Bar" });
body.setBool(true);
body.setBytes(new byte[] { 0x1, 0x2 });
converter.write(body, null, outputMessage);
Charset utf8 = Charset.forName("UTF-8");
String result = outputMessage.getBodyreplacedtring(utf8);
replacedertTrue(result.contains("\"string\":\"Foo\""));
replacedertTrue(result.contains("\"number\":42"));
replacedertTrue(result.contains("fraction\":42.0"));
replacedertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
replacedertTrue(result.contains("\"bool\":true"));
replacedertTrue(result.contains("\"bytes\":\"AQI=\""));
replacedertEquals("Invalid content-type", new MediaType("application", "json", utf8), outputMessage.getHeaders().getContentType());
}
18
View Source File : MappingJackson2HttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void prefixJsonCustom() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setJsonPrefix(")))");
this.converter.writeInternal("foo", null, outputMessage);
replacedertEquals(")))\"foo\"", outputMessage.getBodyreplacedtring(Charset.forName("UTF-8")));
}
18
View Source File : GsonHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void prefixJsonCustom() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setJsonPrefix(")))");
this.converter.writeInternal("foo", null, outputMessage);
replacedertEquals(")))\"foo\"", outputMessage.getBodyreplacedtring(UTF8));
}
18
View Source File : GsonHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void prefixJson() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setPrefixJson(true);
this.converter.writeInternal("foo", null, outputMessage);
replacedertEquals(")]}', \"foo\"", outputMessage.getBodyreplacedtring(UTF8));
}
18
View Source File : GsonHttpMessageConverterTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void write() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MyBean body = new MyBean();
body.setString("Foo");
body.setNumber(42);
body.setFraction(42F);
body.setArray(new String[] { "Foo", "Bar" });
body.setBool(true);
body.setBytes(new byte[] { 0x1, 0x2 });
this.converter.write(body, null, outputMessage);
Charset utf8 = Charset.forName("UTF-8");
String result = outputMessage.getBodyreplacedtring(utf8);
replacedertTrue(result.contains("\"string\":\"Foo\""));
replacedertTrue(result.contains("\"number\":42"));
replacedertTrue(result.contains("fraction\":42.0"));
replacedertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
replacedertTrue(result.contains("\"bool\":true"));
replacedertTrue(result.contains("\"bytes\":[1,2]"));
replacedertEquals("Invalid content-type", new MediaType("application", "json", utf8), outputMessage.getHeaders().getContentType());
}
17
View Source File : MarshallingHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void writeWithMarshallingFailureException() throws Exception {
String body = "<root>Hello World</root>";
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MarshallingFailureException ex = new MarshallingFailureException("forced");
Marshaller marshaller = mock(Marshaller.clreplaced);
willThrow(ex).given(marshaller).marshal(eq(body), isA(Result.clreplaced));
try {
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller);
converter.write(body, null, outputMessage);
fail("HttpMessageNotWritableException should be thrown");
} catch (HttpMessageNotWritableException e) {
replacedertTrue("Invalid exception hierarchy", e.getCause() == ex);
}
}
17
View Source File : Jaxb2RootElementHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
// SPR-11488
@Test
public void customizeMarshaller() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter();
myConverter.write(new MyRootElement(new MyCustomElement("a", "b")), null, outputMessage);
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
replacedertThat("Invalid result", outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8), isSimilarTo("<myRootElement><element>a|||b</element></myRootElement>").withDifferenceEvaluator(ev));
}
17
View Source File : Jaxb2RootElementHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void writeXmlRootElement() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(rootElement, null, outputMessage);
replacedertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
replacedertThat("Invalid result", outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8), isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev));
}
17
View Source File : Jaxb2RootElementHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void writeXmlRootElementSubclreplaced() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(rootElementCglib, null, outputMessage);
replacedertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
replacedertThat("Invalid result", outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8), isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev));
}
17
View Source File : ResourceHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void shouldWriteImageResource() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource body = new ClreplacedPathResource("logo.jpg", getClreplaced());
converter.write(body, null, outputMessage);
replacedertEquals("Invalid content-type", MediaType.IMAGE_JPEG, outputMessage.getHeaders().getContentType());
replacedertEquals("Invalid content-length", body.getFile().length(), outputMessage.getHeaders().getContentLength());
}
17
View Source File : ResourceHttpMessageConverterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
// SPR-13620
@Test
@SuppressWarnings("unchecked")
public void writeContentInputStreamThrowingNullPointerException() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource resource = mock(Resource.clreplaced);
InputStream in = mock(InputStream.clreplaced);
given(resource.getInputStream()).willReturn(in);
given(in.read(any())).willThrow(NullPointerException.clreplaced);
converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage);
replacedertEquals(0, outputMessage.getHeaders().getContentLength());
}
See More Examples