use of cn.taketoday.core.DefaultMultiValueMap in project today-infrastructure by TAKETODAY.
the class FormHttpMessageConverterTests method writeMultipartCharset.
@Test
public void writeMultipartCharset() throws Exception {
MultiValueMap<String, Object> parts = new DefaultMultiValueMap<>();
Resource logo = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg");
parts.add("logo", logo);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.write(parts, MULTIPART_FORM_DATA, outputMessage);
MediaType contentType = outputMessage.getHeaders().getContentType();
Map<String, String> parameters = contentType.getParameters();
assertThat(parameters).containsOnlyKeys("boundary");
this.converter.setCharset(StandardCharsets.ISO_8859_1);
outputMessage = new MockHttpOutputMessage();
this.converter.write(parts, MULTIPART_FORM_DATA, outputMessage);
parameters = outputMessage.getHeaders().getContentType().getParameters();
assertThat(parameters).containsOnlyKeys("boundary", "charset");
assertThat(parameters).containsEntry("charset", "ISO-8859-1");
}
use of cn.taketoday.core.DefaultMultiValueMap in project today-infrastructure by TAKETODAY.
the class AbstractDataBinderParameterResolver method resolveName.
@Nullable
@Override
protected Object resolveName(String name, ResolvableMethodParameter resolvable, RequestContext context) throws Exception {
final int parameterNameLength = name.length();
// prepare property values
final Map<String, String[]> parameters = context.getParameters();
final DefaultMultiValueMap<String, PropertyValue> propertyValues = new DefaultMultiValueMap<>();
for (final Map.Entry<String, String[]> entry : parameters.entrySet()) {
final String[] paramValues = entry.getValue();
if (ObjectUtils.isNotEmpty(paramValues)) {
final String requestParameterName = entry.getKey();
// users[key].userName=TODAY&users[key].age=20
if (requestParameterName.startsWith(name) && requestParameterName.charAt(parameterNameLength) == '[') {
// userList[0].name '.' 's index
final int separatorIndex = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(requestParameterName);
final String property = requestParameterName.substring(separatorIndex + 1);
final int closeKey = requestParameterName.indexOf(']');
final String key = requestParameterName.substring(parameterNameLength + 1, closeKey);
final PropertyValue propertyValue = new PropertyValue(property, paramValues[0]);
propertyValues.add(key, propertyValue);
}
}
}
return doBind(propertyValues, resolvable);
}
use of cn.taketoday.core.DefaultMultiValueMap 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.core.DefaultMultiValueMap in project today-framework by TAKETODAY.
the class FormHttpMessageWriterTests method writeForm.
@Test
public void writeForm() {
MultiValueMap<String, String> body = new DefaultMultiValueMap<>(new LinkedHashMap<>());
body.set("name 1", "value 1");
body.add("name 2", "value 2+1");
body.add("name 2", "value 2+2");
body.add("name 3", null);
MockServerHttpResponse response = new MockServerHttpResponse(this.bufferFactory);
this.writer.write(Mono.just(body), null, MediaType.APPLICATION_FORM_URLENCODED, response, null).block();
String expected = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
StepVerifier.create(response.getBody()).consumeNextWith(stringConsumer(expected)).expectComplete().verify();
HttpHeaders headers = response.getHeaders();
assertThat(headers.getContentType().toString()).isEqualTo("application/x-www-form-urlencoded;charset=UTF-8");
assertThat(headers.getContentLength()).isEqualTo(expected.length());
}
use of cn.taketoday.core.DefaultMultiValueMap in project today-framework by TAKETODAY.
the class FormHttpMessageConverterTests method writeMultipartOrder.
// SPR-13309
@Test
public void writeMultipartOrder() throws Exception {
MyBean myBean = new MyBean();
myBean.setString("foo");
MultiValueMap<String, Object> parts = new DefaultMultiValueMap<>();
parts.add("part1", myBean);
HttpHeaders entityHeaders = HttpHeaders.create();
entityHeaders.setContentType(APPLICATION_JSON);
HttpEntity<MyBean> entity = new HttpEntity<>(myBean, entityHeaders);
parts.add("part2", entity);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setMultipartCharset(StandardCharsets.UTF_8);
this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);
MediaType contentType = outputMessage.getHeaders().getContentType();
assertThat(contentType.getParameter("boundary")).as("No boundary found").isNotNull();
// see if Commons FileUpload can read what we wrote
FileItemFactory fileItemFactory = new DiskFileItemFactory();
FileUpload fileUpload = new FileUpload(fileItemFactory);
RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
List<FileItem> items = fileUpload.parseRequest(requestContext);
assertThat(items.size()).isEqualTo(2);
FileItem item = items.get(0);
assertThat(item.isFormField()).isTrue();
assertThat(item.getFieldName()).isEqualTo("part1");
assertThat(item.getString()).isEqualTo("{\"string\":\"foo\"}");
}
Aggregations