use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class FormHttpMessageConverterTests method readForm.
@Test
public void readForm() throws Exception {
String body = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.ISO_8859_1));
inputMessage.getHeaders().setContentType(new MediaType("application", "x-www-form-urlencoded", StandardCharsets.ISO_8859_1));
MultiValueMap<String, String> result = this.converter.read(null, inputMessage);
assertThat(result.size()).as("Invalid result").isEqualTo(3);
assertThat(result.getFirst("name 1")).as("Invalid result").isEqualTo("value 1");
List<String> values = result.get("name 2");
assertThat(values.size()).as("Invalid result").isEqualTo(2);
assertThat(values.get(0)).as("Invalid result").isEqualTo("value 2+1");
assertThat(values.get(1)).as("Invalid result").isEqualTo("value 2+2");
assertThat(result.getFirst("name 3")).as("Invalid result").isNull();
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class AtomFeedHttpMessageConverterTests method read.
@Test
public void read() throws IOException {
InputStream inputStream = getClass().getResourceAsStream("atom.xml");
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
inputMessage.getHeaders().setContentType(ATOM_XML_UTF8);
Feed result = converter.read(Feed.class, inputMessage);
assertThat(result.getTitle()).isEqualTo("title");
assertThat(result.getSubtitle().getValue()).isEqualTo("subtitle");
List<?> entries = result.getEntries();
assertThat(entries.size()).isEqualTo(2);
Entry entry1 = (Entry) entries.get(0);
assertThat(entry1.getId()).isEqualTo("id1");
assertThat(entry1.getTitle()).isEqualTo("title1");
Entry entry2 = (Entry) entries.get(1);
assertThat(entry2.getId()).isEqualTo("id2");
assertThat(entry2.getTitle()).isEqualTo("title2");
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
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 org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class StringHttpMessageConverterTests method readJsonApi.
// gh-25328
@Test
public 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"));
String result = this.converter.read(String.class, inputMessage);
assertThat(result).as("Invalid result").isEqualTo(body);
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class ResourceHttpMessageConverterTests method shouldReadInputStreamResource.
// SPR-13443
@Test
public void shouldReadInputStreamResource() throws IOException {
try (InputStream body = getClass().getResourceAsStream("logo.jpg")) {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
inputMessage.getHeaders().setContentDisposition(ContentDisposition.attachment().filename("yourlogo.jpg").build());
inputMessage.getHeaders().setContentLength(123);
Resource actualResource = converter.read(InputStreamResource.class, inputMessage);
assertThat(actualResource).isInstanceOf(InputStreamResource.class);
assertThat(actualResource.getInputStream()).isEqualTo(body);
assertThat(actualResource.getFilename()).isEqualTo("yourlogo.jpg");
assertThat(actualResource.contentLength()).isEqualTo(123);
}
}
Aggregations