diff --git a/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientRequestConverter.java b/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientRequestConverter.java index 007b15aca..543c94a9c 100644 --- a/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientRequestConverter.java +++ b/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientRequestConverter.java @@ -7,7 +7,9 @@ import java.io.ByteArrayOutputStream; import java.util.Collection; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -20,6 +22,7 @@ import org.springframework.core.io.buffer.DefaultDataBuffer; import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.http.ReactiveHttpInputMessage; import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.multipart.DefaultPartHttpMessageReader; @@ -47,8 +50,26 @@ class WebTestClientRequestConverter implements RequestConverter @Override public OperationRequest convert(ExchangeResult result) { HttpHeaders headers = extractRequestHeaders(result); - return new OperationRequestFactory().create(result.getUrl(), result.getMethod(), result.getRequestBodyContent(), - headers, extractRequestParts(result), extractCookies(headers)); + List parts = extractRequestParts(result); + byte[] content = result.getRequestBodyContent(); + if (parts != null && !parts.isEmpty()) { + // Describe the request by its parts, as MockMvcRequestConverter does. The + // boundary belongs to the raw body so it's removed along with it. + content = new byte[0]; + removeBoundary(headers); + } + return new OperationRequestFactory().create(result.getUrl(), result.getMethod(), content, headers, parts, + extractCookies(headers)); + } + + private void removeBoundary(HttpHeaders headers) { + MediaType contentType = headers.getContentType(); + if (contentType != null) { + Map parameters = new LinkedHashMap<>(contentType.getParameters()); + if (parameters.keySet().removeIf("boundary"::equalsIgnoreCase)) { + headers.setContentType(new MediaType(contentType, parameters)); + } + } } private HttpHeaders extractRequestHeaders(ExchangeResult result) { diff --git a/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientRequestConverterTests.java b/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientRequestConverterTests.java index e7a807e46..31d53f83e 100644 --- a/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientRequestConverterTests.java +++ b/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientRequestConverterTests.java @@ -257,6 +257,8 @@ void multipartUpload() { OperationRequest request = this.converter.convert(result); assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo")); assertThat(request.getMethod()).isEqualTo(HttpMethod.POST); + assertThat(request.getContent()).isEmpty(); + assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.MULTIPART_FORM_DATA); assertThat(request.getParts()).hasSize(1); OperationRequestPart part = request.getParts().iterator().next(); assertThat(part.getName()).isEqualTo("file"); @@ -307,6 +309,27 @@ public String getFilename() { assertThat(part.getContent()).containsExactly(1, 2, 3, 4); } + @Test + void multipartContentTypeWithBodyThatCannotBeParsedIsUnchanged() { + ExchangeResult result = WebTestClient + .bindToRouterFunction(RouterFunctions.route(POST("/foo"), (req) -> ServerResponse.ok().build())) + .configureClient() + .baseUrl("http://localhost") + .build() + .post() + .uri("/foo") + .contentType(MediaType.parseMediaType("multipart/form-data;boundary=example")) + .bodyValue("not multipart") + .exchange() + .expectBody() + .returnResult(); + OperationRequest request = this.converter.convert(result); + assertThat(request.getParts()).isEmpty(); + assertThat(request.getContentAsString()).isEqualTo("not multipart"); + assertThat(request.getHeaders().getContentType()) + .isEqualTo(MediaType.parseMediaType("multipart/form-data;boundary=example")); + } + @Test void requestWithCookies() { ExchangeResult result = WebTestClient diff --git a/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientRestDocumentationIntegrationTests.java b/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientRestDocumentationIntegrationTests.java index 190f8bf73..71ab8c7fb 100644 --- a/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientRestDocumentationIntegrationTests.java +++ b/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientRestDocumentationIntegrationTests.java @@ -32,6 +32,7 @@ import org.springframework.restdocs.templates.TemplateFormats; import org.springframework.restdocs.testfixtures.SnippetConditions; import org.springframework.restdocs.testfixtures.SnippetConditions.CodeBlockCondition; +import org.springframework.restdocs.testfixtures.SnippetConditions.HttpRequestCondition; import org.springframework.restdocs.testfixtures.SnippetConditions.HttpResponseCondition; import org.springframework.restdocs.testfixtures.SnippetConditions.TableCondition; import org.springframework.test.web.reactive.server.EntityExchangeResult; @@ -40,6 +41,7 @@ import org.springframework.util.FileSystemUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.reactive.function.BodyExtractors; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.server.RequestPredicates; @@ -157,6 +159,25 @@ void multipart() { assertThat(new File("build/generated-snippets/multipart/request-parts.adoc")) .has(content(tableWithHeader(TemplateFormats.asciidoctor(), "Part", "Description").row("`a`", "Part a") .row("`b`", "Part b"))); + assertThat(new File("build/generated-snippets/multipart/curl-request.adoc")) + .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash") + .withContent(String.format("$ curl 'https://api.example.com/upload' -i -X POST \\%n" + + " -H 'Content-Type: multipart/form-data' \\%n" + + " -F 'a=alpha;type=text/plain;charset=UTF-8' \\%n" + + " -F 'b=bravo;type=text/plain;charset=UTF-8'")))); + assertThat(new File("build/generated-snippets/multipart/httpie-request.adoc")) + .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash") + .withContent(String.format("$ http --multipart POST 'https://api.example.com/upload' \\%n" + + " 'a'='alpha' \\%n" + " 'b'='bravo'")))); + String boundary = "6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm"; + assertThat(new File("build/generated-snippets/multipart/http-request.adoc")) + .has(content(httpRequest(TemplateFormats.asciidoctor(), RequestMethod.POST, "/upload") + .header("Content-Type", "multipart/form-data; boundary=" + boundary) + .header("Host", "api.example.com") + .content(String.format("--%1$s%nContent-Disposition: form-data; name=a%n" + + "Content-Type: text/plain;charset=UTF-8%n%nalpha%n" + + "--%1$s%nContent-Disposition: form-data; name=b%n" + + "Content-Type: text/plain;charset=UTF-8%n%nbravo%n--%1$s--", boundary)))); } @Test @@ -266,6 +287,10 @@ private CodeBlockCondition codeBlock(TemplateFormat format, String language) return SnippetConditions.codeBlock(format, language); } + private HttpRequestCondition httpRequest(TemplateFormat format, RequestMethod requestMethod, String uri) { + return SnippetConditions.httpRequest(format, requestMethod, uri); + } + private HttpResponseCondition httpResponse(TemplateFormat format, HttpStatus status) { return SnippetConditions.httpResponse(format, status); }