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 695448ae..7c91e4d0 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 @@ -120,7 +120,7 @@ public abstract class CurlDocumentation { MockHttpServletRequest request = this.result.getRequest(); this.writer.print(String.format("curl %s://%s:%d%s", request.getScheme(), request.getRemoteHost(), request.getRemotePort(), - request.getRequestURI())); + getRequestUriWithQueryString(request))); if (this.curlConfiguration.isIncludeResponseHeaders()) { this.writer.print(" -i"); @@ -145,6 +145,11 @@ public abstract class CurlDocumentation { this.writer.println(); } + private String getRequestUriWithQueryString(MockHttpServletRequest request) { + return request.getQueryString() != null ? request.getRequestURI() + "?" + + request.getQueryString() : request.getRequestURI(); + } + private String getContent(MockHttpServletRequest request) throws IOException { StringWriter bodyWriter = new StringWriter(); FileCopyUtils.copy(request.getReader(), bodyWriter); 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 b68f8d50..95011d11 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 @@ -95,6 +95,25 @@ public class CurlDocumentationTests { hasItem("$ curl http://localhost:80/foo -i -d 'content'")); } + @Test + public void requestWitUriQueryString() throws IOException { + documentCurlRequest("request-with-uri-query-string").handle( + new StubMvcResult(new MockHttpServletRequest("GET", "/foo?param=value"), + null)); + assertThat(requestSnippetLines("request-with-uri-query-string"), + hasItem("$ curl http://localhost:80/foo?param=value -i")); + } + + @Test + public void requestWithQueryString() throws IOException { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); + request.setQueryString("param=value"); + documentCurlRequest("request-with-query-string").handle( + new StubMvcResult(request, null)); + assertThat(requestSnippetLines("request-with-query-string"), + hasItem("$ curl http://localhost:80/foo?param=value -i")); + } + @Test public void requestWithHeaders() throws IOException { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");