Apply custom host and port configuration correctly

Previously, a custom host was configured by setting the request’s
remote host. This is in correct as the remote host as the client’s host.
The custom port was also being configured by setting the request’s
remote port and server port. Only the latter is required.

This commit correct the logic by setting the request’s server name and
server port when configuring a custom host and custom port respectively.
The tests have been improved by introducing the use of Spring HATEOAS’s
BasicLinkBuilder to verify that the configured request produces the
expected scheme, host and port in any generated links.

Closes gh-44
This commit is contained in:
Andy Wilkinson
2015-03-25 14:40:27 +00:00
parent 976dd00365
commit 86b431018d
5 changed files with 37 additions and 12 deletions

View File

@@ -5,6 +5,7 @@ project(':spring-restdocs') {
jacocoVersion = '0.7.2.201409121644'
junitVersion = '4.11'
servletApiVersion = '3.1.0'
springHateoasVersion = '0.17.0.RELEASE'
springVersion = '4.1.4.RELEASE'
mockitoVersion = '1.10.19'
}
@@ -64,6 +65,7 @@ project(':spring-restdocs') {
compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
jacoco "org.jacoco:org.jacoco.agent:$jacocoVersion:runtime"
testCompile "org.springframework:spring-webmvc:$springVersion"
testCompile "org.springframework.hateoas:spring-hateoas:$springHateoasVersion"
testCompile "org.mockito:mockito-core:$mockitoVersion"
}

View File

@@ -106,9 +106,8 @@ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter {
currentContext.getAndIncrementStepCount();
}
request.setScheme(RestDocumentationConfigurer.this.scheme);
request.setRemotePort(RestDocumentationConfigurer.this.port);
request.setServerPort(RestDocumentationConfigurer.this.port);
request.setRemoteHost(RestDocumentationConfigurer.this.host);
request.setServerName(RestDocumentationConfigurer.this.host);
return request;
}
};

View File

@@ -134,10 +134,10 @@ public abstract class CurlDocumentation {
public void perform() throws IOException {
MockHttpServletRequest request = this.result.getRequest();
this.writer.print(String.format("curl %s://%s", request.getScheme(),
request.getRemoteHost()));
request.getServerName()));
if (isNonStandardPort(request)) {
this.writer.print(String.format(":%d", request.getRemotePort()));
this.writer.print(String.format(":%d", request.getServerPort()));
}
this.writer.print(getRequestUriWithQueryString(request));
@@ -180,9 +180,9 @@ public abstract class CurlDocumentation {
}
private boolean isNonStandardPort(HttpServletRequest request) {
return (SCHEME_HTTP.equals(request.getScheme()) && request.getRemotePort() != STANDARD_PORT_HTTP)
return (SCHEME_HTTP.equals(request.getScheme()) && request.getServerPort() != STANDARD_PORT_HTTP)
|| (SCHEME_HTTPS.equals(request.getScheme()) && request
.getRemotePort() != STANDARD_PORT_HTTPS);
.getServerPort() != STANDARD_PORT_HTTPS);
}
private String getRequestUriWithQueryString(HttpServletRequest request) {

View File

@@ -18,10 +18,14 @@ package org.springframework.restdocs.config;
import static org.junit.Assert.assertEquals;
import java.net.URI;
import org.junit.Test;
import org.springframework.hateoas.mvc.BasicLinkBuilder;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.restdocs.config.RestDocumentationConfigurer;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* Tests for {@link RestDocumentationConfigurer}.
@@ -70,9 +74,19 @@ public class RestDocumentationConfigurerTests {
private void assertUriConfiguration(String scheme, String host, int port) {
assertEquals(scheme, this.request.getScheme());
assertEquals(host, this.request.getRemoteHost());
assertEquals(port, this.request.getRemotePort());
assertEquals(host, this.request.getServerName());
assertEquals(port, this.request.getServerPort());
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(
this.request));
try {
URI uri = BasicLinkBuilder.linkToCurrentMapping().toUri();
assertEquals(scheme, uri.getScheme());
assertEquals(host, uri.getHost());
assertEquals(port, uri.getPort());
}
finally {
RequestContextHolder.resetRequestAttributes();
}
}
}

View File

@@ -256,7 +256,7 @@ public class CurlDocumentationTests {
@Test
public void httpWithNonStandardPort() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.setRemotePort(8080);
request.setServerPort(8080);
documentCurlRequest("http-with-non-standard-port").handle(
new StubMvcResult(request, null));
assertThat(requestSnippetLines("http-with-non-standard-port"),
@@ -266,7 +266,7 @@ public class CurlDocumentationTests {
@Test
public void httpsWithStandardPort() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.setRemotePort(443);
request.setServerPort(443);
request.setScheme("https");
documentCurlRequest("https-with-standard-port").handle(
new StubMvcResult(request, null));
@@ -277,7 +277,7 @@ public class CurlDocumentationTests {
@Test
public void httpsWithNonStandardPort() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.setRemotePort(8443);
request.setServerPort(8443);
request.setScheme("https");
documentCurlRequest("https-with-non-standard-port").handle(
new StubMvcResult(request, null));
@@ -285,6 +285,16 @@ public class CurlDocumentationTests {
hasItem("$ curl https://localhost:8443/foo -i"));
}
@Test
public void requestWithCustomHost() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.setServerName("api.example.com");
documentCurlRequest("request-with-custom-host").handle(
new StubMvcResult(request, null));
assertThat(requestSnippetLines("request-with-custom-host"),
hasItem("$ curl http://api.example.com/foo -i"));
}
private List<String> requestSnippetLines(String snippetName) throws IOException {
return snippetLines(snippetName, "request");
}