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 882806a6..ee7f3cb9 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 @@ -160,6 +160,13 @@ public abstract class CurlDocumentation { if (request.getContentLengthLong() > 0) { this.writer.print(String.format(" -d '%s'", getContent(request))); } + else if (isPostRequest(request)) { + Map parameters = request.getParameterMap(); + if (!parameters.isEmpty()) { + this.writer.print(String + .format(" -d '%s'", toQueryString(parameters))); + } + } this.writer.println(); } @@ -168,6 +175,10 @@ public abstract class CurlDocumentation { return RequestMethod.GET == RequestMethod.valueOf(request.getMethod()); } + private boolean isPostRequest(HttpServletRequest request) { + return RequestMethod.POST == RequestMethod.valueOf(request.getMethod()); + } + private boolean isNonStandardPort(HttpServletRequest request) { return (SCHEME_HTTP.equals(request.getScheme()) && request.getRemotePort() != STANDARD_PORT_HTTP) || (SCHEME_HTTPS.equals(request.getScheme()) && request 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 36735028..cdf7a9b8 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 @@ -147,6 +147,39 @@ public class CurlDocumentationTests { hasItem("$ curl http://localhost/foo?k1=foo+bar%26 -i")); } + @Test + public void postRequestWithOneParameter() throws IOException { + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo"); + request.addParameter("k1", "v1"); + documentCurlRequest("post-request-with-one-parameter").handle( + new StubMvcResult(request, null)); + assertThat(requestSnippetLines("post-request-with-one-parameter"), + hasItem("$ curl http://localhost/foo -i -X POST -d 'k1=v1'")); + } + + @Test + public void postRequestWithMultipleParameters() throws IOException { + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo"); + request.addParameter("k1", "v1"); + request.addParameter("k2", "v2"); + request.addParameter("k1", "v1-bis"); + documentCurlRequest("post-request-with-multiple-parameters").handle( + new StubMvcResult(request, null)); + assertThat( + requestSnippetLines("post-request-with-multiple-parameters"), + hasItem("$ curl http://localhost/foo -i -X POST -d 'k1=v1\\&k1=v1-bis\\&k2=v2'")); + } + + @Test + public void postRequestWithUrlEncodedParameter() throws IOException { + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo"); + request.addParameter("k1", "a&b"); + documentCurlRequest("post-request-with-url-encoded-parameter").handle( + new StubMvcResult(request, null)); + assertThat(requestSnippetLines("post-request-with-url-encoded-parameter"), + hasItem("$ curl http://localhost/foo -i -X POST -d 'k1=a%26b'")); + } + @Test public void requestWithHeaders() throws IOException { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");