use of cn.taketoday.core.MultiValueMap in project today-infrastructure by TAKETODAY.
the class MultipartHttpMessageWriterTests method writeMultipartFormData.
@Test
public void writeMultipartFormData() throws Exception {
Resource logo = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg");
Resource utf8 = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg") {
@Override
public String getName() {
// SPR-12108
return "Hall\u00F6le.jpg";
}
};
Flux<DataBuffer> bufferPublisher = Flux.just(this.bufferFactory.wrap("Aa".getBytes(StandardCharsets.UTF_8)), this.bufferFactory.wrap("Bb".getBytes(StandardCharsets.UTF_8)), this.bufferFactory.wrap("Cc".getBytes(StandardCharsets.UTF_8)));
FilePart mockPart = mock(FilePart.class);
HttpHeaders partHeaders = HttpHeaders.create();
partHeaders.setContentType(MediaType.TEXT_PLAIN);
partHeaders.setContentDispositionFormData("foo", "file.txt");
partHeaders.add("foo", "bar");
given(mockPart.headers()).willReturn(partHeaders);
given(mockPart.content()).willReturn(bufferPublisher);
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
bodyBuilder.part("name 1", "value 1");
bodyBuilder.part("name 2", "value 2+1");
bodyBuilder.part("name 2", "value 2+2");
bodyBuilder.part("logo", logo);
bodyBuilder.part("utf8", utf8);
bodyBuilder.part("json", new Foo("bar"), MediaType.APPLICATION_JSON);
bodyBuilder.asyncPart("publisher", Flux.just("foo", "bar", "baz"), String.class);
bodyBuilder.part("filePublisher", mockPart);
Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
Map<String, Object> hints = Collections.emptyMap();
this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block(Duration.ofSeconds(5));
MultiValueMap<String, Part> requestParts = parse(this.response, hints);
assertThat(requestParts.size()).isEqualTo(7);
Part part = requestParts.getFirst("name 1");
assertThat(part instanceof FormFieldPart).isTrue();
assertThat(part.name()).isEqualTo("name 1");
assertThat(((FormFieldPart) part).value()).isEqualTo("value 1");
List<Part> parts2 = requestParts.get("name 2");
assertThat(parts2.size()).isEqualTo(2);
part = parts2.get(0);
assertThat(part instanceof FormFieldPart).isTrue();
assertThat(part.name()).isEqualTo("name 2");
assertThat(((FormFieldPart) part).value()).isEqualTo("value 2+1");
part = parts2.get(1);
assertThat(part instanceof FormFieldPart).isTrue();
assertThat(part.name()).isEqualTo("name 2");
assertThat(((FormFieldPart) part).value()).isEqualTo("value 2+2");
part = requestParts.getFirst("logo");
assertThat(part instanceof FilePart).isTrue();
assertThat(part.name()).isEqualTo("logo");
assertThat(((FilePart) part).filename()).isEqualTo("logo.jpg");
assertThat(part.headers().getContentType()).isEqualTo(MediaType.IMAGE_JPEG);
assertThat(part.headers().getContentLength()).isEqualTo(logo.getFile().length());
part = requestParts.getFirst("utf8");
assertThat(part instanceof FilePart).isTrue();
assertThat(part.name()).isEqualTo("utf8");
assertThat(((FilePart) part).filename()).isEqualTo("Hall\u00F6le.jpg");
assertThat(part.headers().getContentType()).isEqualTo(MediaType.IMAGE_JPEG);
assertThat(part.headers().getContentLength()).isEqualTo(utf8.getFile().length());
part = requestParts.getFirst("json");
assertThat(part).isNotNull();
assertThat(part.name()).isEqualTo("json");
assertThat(part.headers().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
String value = decodeToString(part);
assertThat(value).isEqualTo("{\"bar\":\"bar\"}");
part = requestParts.getFirst("publisher");
assertThat(part).isNotNull();
assertThat(part.name()).isEqualTo("publisher");
value = decodeToString(part);
assertThat(value).isEqualTo("foobarbaz");
part = requestParts.getFirst("filePublisher");
assertThat(part).isNotNull();
assertThat(part.name()).isEqualTo("filePublisher");
assertThat(part.headers()).containsEntry("foo", Collections.singletonList("bar"));
assertThat(((FilePart) part).filename()).isEqualTo("file.txt");
value = decodeToString(part);
assertThat(value).isEqualTo("AaBbCc");
}
use of cn.taketoday.core.MultiValueMap in project today-infrastructure by TAKETODAY.
the class MultipartHttpMessageWriterTests method writeMultipartRelated.
// gh-24582
@Test
public void writeMultipartRelated() {
MediaType mediaType = MediaType.parseMediaType("multipart/related;type=foo");
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
bodyBuilder.part("name 1", "value 1");
bodyBuilder.part("name 2", "value 2");
Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
Map<String, Object> hints = Collections.emptyMap();
this.writer.write(result, null, mediaType, this.response, hints).block(Duration.ofSeconds(5));
MediaType contentType = this.response.getHeaders().getContentType();
assertThat(contentType).isNotNull();
assertThat(contentType.isCompatibleWith(mediaType)).isTrue();
assertThat(contentType.getParameter("type")).isEqualTo("foo");
assertThat(contentType.getParameter("boundary")).isNotEmpty();
assertThat(contentType.getParameter("charset")).isNull();
MultiValueMap<String, Part> requestParts = parse(this.response, hints);
assertThat(requestParts.size()).isEqualTo(2);
assertThat(requestParts.getFirst("name 1").name()).isEqualTo("name 1");
assertThat(requestParts.getFirst("name 2").name()).isEqualTo("name 2");
}
use of cn.taketoday.core.MultiValueMap in project today-infrastructure by TAKETODAY.
the class FormHttpMessageReaderTests method readFormError.
@Test
public void readFormError() {
DataBuffer fooBuffer = stringBuffer("name=value");
Flux<DataBuffer> body = Flux.just(fooBuffer).concatWith(Flux.error(new RuntimeException()));
MockServerHttpRequest request = request(body);
Flux<MultiValueMap<String, String>> result = this.reader.read(null, request, null);
StepVerifier.create(result).expectError().verify();
}
use of cn.taketoday.core.MultiValueMap in project today-infrastructure by TAKETODAY.
the class MapToMapConverterTests method multiValueMapToMultiValueMap.
@Test
@SuppressWarnings("unchecked")
void multiValueMapToMultiValueMap() throws Exception {
DefaultConversionService.addDefaultConverters(conversionService);
MultiValueMap<String, Integer> source = MultiValueMap.fromLinkedHashMap();
source.put("a", Arrays.asList(1, 2, 3));
source.put("b", Arrays.asList(4, 5, 6));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget"));
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType);
assertThat(converted.size()).isEqualTo(2);
assertThat(converted.get("a")).isEqualTo(Arrays.asList("1", "2", "3"));
assertThat(converted.get("b")).isEqualTo(Arrays.asList("4", "5", "6"));
}
use of cn.taketoday.core.MultiValueMap in project today-infrastructure by TAKETODAY.
the class MapToMapConverterTests method mapToMultiValueMap.
@Test
@SuppressWarnings("unchecked")
void mapToMultiValueMap() throws Exception {
DefaultConversionService.addDefaultConverters(conversionService);
Map<String, Integer> source = new HashMap<>();
source.put("a", 1);
source.put("b", 2);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget"));
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType);
assertThat(converted.size()).isEqualTo(2);
assertThat(converted.get("a")).isEqualTo(Arrays.asList("1"));
assertThat(converted.get("b")).isEqualTo(Arrays.asList("2"));
}
Aggregations