From a5e26a57d849aee9f02f9ada96501a49e785bfb5 Mon Sep 17 00:00:00 2001 From: Dmitriy Mayboroda Date: Thu, 16 Apr 2015 11:12:00 +0300 Subject: [PATCH] Allow requests to be configured with a custom context path Closes gh-49 Closes gh-27 --- .gitignore | 4 +++- .../config/RestDocumentationConfigurer.java | 24 +++++++++++++++++-- .../restdocs/curl/CurlDocumentation.java | 5 ++++ .../util/DocumentableHttpServletRequest.java | 10 ++++++++ .../RestDocumentationConfigurerTests.java | 12 ++++++++++ .../restdocs/curl/CurlDocumentationTests.java | 12 ++++++++++ 6 files changed, 64 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 91fa34ba..19e5b7d9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ .settings bin build -target \ No newline at end of file +target +.idea +*.iml \ No newline at end of file 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 57b37f98..73c34f50 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 @@ -28,8 +28,8 @@ import org.springframework.web.context.WebApplicationContext; * A {@link MockMvcConfigurer} that can be used to configure the documentation * * @author Andy Wilkinson + * @author Dmitriy Mayboroda * @see ConfigurableMockMvcBuilder#apply(MockMvcConfigurer) - * */ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { @@ -51,12 +51,20 @@ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { */ public static final int DEFAULT_PORT = 8080; + /** + * The default context path for documented URIs + * @see #withContextPath(String) + */ + public static final String DEFAULT_CONTEXT_PATH = ""; + private String scheme = DEFAULT_SCHEME; private String host = DEFAULT_HOST; private int port = DEFAULT_PORT; + private String contextPath = DEFAULT_CONTEXT_PATH; + /** * Configures any documented URIs to use the given {@code scheme}. The default is * {@code http}. @@ -93,6 +101,18 @@ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { return this; } + /** + * Configures any documented URIs to use the given {@code contextPath}. The default is + * an empty string. + * + * @param The context path + * @return {@code this} + */ + public RestDocumentationConfigurer withContextPath(String contextPath) { + this.contextPath = contextPath; + return this; + } + @Override public RequestPostProcessor beforeMockMvcCreated( ConfigurableMockMvcBuilder builder, WebApplicationContext context) { @@ -109,6 +129,7 @@ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { request.setScheme(RestDocumentationConfigurer.this.scheme); request.setServerPort(RestDocumentationConfigurer.this.port); request.setServerName(RestDocumentationConfigurer.this.host); + request.setContextPath(RestDocumentationConfigurer.this.contextPath); configureContentLengthHeaderIfAppropriate(request); return request; } @@ -124,5 +145,4 @@ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { }; } - } 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 960ba88c..7a38d867 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 @@ -33,6 +33,7 @@ import org.springframework.util.StringUtils; * * @author Andy Wilkinson * @author Yann Le Guern + * @author Dmitriy Mayboroda */ public abstract class CurlDocumentation { @@ -88,6 +89,10 @@ public abstract class CurlDocumentation { this.writer.print(String.format(":%d", request.getPort())); } + if (StringUtils.hasText(request.getContextPath())) { + this.writer.print(String.format("/%s", request.getContextPath())); + } + this.writer.print(request.getRequestUriWithQueryString().replace("&", "\\&")); this.writer.print(" -i"); 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 03fa98a4..3d228cab 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 @@ -186,6 +186,16 @@ public class DocumentableHttpServletRequest { return toQueryString(this.delegate.getParameterMap()); } + /** + * Returns the request's context path + * + * @return The context path of the request + * @see HttpServletRequest#getContextPath() + */ + public String getContextPath() { + return this.delegate.getContextPath(); + } + private String getQueryString() { if (this.delegate.getQueryString() != null) { return this.delegate.getQueryString(); 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 f5c910a3..729ee018 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 @@ -35,6 +35,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; * Tests for {@link RestDocumentationConfigurer}. * * @author Andy Wilkinson + * @author Dmitriy Mayboroda */ public class RestDocumentationConfigurerTests { @@ -76,6 +77,17 @@ public class RestDocumentationConfigurerTests { assertUriConfiguration("http", "localhost", 8081); } + @Test + public void customContextPath() { + 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 noContentLengthHeaderWhenRequestHasNotContent() { RequestPostProcessor postProcessor = new RestDocumentationConfigurer().withPort( 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 277603c6..0cebc646 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 @@ -39,6 +39,7 @@ import org.springframework.restdocs.StubMvcResult; * * @author Andy Wilkinson * @author Yann Le Guern + * @author Dmitriy Mayboroda */ public class CurlDocumentationTests { @@ -219,6 +220,17 @@ public class CurlDocumentationTests { hasItem("$ curl http://api.example.com/foo -i")); } + @Test + public void requestWithContextPath() throws IOException { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); + request.setServerName("api.example.com"); + request.setContextPath("v3"); + documentCurlRequest("request-with-custom-context").handle( + new StubMvcResult(request, null)); + assertThat(requestSnippetLines("request-with-custom-context"), + hasItem("$ curl http://api.example.com/v3/foo -i")); + } + private List requestSnippetLines(String snippetName) throws IOException { return snippetLines(snippetName, "curl-request"); }