Allow requests to be configured with a custom context path

Closes gh-49
Closes gh-27
This commit is contained in:
Dmitriy Mayboroda
2015-04-16 11:12:00 +03:00
committed by Andy Wilkinson
parent bcda6fe459
commit a5e26a57d8
6 changed files with 64 additions and 3 deletions

4
.gitignore vendored
View File

@@ -4,4 +4,6 @@
.settings
bin
build
target
target
.idea
*.iml

View File

@@ -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 {
};
}
}

View File

@@ -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");

View File

@@ -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();

View File

@@ -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(

View File

@@ -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<String> requestSnippetLines(String snippetName) throws IOException {
return snippetLines(snippetName, "curl-request");
}