Search in sources :

Example 1 with PublisherEntity

use of cn.taketoday.http.client.MultipartBodyBuilder.PublisherEntity in project today-infrastructure by TAKETODAY.

the class MultipartBodyBuilderTests method builder.

@Test
public void builder() {
    MultipartBodyBuilder builder = new MultipartBodyBuilder();
    MultiValueMap<String, String> multipartData = new DefaultMultiValueMap<>();
    multipartData.add("form field", "form value");
    builder.part("key", multipartData).header("foo", "bar");
    Resource logo = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg");
    builder.part("logo", logo).header("baz", "qux");
    HttpHeaders entityHeaders = HttpHeaders.create();
    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 TypeReference<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.fromClass(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.fromClass(String.class));
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) HttpEntity(cn.taketoday.http.HttpEntity) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) TypeReference(cn.taketoday.core.TypeReference) PublisherEntity(cn.taketoday.http.client.MultipartBodyBuilder.PublisherEntity) Test(org.junit.jupiter.api.Test)

Example 2 with PublisherEntity

use of cn.taketoday.http.client.MultipartBodyBuilder.PublisherEntity in project today-framework by TAKETODAY.

the class MultipartBodyBuilderTests method builder.

@Test
public void builder() {
    MultipartBodyBuilder builder = new MultipartBodyBuilder();
    MultiValueMap<String, String> multipartData = new DefaultMultiValueMap<>();
    multipartData.add("form field", "form value");
    builder.part("key", multipartData).header("foo", "bar");
    Resource logo = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg");
    builder.part("logo", logo).header("baz", "qux");
    HttpHeaders entityHeaders = HttpHeaders.create();
    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 TypeReference<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.fromClass(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.fromClass(String.class));
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) HttpEntity(cn.taketoday.http.HttpEntity) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) TypeReference(cn.taketoday.core.TypeReference) PublisherEntity(cn.taketoday.http.client.MultipartBodyBuilder.PublisherEntity) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultMultiValueMap (cn.taketoday.core.DefaultMultiValueMap)2 TypeReference (cn.taketoday.core.TypeReference)2 ClassPathResource (cn.taketoday.core.io.ClassPathResource)2 Resource (cn.taketoday.core.io.Resource)2 HttpEntity (cn.taketoday.http.HttpEntity)2 HttpHeaders (cn.taketoday.http.HttpHeaders)2 PublisherEntity (cn.taketoday.http.client.MultipartBodyBuilder.PublisherEntity)2 Test (org.junit.jupiter.api.Test)2