Search in sources :

Example 16 with MultiValueMap

use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.

the class MultipartHttpMessageWriterTests method writeMultipartFormData.

@Test
public void writeMultipartFormData() throws Exception {
    Resource logo = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg");
    Resource utf8 = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg") {

        @Override
        public String getName() {
            // SPR-12108
            return "Hall\u00F6le.jpg";
        }
    };
    Flux<DataBuffer> bufferPublisher = Flux.just(this.bufferFactory.wrap("Aa".getBytes(StandardCharsets.UTF_8)), this.bufferFactory.wrap("Bb".getBytes(StandardCharsets.UTF_8)), this.bufferFactory.wrap("Cc".getBytes(StandardCharsets.UTF_8)));
    FilePart mockPart = mock(FilePart.class);
    HttpHeaders partHeaders = HttpHeaders.create();
    partHeaders.setContentType(MediaType.TEXT_PLAIN);
    partHeaders.setContentDispositionFormData("foo", "file.txt");
    partHeaders.add("foo", "bar");
    given(mockPart.headers()).willReturn(partHeaders);
    given(mockPart.content()).willReturn(bufferPublisher);
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("name 1", "value 1");
    bodyBuilder.part("name 2", "value 2+1");
    bodyBuilder.part("name 2", "value 2+2");
    bodyBuilder.part("logo", logo);
    bodyBuilder.part("utf8", utf8);
    bodyBuilder.part("json", new Foo("bar"), MediaType.APPLICATION_JSON);
    bodyBuilder.asyncPart("publisher", Flux.just("foo", "bar", "baz"), String.class);
    bodyBuilder.part("filePublisher", mockPart);
    Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
    Map<String, Object> hints = Collections.emptyMap();
    this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block(Duration.ofSeconds(5));
    MultiValueMap<String, Part> requestParts = parse(this.response, hints);
    assertThat(requestParts.size()).isEqualTo(7);
    Part part = requestParts.getFirst("name 1");
    assertThat(part instanceof FormFieldPart).isTrue();
    assertThat(part.name()).isEqualTo("name 1");
    assertThat(((FormFieldPart) part).value()).isEqualTo("value 1");
    List<Part> parts2 = requestParts.get("name 2");
    assertThat(parts2.size()).isEqualTo(2);
    part = parts2.get(0);
    assertThat(part instanceof FormFieldPart).isTrue();
    assertThat(part.name()).isEqualTo("name 2");
    assertThat(((FormFieldPart) part).value()).isEqualTo("value 2+1");
    part = parts2.get(1);
    assertThat(part instanceof FormFieldPart).isTrue();
    assertThat(part.name()).isEqualTo("name 2");
    assertThat(((FormFieldPart) part).value()).isEqualTo("value 2+2");
    part = requestParts.getFirst("logo");
    assertThat(part instanceof FilePart).isTrue();
    assertThat(part.name()).isEqualTo("logo");
    assertThat(((FilePart) part).filename()).isEqualTo("logo.jpg");
    assertThat(part.headers().getContentType()).isEqualTo(MediaType.IMAGE_JPEG);
    assertThat(part.headers().getContentLength()).isEqualTo(logo.getFile().length());
    part = requestParts.getFirst("utf8");
    assertThat(part instanceof FilePart).isTrue();
    assertThat(part.name()).isEqualTo("utf8");
    assertThat(((FilePart) part).filename()).isEqualTo("Hall\u00F6le.jpg");
    assertThat(part.headers().getContentType()).isEqualTo(MediaType.IMAGE_JPEG);
    assertThat(part.headers().getContentLength()).isEqualTo(utf8.getFile().length());
    part = requestParts.getFirst("json");
    assertThat(part).isNotNull();
    assertThat(part.name()).isEqualTo("json");
    assertThat(part.headers().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
    String value = decodeToString(part);
    assertThat(value).isEqualTo("{\"bar\":\"bar\"}");
    part = requestParts.getFirst("publisher");
    assertThat(part).isNotNull();
    assertThat(part.name()).isEqualTo("publisher");
    value = decodeToString(part);
    assertThat(value).isEqualTo("foobarbaz");
    part = requestParts.getFirst("filePublisher");
    assertThat(part).isNotNull();
    assertThat(part.name()).isEqualTo("filePublisher");
    assertThat(part.headers()).containsEntry("foo", Collections.singletonList("bar"));
    assertThat(((FilePart) part).filename()).isEqualTo("file.txt");
    value = decodeToString(part);
    assertThat(value).isEqualTo("AaBbCc");
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) MultipartBodyBuilder(cn.taketoday.http.client.MultipartBodyBuilder) MultiValueMap(cn.taketoday.core.MultiValueMap) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 17 with MultiValueMap

use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.

the class MultipartHttpMessageWriterTests method writeMultipartRelated.

// gh-24582
@Test
public void writeMultipartRelated() {
    MediaType mediaType = MediaType.parseMediaType("multipart/related;type=foo");
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("name 1", "value 1");
    bodyBuilder.part("name 2", "value 2");
    Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
    Map<String, Object> hints = Collections.emptyMap();
    this.writer.write(result, null, mediaType, this.response, hints).block(Duration.ofSeconds(5));
    MediaType contentType = this.response.getHeaders().getContentType();
    assertThat(contentType).isNotNull();
    assertThat(contentType.isCompatibleWith(mediaType)).isTrue();
    assertThat(contentType.getParameter("type")).isEqualTo("foo");
    assertThat(contentType.getParameter("boundary")).isNotEmpty();
    assertThat(contentType.getParameter("charset")).isNull();
    MultiValueMap<String, Part> requestParts = parse(this.response, hints);
    assertThat(requestParts.size()).isEqualTo(2);
    assertThat(requestParts.getFirst("name 1").name()).isEqualTo("name 1");
    assertThat(requestParts.getFirst("name 2").name()).isEqualTo("name 2");
}
Also used : MultipartBodyBuilder(cn.taketoday.http.client.MultipartBodyBuilder) MediaType(cn.taketoday.http.MediaType) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 18 with MultiValueMap

use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.

the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapOfString.

@Test
public void resolveMultiValueMapOfString() throws Throwable {
    String name = "foo";
    String value1 = "bar";
    String value2 = "baz";
    request.addParameter(name, value1, value2);
    MultiValueMap<String, String> expected = new LinkedMultiValueMap<>(1);
    expected.add(name, value1);
    expected.add(name, value2);
    ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultiValueMap.class, String.class, String.class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof MultiValueMap;
    assertThat(condition).isTrue();
    assertThat(result).as("Invalid result").isEqualTo(expected);
}
Also used : RequestParam(cn.taketoday.web.annotation.RequestParam) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 19 with MultiValueMap

use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.

the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapOfMultipartFile.

@Test
@SuppressWarnings("unchecked")
public void resolveMultiValueMapOfMultipartFile() throws Throwable {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("mfilelist", "Hello World 1".getBytes());
    MultipartFile expected2 = new MockMultipartFile("mfilelist", "Hello World 2".getBytes());
    MultipartFile expected3 = new MockMultipartFile("other", "Hello World 3".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    request.addFile(expected3);
    webRequest = new ServletRequestContext(null, request, null);
    ResolvableMethodParameter param = this.testMethod.annot(requestParam().noName()).arg(MultiValueMap.class, String.class, MultipartFile.class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof MultiValueMap;
    assertThat(condition).isTrue();
    MultiValueMap<String, MultipartFile> resultMap = (MultiValueMap<String, MultipartFile>) result;
    assertThat(resultMap.size()).isEqualTo(2);
    assertThat(resultMap.get("mfilelist").size()).isEqualTo(2);
    assertThat(resultMap.get("mfilelist").get(0)).isEqualTo(expected1);
    assertThat(resultMap.get("mfilelist").get(1)).isEqualTo(expected2);
    assertThat(resultMap.get("other").size()).isEqualTo(1);
    assertThat(resultMap.get("other").get(0)).isEqualTo(expected3);
}
Also used : MockMultipartFile(cn.taketoday.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(cn.taketoday.web.testfixture.servlet.MockMultipartFile) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MockMultipartHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 20 with MultiValueMap

use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.

the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapOfPart.

@Test
@SuppressWarnings("unchecked")
public void resolveMultiValueMapOfPart() throws Throwable {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContentType("multipart/form-data");
    Part expected1 = new MockPart("mfilelist", "Hello World 1".getBytes());
    Part expected2 = new MockPart("mfilelist", "Hello World 2".getBytes());
    Part expected3 = new MockPart("other", "Hello World 3".getBytes());
    request.addPart(expected1);
    request.addPart(expected2);
    request.addPart(expected3);
    request.setMethod("POST");
    webRequest = new ServletRequestContext(null, request, null);
    ResolvableMethodParameter param = this.testMethod.annot(requestParam().noName()).arg(MultiValueMap.class, String.class, Part.class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof MultiValueMap;
    assertThat(condition).isTrue();
    MultiValueMap<String, Part> resultMap = (MultiValueMap<String, Part>) result;
    assertThat(resultMap.size()).isEqualTo(2);
    assertThat(resultMap.get("mfilelist").size()).isEqualTo(2);
    assertThat(resultMap.get("mfilelist").get(0)).isEqualTo(expected1);
    assertThat(resultMap.get("mfilelist").get(1)).isEqualTo(expected2);
    assertThat(resultMap.get("other").size()).isEqualTo(1);
    assertThat(resultMap.get("other").get(0)).isEqualTo(expected3);
}
Also used : MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) Part(jakarta.servlet.http.Part) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Aggregations

MultiValueMap (cn.taketoday.core.MultiValueMap)46 Test (org.junit.jupiter.api.Test)27 Map (java.util.Map)17 List (java.util.List)13 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)12 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)10 Nullable (cn.taketoday.lang.Nullable)9 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)8 HttpHeaders (cn.taketoday.http.HttpHeaders)8 MultipartBodyBuilder (cn.taketoday.http.client.MultipartBodyBuilder)8 MethodParameter (cn.taketoday.core.MethodParameter)7 Resource (cn.taketoday.core.io.Resource)6 MatrixParam (cn.taketoday.web.annotation.MatrixParam)5 ArrayList (java.util.ArrayList)5 BeanMetadata (cn.taketoday.beans.BeanMetadata)4 PropertyValue (cn.taketoday.beans.PropertyValue)4 PropertyValues (cn.taketoday.beans.PropertyValues)4 DefaultMultiValueMap (cn.taketoday.core.DefaultMultiValueMap)4 ResolvableType (cn.taketoday.core.ResolvableType)4 TypeDescriptor (cn.taketoday.core.TypeDescriptor)4