Search in sources :

Example 1 with DefaultMultiValueMap

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");
}
Also used : DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) MediaType(cn.taketoday.http.MediaType) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultMultiValueMap

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);
}
Also used : DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) PropertyValue(cn.taketoday.beans.PropertyValue) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) Nullable(cn.taketoday.lang.Nullable)

Example 3 with DefaultMultiValueMap

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");
}
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 4 with DefaultMultiValueMap

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());
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) MockServerHttpResponse(cn.taketoday.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultMultiValueMap

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\"}");
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) HttpEntity(cn.taketoday.http.HttpEntity) MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) DiskFileItemFactory(org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.tomcat.util.http.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.tomcat.util.http.fileupload.FileItem) MediaType(cn.taketoday.http.MediaType) RequestContext(org.apache.tomcat.util.http.fileupload.RequestContext) FileUpload(org.apache.tomcat.util.http.fileupload.FileUpload) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultMultiValueMap (cn.taketoday.core.DefaultMultiValueMap)14 Test (org.junit.jupiter.api.Test)8 MultiValueMap (cn.taketoday.core.MultiValueMap)6 HttpHeaders (cn.taketoday.http.HttpHeaders)6 Map (java.util.Map)6 ClassPathResource (cn.taketoday.core.io.ClassPathResource)4 Resource (cn.taketoday.core.io.Resource)4 HttpEntity (cn.taketoday.http.HttpEntity)4 MediaType (cn.taketoday.http.MediaType)4 MockHttpOutputMessage (cn.taketoday.http.MockHttpOutputMessage)4 PropertyValue (cn.taketoday.beans.PropertyValue)2 TypeReference (cn.taketoday.core.TypeReference)2 HttpCookie (cn.taketoday.http.HttpCookie)2 PublisherEntity (cn.taketoday.http.client.MultipartBodyBuilder.PublisherEntity)2 MockServerHttpResponse (cn.taketoday.http.server.reactive.MockServerHttpResponse)2 Nullable (cn.taketoday.lang.Nullable)2 Module (com.fasterxml.jackson.databind.Module)2 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)2 JacksonXmlModule (com.fasterxml.jackson.dataformat.xml.JacksonXmlModule)2 Cookie (io.netty.handler.codec.http.cookie.Cookie)2