Ensure that a context path without a leading slash is handled correctly
A context path should either be an empty String, or a String that begins with a slash. This commit updates both CurlDocumentation and RestDocumentationConfigurer to prepend a / to the specified context path when required. See gh-49
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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("&", "\\&"));
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user