From 780e81e21f2c743d42a0914b445b44e65fd5fa67 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 23 Oct 2017 23:42:33 -0400 Subject: [PATCH] Add multipart form data test back. --- .../gateway/test/BaseWebClientTests.java | 24 ++++++++++++------- .../gateway/test/FormIntegrationTests.java | 6 ++--- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java index 03a11594..86d2c36c 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java @@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.test; import java.io.IOException; import java.time.Duration; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -38,12 +39,13 @@ import org.springframework.context.annotation.Import; import org.springframework.core.annotation.Order; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.http.codec.multipart.FilePart; import org.springframework.http.codec.multipart.Part; import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.server.ServerWebExchange; @@ -54,6 +56,7 @@ import com.netflix.loadbalancer.ServerList; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_HANDLER_MAPPER_ATTR; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; /** @@ -134,14 +137,17 @@ public class BaseWebClientTests { } @RequestMapping(value = "/post", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public Mono> postFormData(//ServerWebExchange exchange, - @RequestParam Map parts - /*@RequestBody(required = false) String body*/) { - HashMap ret = new HashMap<>(); - // HashMap files = parseMultipart(exchange, null); - - // ret.put("files", files); - return Mono.just(ret); + public Mono> postFormData(@RequestBody Mono> parts) { + // StringDecoder decoder = StringDecoder.allMimeTypes(true); + return parts.flux().flatMap(map -> Flux.fromIterable(map.values())) + .flatMap(map -> Flux.fromIterable(map)) + .filter(part -> part instanceof FilePart) + .reduce(new HashMap(), (files, part) -> { + MediaType contentType = part.headers().getContentType(); + long contentLength = part.headers().getContentLength(); + files.put(part.name(), "data:"+contentType+";base64,"+contentLength); //TODO: get part data + return files; + }).map(files -> Collections.singletonMap("files", files)); } @RequestMapping(path = "/post", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/FormIntegrationTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/FormIntegrationTests.java index cee50250..e99f872e 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/FormIntegrationTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/FormIntegrationTests.java @@ -20,7 +20,6 @@ package org.springframework.cloud.gateway.test; import java.nio.charset.Charset; import java.util.Map; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.SpringBootConfiguration; @@ -76,7 +75,6 @@ public class FormIntegrationTests extends BaseWebClientTests { } @Test - @Ignore //FIXME: java.lang.IllegalStateException: Only one connection receive subscriber allowed. public void multipartFormDataWorks() { ClassPathResource img = new ClassPathResource("1x1.png"); @@ -98,8 +96,8 @@ public class FormIntegrationTests extends BaseWebClientTests { StepVerifier.create(result) .consumeNextWith(map -> { Map files = getMap(map, "files"); - assertThat(files).containsKey("file"); - String file = (String) files.get("file"); + assertThat(files).containsKey("imgpart"); + String file = (String) files.get("imgpart"); assertThat(file).startsWith("data:").contains(";base64,"); }) .expectComplete()