Search in sources :

Example 26 with MultipartBodyBuilder

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

the class MultipartControllerTests method multipartRequestWithFileArray.

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

Example 27 with MultipartBodyBuilder

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

the class RequestPartMethodArgumentResolverTests method part.

@Test
void part() {
    MethodParameter param = this.testMethod.annot(requestPart()).arg(Part.class);
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("name", new Person("Jones"));
    Part actual = resolveArgument(param, bodyBuilder);
    DataBuffer buffer = DataBufferUtils.join(actual.content()).block();
    assertThat(buffer.toString(UTF_8)).isEqualTo("{\"name\":\"Jones\"}");
}
Also used : MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) RequestPart(org.springframework.web.bind.annotation.RequestPart) Part(org.springframework.http.codec.multipart.Part) MvcAnnotationPredicates.requestPart(org.springframework.web.testfixture.method.MvcAnnotationPredicates.requestPart) MethodParameter(org.springframework.core.MethodParameter) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 28 with MultipartBodyBuilder

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

the class MultipartHttpMessageWriterTests method singleSubscriberWithStrings.

// SPR-16402
@Test
public void singleSubscriberWithStrings() {
    @SuppressWarnings("deprecation") reactor.core.publisher.UnicastProcessor<String> processor = reactor.core.publisher.UnicastProcessor.create();
    Flux.just("foo", "bar", "baz").subscribe(processor);
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.asyncPart("name", processor, String.class);
    Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
    this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, Collections.emptyMap()).block(Duration.ofSeconds(5));
    // Make sure body is consumed to avoid leak reports
    this.response.getBodyAsString().block(Duration.ofSeconds(5));
}
Also used : MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 29 with MultipartBodyBuilder

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

the class MultipartIntegrationTests method generateBody.

private MultiValueMap<String, HttpEntity<?>> generateBody() {
    MultipartBodyBuilder builder = new MultipartBodyBuilder();
    builder.part("fieldPart", "fieldValue");
    builder.part("fileParts", new ClassPathResource("foo.txt", MultipartHttpMessageReader.class));
    builder.part("fileParts", new ClassPathResource("logo.png", getClass()));
    builder.part("jsonPart", new Person("Jason"));
    return builder.build();
}
Also used : MultipartHttpMessageReader(org.springframework.http.codec.multipart.MultipartHttpMessageReader) MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 30 with MultipartBodyBuilder

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

the class RequestPartMethodArgumentResolverTests method partRequired.

@Test
void partRequired() {
    MethodParameter param = this.testMethod.annot(requestPart()).arg(Part.class);
    ServerWebExchange exchange = createExchange(new MultipartBodyBuilder());
    Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);
    StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerWebInputException(org.springframework.web.server.ServerWebInputException) MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) MethodParameter(org.springframework.core.MethodParameter) BindingContext(org.springframework.web.reactive.BindingContext) 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