Search in sources :

Example 1 with PublisherEntity

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

the class MultipartBodyBuilderTests method builder.

@Test
public void builder() {
    MultipartBodyBuilder builder = new MultipartBodyBuilder();
    MultiValueMap<String, String> multipartData = new LinkedMultiValueMap<>();
    multipartData.add("form field", "form value");
    builder.part("key", multipartData).header("foo", "bar");
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    builder.part("logo", logo).header("baz", "qux");
    HttpHeaders entityHeaders = new HttpHeaders();
    entityHeaders.add("foo", "bar");
    HttpEntity<String> entity = new HttpEntity<>("body", entityHeaders);
    builder.part("entity", entity).header("baz", "qux");
    Publisher<String> publisher = Flux.just("foo", "bar", "baz");
    builder.asyncPart("publisherClass", publisher, String.class).header("baz", "qux");
    builder.asyncPart("publisherPtr", publisher, new ParameterizedTypeReference<String>() {
    }).header("baz", "qux");
    MultiValueMap<String, HttpEntity<?>> result = builder.build();
    assertThat(result.size()).isEqualTo(5);
    HttpEntity<?> resultEntity = result.getFirst("key");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(multipartData);
    assertThat(resultEntity.getHeaders().getFirst("foo")).isEqualTo("bar");
    resultEntity = result.getFirst("logo");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(logo);
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("entity");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo("body");
    assertThat(resultEntity.getHeaders().getFirst("foo")).isEqualTo("bar");
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("publisherClass");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(publisher);
    assertThat(((PublisherEntity<?, ?>) resultEntity).getResolvableType()).isEqualTo(ResolvableType.forClass(String.class));
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("publisherPtr");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(publisher);
    assertThat(((PublisherEntity<?, ?>) resultEntity).getResolvableType()).isEqualTo(ResolvableType.forClass(String.class));
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) PublisherEntity(org.springframework.http.client.MultipartBodyBuilder.PublisherEntity) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)1 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 PublisherEntity (org.springframework.http.client.MultipartBodyBuilder.PublisherEntity)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1