use of cn.taketoday.http.MockHttpInputMessage in project today-infrastructure by TAKETODAY.
the class ByteArrayHttpMessageConverterTests method read.
@Test
public void read() throws IOException {
byte[] body = new byte[] { 0x1, 0x2 };
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
inputMessage.getHeaders().setContentType(new MediaType("application", "octet-stream"));
byte[] result = converter.read(byte[].class, inputMessage);
assertThat(result).as("Invalid result").isEqualTo(body);
}
use of cn.taketoday.http.MockHttpInputMessage in project today-infrastructure by TAKETODAY.
the class ResourceHttpMessageConverterTests method shouldReadImageResource.
@Test
public void shouldReadImageResource() throws IOException {
byte[] body = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("logo.jpg"));
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
inputMessage.getHeaders().setContentDisposition(ContentDisposition.attachment().filename("yourlogo.jpg").build());
Resource actualResource = converter.read(Resource.class, inputMessage);
assertThat(FileCopyUtils.copyToByteArray(actualResource.getInputStream())).isEqualTo(body);
assertThat(actualResource.getName()).isEqualTo("yourlogo.jpg");
}
use of cn.taketoday.http.MockHttpInputMessage in project today-infrastructure by TAKETODAY.
the class ResourceHttpMessageConverterTests method shouldNotReadInputStreamResource.
// SPR-14882
@Test
public void shouldNotReadInputStreamResource() throws IOException {
ResourceHttpMessageConverter noStreamConverter = new ResourceHttpMessageConverter(false);
try (InputStream body = getClass().getResourceAsStream("logo.jpg")) {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> noStreamConverter.read(InputStreamResource.class, inputMessage));
}
}
use of cn.taketoday.http.MockHttpInputMessage in project today-infrastructure by TAKETODAY.
the class StringHttpMessageConverterTests method readJson.
// 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.class, inputMessage);
assertThat(result).as("Invalid result").isEqualTo(body);
}
use of cn.taketoday.http.MockHttpInputMessage in project today-infrastructure by TAKETODAY.
the class StringHttpMessageConverterTests method read.
@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.class, inputMessage);
assertThat(result).as("Invalid result").isEqualTo(body);
}
Aggregations