Omit port in curl request snippet if it’s the default for the scheme
Previously an HTTP request to localhost on port 80 or an HTTPS request to localhost on 443 would generate curl request snippets with the uris http://localhost:80 and https://localhost:443 respectively. While specifying the port does no harm, it is unnecessary. This commit updates CurlDocumentation so that the uri in the request snippet does not include the port when the port is the default for the uri’s scheme. Closes gh-17
This commit is contained in:
@@ -21,6 +21,8 @@ import static org.springframework.restdocs.util.IterableEnumeration.iterable;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.restdocs.snippet.DocumentationWriter;
|
||||
@@ -102,6 +104,14 @@ public abstract class CurlDocumentation {
|
||||
private static final class CurlRequestDocumentationAction implements
|
||||
DocumentationAction {
|
||||
|
||||
private static final String SCHEME_HTTP = "http";
|
||||
|
||||
private static final String SCHEME_HTTPS = "https";
|
||||
|
||||
private static final int STANDARD_PORT_HTTP = 80;
|
||||
|
||||
private static final int STANDARD_PORT_HTTPS = 443;
|
||||
|
||||
private final DocumentationWriter writer;
|
||||
|
||||
private final MvcResult result;
|
||||
@@ -118,9 +128,14 @@ public abstract class CurlDocumentation {
|
||||
@Override
|
||||
public void perform() throws IOException {
|
||||
MockHttpServletRequest request = this.result.getRequest();
|
||||
this.writer.print(String.format("curl %s://%s:%d%s", request.getScheme(),
|
||||
request.getRemoteHost(), request.getRemotePort(),
|
||||
getRequestUriWithQueryString(request)));
|
||||
this.writer.print(String.format("curl %s://%s", request.getScheme(),
|
||||
request.getRemoteHost()));
|
||||
|
||||
if (isNonStandardPort(request)) {
|
||||
this.writer.print(String.format(":%d", request.getRemotePort()));
|
||||
}
|
||||
|
||||
this.writer.print(getRequestUriWithQueryString(request));
|
||||
|
||||
if (this.curlConfiguration.isIncludeResponseHeaders()) {
|
||||
this.writer.print(" -i");
|
||||
@@ -145,7 +160,13 @@ public abstract class CurlDocumentation {
|
||||
this.writer.println();
|
||||
}
|
||||
|
||||
private String getRequestUriWithQueryString(MockHttpServletRequest request) {
|
||||
private boolean isNonStandardPort(HttpServletRequest request) {
|
||||
return (SCHEME_HTTP.equals(request.getScheme()) && request.getRemotePort() != STANDARD_PORT_HTTP)
|
||||
|| (SCHEME_HTTPS.equals(request.getScheme()) && request
|
||||
.getRemotePort() != STANDARD_PORT_HTTPS);
|
||||
}
|
||||
|
||||
private String getRequestUriWithQueryString(HttpServletRequest request) {
|
||||
return request.getQueryString() != null ? request.getRequestURI() + "?"
|
||||
+ request.getQueryString() : request.getRequestURI();
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class CurlDocumentationTests {
|
||||
documentCurlRequest("get-request").handle(
|
||||
new StubMvcResult(new MockHttpServletRequest("GET", "/foo"), null));
|
||||
assertThat(requestSnippetLines("get-request"),
|
||||
hasItem("$ curl http://localhost:80/foo -i"));
|
||||
hasItem("$ curl http://localhost/foo -i"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -73,7 +73,7 @@ public class CurlDocumentationTests {
|
||||
documentCurlRequest("non-get-request").handle(
|
||||
new StubMvcResult(new MockHttpServletRequest("POST", "/foo"), null));
|
||||
assertThat(requestSnippetLines("non-get-request"),
|
||||
hasItem("$ curl http://localhost:80/foo -i -X POST"));
|
||||
hasItem("$ curl http://localhost/foo -i -X POST"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -82,7 +82,7 @@ public class CurlDocumentationTests {
|
||||
.includeResponseHeaders(false)
|
||||
.handle(new StubMvcResult(new MockHttpServletRequest("GET", "/foo"), null));
|
||||
assertThat(requestSnippetLines("request-without-response-header-inclusion"),
|
||||
hasItem("$ curl http://localhost:80/foo"));
|
||||
hasItem("$ curl http://localhost/foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -92,7 +92,7 @@ public class CurlDocumentationTests {
|
||||
documentCurlRequest("request-with-content").handle(
|
||||
new StubMvcResult(request, null));
|
||||
assertThat(requestSnippetLines("request-with-content"),
|
||||
hasItem("$ curl http://localhost:80/foo -i -d 'content'"));
|
||||
hasItem("$ curl http://localhost/foo -i -d 'content'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -101,7 +101,7 @@ public class CurlDocumentationTests {
|
||||
new StubMvcResult(new MockHttpServletRequest("GET", "/foo?param=value"),
|
||||
null));
|
||||
assertThat(requestSnippetLines("request-with-uri-query-string"),
|
||||
hasItem("$ curl http://localhost:80/foo?param=value -i"));
|
||||
hasItem("$ curl http://localhost/foo?param=value -i"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -111,7 +111,7 @@ public class CurlDocumentationTests {
|
||||
documentCurlRequest("request-with-query-string").handle(
|
||||
new StubMvcResult(request, null));
|
||||
assertThat(requestSnippetLines("request-with-query-string"),
|
||||
hasItem("$ curl http://localhost:80/foo?param=value -i"));
|
||||
hasItem("$ curl http://localhost/foo?param=value -i"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -123,7 +123,7 @@ public class CurlDocumentationTests {
|
||||
new StubMvcResult(request, null));
|
||||
assertThat(
|
||||
requestSnippetLines("request-with-headers"),
|
||||
hasItem("$ curl http://localhost:80/foo -i -H \"Content-Type: application/json\" -H \"a: alpha\""));
|
||||
hasItem("$ curl http://localhost/foo -i -H \"Content-Type: application/json\" -H \"a: alpha\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -184,7 +184,39 @@ public class CurlDocumentationTests {
|
||||
new StubMvcResult(new MockHttpServletRequest("GET", "/foo"),
|
||||
new MockHttpServletResponse()));
|
||||
assertThat(requestResponseSnippetLines("request-and-response"),
|
||||
hasItems("$ curl http://localhost:80/foo -i", "HTTP/1.1 200 OK"));
|
||||
hasItems("$ curl http://localhost/foo -i", "HTTP/1.1 200 OK"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpWithNonStandardPort() throws IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setRemotePort(8080);
|
||||
documentCurlRequest("http-with-non-standard-port").handle(
|
||||
new StubMvcResult(request, null));
|
||||
assertThat(requestSnippetLines("http-with-non-standard-port"),
|
||||
hasItem("$ curl http://localhost:8080/foo -i"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpsWithStandardPort() throws IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setRemotePort(443);
|
||||
request.setScheme("https");
|
||||
documentCurlRequest("https-with-standard-port").handle(
|
||||
new StubMvcResult(request, null));
|
||||
assertThat(requestSnippetLines("https-with-standard-port"),
|
||||
hasItem("$ curl https://localhost/foo -i"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpsWithNonStandardPort() throws IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setRemotePort(8443);
|
||||
request.setScheme("https");
|
||||
documentCurlRequest("https-with-non-standard-port").handle(
|
||||
new StubMvcResult(request, null));
|
||||
assertThat(requestSnippetLines("https-with-non-standard-port"),
|
||||
hasItem("$ curl https://localhost:8443/foo -i"));
|
||||
}
|
||||
|
||||
private List<String> requestSnippetLines(String snippetName) throws IOException {
|
||||
|
||||
Reference in New Issue
Block a user