diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java b/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java index 73c34f50..858690bd 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java +++ b/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java @@ -109,7 +109,8 @@ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { * @return {@code this} */ public RestDocumentationConfigurer withContextPath(String contextPath) { - this.contextPath = contextPath; + this.contextPath = (StringUtils.hasText(contextPath) && !contextPath + .startsWith("/")) ? "/" + contextPath : contextPath; return this; } 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 7a38d867..b7eb7733 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 @@ -90,7 +90,9 @@ public abstract class CurlDocumentation { } if (StringUtils.hasText(request.getContextPath())) { - this.writer.print(String.format("/%s", request.getContextPath())); + this.writer.print(String.format( + request.getContextPath().startsWith("/") ? "%s" : "/%s", + request.getContextPath())); } this.writer.print(request.getRequestUriWithQueryString().replace("&", "\\&")); diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java b/spring-restdocs/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java index 729ee018..1ec54d30 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java +++ b/spring-restdocs/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java @@ -78,12 +78,23 @@ public class RestDocumentationConfigurerTests { } @Test - public void customContextPath() { + public void customContextPathWithoutSlash() { String contextPath = "context-path"; RequestPostProcessor postProcessor = new RestDocumentationConfigurer() .withContextPath(contextPath).beforeMockMvcCreated(null, null); postProcessor.postProcessRequest(this.request); + assertUriConfiguration("http", "localhost", 8080); + assertThat(this.request.getContextPath(), equalTo("/" + contextPath)); + } + + @Test + public void customContextPathWithSlash() { + String contextPath = "/context-path"; + RequestPostProcessor postProcessor = new RestDocumentationConfigurer() + .withContextPath(contextPath).beforeMockMvcCreated(null, null); + postProcessor.postProcessRequest(this.request); + assertUriConfiguration("http", "localhost", 8080); assertThat(this.request.getContextPath(), equalTo(contextPath)); } 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 0cebc646..83a7ff4f 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 @@ -221,13 +221,24 @@ public class CurlDocumentationTests { } @Test - public void requestWithContextPath() throws IOException { + public void requestWithContextPathWithSlash() throws IOException { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); + request.setServerName("api.example.com"); + request.setContextPath("/v3"); + documentCurlRequest("request-with-custom-context-with-slash").handle( + new StubMvcResult(request, null)); + assertThat(requestSnippetLines("request-with-custom-context-with-slash"), + hasItem("$ curl http://api.example.com/v3/foo -i")); + } + + @Test + public void requestWithContextPathWithoutSlash() throws IOException { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.setServerName("api.example.com"); request.setContextPath("v3"); - documentCurlRequest("request-with-custom-context").handle( + documentCurlRequest("request-with-custom-context-without-slash").handle( new StubMvcResult(request, null)); - assertThat(requestSnippetLines("request-with-custom-context"), + assertThat(requestSnippetLines("request-with-custom-context-without-slash"), hasItem("$ curl http://api.example.com/v3/foo -i")); }