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");
}
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");
}
Aggregations