Include request URI’s port, if any, in default Host header
Previously, OperationRequestFactory would add a Host header if one did not already exist in the request’s headers, however it did not include the port. This meant that when the request was being made to a non-standard port (a port other than 80 for an HTTP request and 443 for an HTTPS request) the Host header was incorrect. This commit updates OperationRequestFactory to check the URI for a port and, if it has one, include it in the Host header. UriModifyingOperationPreprocessor has also been updated to correctly include the port in the Host header. Closes gh-269
This commit is contained in:
@@ -142,7 +142,7 @@ public class RestAssuredRequestConverterTests {
|
||||
assertThat(request.getHeaders().get("Foo"), is(equalTo(Arrays.asList("bar"))));
|
||||
assertThat(request.getHeaders().get("Accept"), is(equalTo(Arrays.asList("*/*"))));
|
||||
assertThat(request.getHeaders().get("Host"),
|
||||
is(equalTo(Arrays.asList("localhost"))));
|
||||
is(equalTo(Arrays.asList("localhost:" + this.port))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -268,9 +268,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
.filter(document("original-request"))
|
||||
.filter(document("preprocessed-request",
|
||||
preprocessRequest(prettyPrint(),
|
||||
removeHeaders("a", HttpHeaders.HOST,
|
||||
HttpHeaders.CONTENT_LENGTH),
|
||||
replacePattern(pattern, "\"<<beta>>\""))))
|
||||
replacePattern(pattern, "\"<<beta>>\""),
|
||||
modifyUris().removePort(),
|
||||
removeHeaders("a", HttpHeaders.CONTENT_LENGTH))))
|
||||
.get("/").then().statusCode(200);
|
||||
assertThat(
|
||||
new File("build/generated-snippets/original-request/http-request.adoc"),
|
||||
@@ -279,7 +279,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
.header("a", "alpha").header("b", "bravo")
|
||||
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
|
||||
.header("Content-Type", "application/json; charset=UTF-8")
|
||||
.header("Host", "localhost")
|
||||
.header("Host", "localhost:" + this.port)
|
||||
.header("Content-Length", "13")
|
||||
.content("{\"a\":\"alpha\"}"))));
|
||||
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
|
||||
@@ -291,7 +291,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
.header("b", "bravo")
|
||||
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
|
||||
.header("Content-Type", "application/json; charset=UTF-8")
|
||||
.content(prettyPrinted))));
|
||||
.header("Host", "localhost").content(prettyPrinted))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -63,9 +63,11 @@ public class UriModifyingOperationPreprocessorTests {
|
||||
public void requestUriHostCanBeModified() {
|
||||
this.preprocessor.host("api.example.com");
|
||||
OperationRequest processed = this.preprocessor
|
||||
.preprocess(createRequestWithUri("http://api.example.com:12345"));
|
||||
.preprocess(createRequestWithUri("http://api.foo.com:12345"));
|
||||
assertThat(processed.getUri(),
|
||||
is(equalTo(URI.create("http://api.example.com:12345"))));
|
||||
assertThat(processed.getHeaders().getFirst(HttpHeaders.HOST),
|
||||
is(equalTo("api.example.com:12345")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -75,6 +77,8 @@ public class UriModifyingOperationPreprocessorTests {
|
||||
.preprocess(createRequestWithUri("http://api.example.com:12345"));
|
||||
assertThat(processed.getUri(),
|
||||
is(equalTo(URI.create("http://api.example.com:23456"))));
|
||||
assertThat(processed.getHeaders().getFirst(HttpHeaders.HOST),
|
||||
is(equalTo("api.example.com:23456")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -83,6 +87,8 @@ public class UriModifyingOperationPreprocessorTests {
|
||||
OperationRequest processed = this.preprocessor
|
||||
.preprocess(createRequestWithUri("http://api.example.com:12345"));
|
||||
assertThat(processed.getUri(), is(equalTo(URI.create("http://api.example.com"))));
|
||||
assertThat(processed.getHeaders().getFirst(HttpHeaders.HOST),
|
||||
is(equalTo("api.example.com")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user