From 23a638817795a913396e86c4665ea4a8998a670e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 16 Mar 2015 14:45:54 +0000 Subject: [PATCH] Escape & characaters in curl request URLs Without escaping, the & will cause bash to truncate the URL and background the curl process. Closes gh-35 --- .../org/springframework/restdocs/curl/CurlDocumentation.java | 4 ++-- .../springframework/restdocs/curl/CurlDocumentationTests.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 cd738df7..882806a6 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 @@ -186,7 +186,7 @@ public abstract class CurlDocumentation { private String getQueryString(HttpServletRequest request) { if (request.getQueryString() != null) { - return request.getQueryString(); + return request.getQueryString().replace("&", "\\&"); } if (isGetRequest(request)) { return toQueryString(request.getParameterMap()); @@ -199,7 +199,7 @@ public abstract class CurlDocumentation { for (Map.Entry entry : map.entrySet()) { for (String value : entry.getValue()) { if (sb.length() > 0) { - sb.append("&"); + sb.append("\\&"); } sb.append(urlEncodeUTF8(entry.getKey())).append('=') .append(urlEncodeUTF8(value)); 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 caaa90e6..36735028 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 @@ -134,7 +134,7 @@ public class CurlDocumentationTests { documentCurlRequest("request-with-multiple-parameters").handle( new StubMvcResult(request, null)); assertThat(requestSnippetLines("request-with-multiple-parameters"), - hasItem("$ curl http://localhost/foo?k1=v1&k1=v1-bis&k2=v2 -i")); + hasItem("$ curl http://localhost/foo?k1=v1\\&k1=v1-bis\\&k2=v2 -i")); } @Test