Search in sources :

Example 11 with MultipartBodyBuilder

use of org.springframework.http.client.MultipartBodyBuilder in project spring-framework by spring-projects.

the class MultipartHttpMessageWriterTests method singleSubscriberWithResource.

// SPR-16402
@Test
public void singleSubscriberWithResource() throws IOException {
    Sinks.Many<Resource> sink = Sinks.many().unicast().onBackpressureBuffer();
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    sink.tryEmitNext(logo);
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.asyncPart("logo", sink.asFlux(), Resource.class);
    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();
    MultiValueMap<String, Part> requestParts = parse(this.response, hints);
    assertThat(requestParts.size()).isEqualTo(1);
    Part part = requestParts.getFirst("logo");
    assertThat(part.name()).isEqualTo("logo");
    assertThat(part instanceof FilePart).isTrue();
    assertThat(((FilePart) part).filename()).isEqualTo("logo.jpg");
    assertThat(part.headers().getContentType()).isEqualTo(MediaType.IMAGE_JPEG);
    assertThat(part.headers().getContentLength()).isEqualTo(logo.getFile().length());
}
Also used : Sinks(reactor.core.publisher.Sinks) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 12 with MultipartBodyBuilder

use of org.springframework.http.client.MultipartBodyBuilder in project spring-framework by spring-projects.

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");
}
Also used : MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 13 with MultipartBodyBuilder

use of org.springframework.http.client.MultipartBodyBuilder in project spring-framework by spring-projects.

the class MultipartHttpMessageWriterTests method customContentDisposition.

// SPR-16376
@Test
public void customContentDisposition() throws IOException {
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    Flux<DataBuffer> buffers = DataBufferUtils.read(logo, DefaultDataBufferFactory.sharedInstance, 1024);
    long contentLength = logo.contentLength();
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("resource", logo).headers(h -> h.setContentDispositionFormData("resource", "spring.jpg"));
    bodyBuilder.asyncPart("buffers", buffers, DataBuffer.class).headers(h -> {
        h.setContentDispositionFormData("buffers", "buffers.jpg");
        h.setContentType(MediaType.IMAGE_JPEG);
        h.setContentLength(contentLength);
    });
    MultiValueMap<String, HttpEntity<?>> multipartData = bodyBuilder.build();
    Map<String, Object> hints = Collections.emptyMap();
    this.writer.write(Mono.just(multipartData), null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block();
    MultiValueMap<String, Part> requestParts = parse(this.response, hints);
    assertThat(requestParts.size()).isEqualTo(2);
    Part part = requestParts.getFirst("resource");
    assertThat(part instanceof FilePart).isTrue();
    assertThat(((FilePart) part).filename()).isEqualTo("spring.jpg");
    assertThat(part.headers().getContentLength()).isEqualTo(logo.getFile().length());
    part = requestParts.getFirst("buffers");
    assertThat(part instanceof FilePart).isTrue();
    assertThat(((FilePart) part).filename()).isEqualTo("buffers.jpg");
    assertThat(part.headers().getContentLength()).isEqualTo(logo.getFile().length());
}
Also used : HttpEntity(org.springframework.http.HttpEntity) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 14 with MultipartBodyBuilder

use of org.springframework.http.client.MultipartBodyBuilder in project spring-framework by spring-projects.

the class MultipartHttpMessageWriterTests method writeMultipartFormData.

@Test
public void writeMultipartFormData() throws Exception {
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {

        @Override
        public String getFilename() {
            // 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 = new HttpHeaders();
    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");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) MultiValueMap(org.springframework.util.MultiValueMap) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 15 with MultipartBodyBuilder

use of org.springframework.http.client.MultipartBodyBuilder in project spring-framework by spring-projects.

the class MultipartControllerTests method multipartRequestWithOptionalFileListNotPresent.

@Test
public void multipartRequestWithOptionalFileListNotPresent() throws Exception {
    Map<String, String> json = Collections.singletonMap("name", "yeeeah");
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
    EntityExchangeResult<Void> exchangeResult = testClient.post().uri("/optionalfilelist").bodyValue(bodyBuilder.build()).exchange().expectStatus().isFound().expectBody().isEmpty();
    // Further assertions on the server response
    MockMvcWebTestClient.resultActionsFor(exchangeResult).andExpect(model().attributeDoesNotExist("fileContent")).andExpect(model().attribute("jsonContent", json));
}
Also used : MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

MultipartBodyBuilder (org.springframework.http.client.MultipartBodyBuilder)36 Test (org.junit.jupiter.api.Test)34 MethodParameter (org.springframework.core.MethodParameter)18 Part (org.springframework.http.codec.multipart.Part)7 RequestPart (org.springframework.web.bind.annotation.RequestPart)7 MvcAnnotationPredicates.requestPart (org.springframework.web.testfixture.method.MvcAnnotationPredicates.requestPart)7 ClassPathResource (org.springframework.core.io.ClassPathResource)4 MultiValueMap (org.springframework.util.MultiValueMap)4 BindingContext (org.springframework.web.reactive.BindingContext)4 ServerWebExchange (org.springframework.web.server.ServerWebExchange)4 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)4 Resource (org.springframework.core.io.Resource)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3 ServerWebInputException (org.springframework.web.server.ServerWebInputException)2 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 MediaType (org.springframework.http.MediaType)1 MultipartHttpMessageReader (org.springframework.http.codec.multipart.MultipartHttpMessageReader)1 MultipartHttpMessageWriter (org.springframework.http.codec.multipart.MultipartHttpMessageWriter)1 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)1