From 4c31dc0216e9e675c88692ff0f1d895efd0d6a5e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 20 Jul 2015 16:03:26 +0100 Subject: [PATCH] Fix request parameters in curl snippet for multipart requests Previously, request parameters would be provided via -d. This is incorrect for a multipart request that is also using -F. This commit updates the generation of the curl request snippet to use -F for request parameters when the request is a multipart request. Closes gh-93 --- .../restdocs/curl/CurlDocumentation.java | 8 ++++++++ .../util/DocumentableHttpServletRequest.java | 9 +++++++++ .../restdocs/curl/CurlDocumentationTests.java | 15 +++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/curl/CurlDocumentation.java b/spring-restdocs/src/main/java/org/springframework/restdocs/curl/CurlDocumentation.java index 45e4d081..65368dc6 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/curl/CurlDocumentation.java +++ b/spring-restdocs/src/main/java/org/springframework/restdocs/curl/CurlDocumentation.java @@ -176,6 +176,14 @@ public abstract class CurlDocumentation { this.writer .print(String.format(" -d '%s'", request.getContentAsString())); } + else if (request.isMultipartRequest()) { + for (Entry entry : request.getParameterMap().entrySet()) { + for (String value : entry.getValue()) { + this.writer.print(String.format(" -F '%s=%s'", entry.getKey(), + value)); + } + } + } else if (request.isPostRequest() || request.isPutRequest()) { String queryString = request.getParameterMapAsQueryString(); if (StringUtils.hasText(queryString)) { diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/util/DocumentableHttpServletRequest.java b/spring-restdocs/src/main/java/org/springframework/restdocs/util/DocumentableHttpServletRequest.java index 4f7e6e06..0aff84b7 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/util/DocumentableHttpServletRequest.java +++ b/spring-restdocs/src/main/java/org/springframework/restdocs/util/DocumentableHttpServletRequest.java @@ -230,6 +230,15 @@ public class DocumentableHttpServletRequest { return this.delegate.getContextPath(); } + /** + * Returns a map of the request's parameters + * @return The map of parameters + * @see HttpServletRequest#getParameterMap() + */ + public Map getParameterMap() { + return this.delegate.getParameterMap(); + } + private String getQueryString() { if (this.delegate.getQueryString() != null) { return this.delegate.getQueryString(); diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/curl/CurlDocumentationTests.java b/spring-restdocs/src/test/java/org/springframework/restdocs/curl/CurlDocumentationTests.java index faa25a0f..3a359ef2 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/curl/CurlDocumentationTests.java +++ b/spring-restdocs/src/test/java/org/springframework/restdocs/curl/CurlDocumentationTests.java @@ -287,4 +287,19 @@ public class CurlDocumentationTests { result(fileUpload("/upload").file(multipartFile))); } + @Test + public void multipartPostWithParameters() throws IOException { + String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H " + + "'Content-Type: multipart/form-data' -F " + + "'image=@documents/images/example.png' -F 'a=apple' -F 'a=avocado' " + + "-F 'b=banana'"; + this.snippet.expectCurlRequest("multipart-post").withContents( + codeBlock("bash").content(expectedContent)); + MockMultipartFile multipartFile = new MockMultipartFile("image", + "documents/images/example.png", null, "bytes".getBytes()); + documentCurlRequest("multipart-post").handle( + result(fileUpload("/upload").file(multipartFile) + .param("a", "apple", "avocado").param("b", "banana"))); + } + }