Include customized Host header in curl and HTTPie request snippets

Previously, the curl and HTTPie request snippets always excluded the
Host header, relying on the Host header that’s auto-generated by curl
and HTTPie instead. This worked well for the most part, but meant that
incorrect snippets were generated when the request was being sent with
a custom host header that did not match the header that would be
auto-generated.

This commit updates CliOperationRequest to only filter out the Host
header if it’s the same as the header that would be auto-generated
by curl or HTTPie.

Closes gh-258
This commit is contained in:
Andy Wilkinson
2016-06-23 11:41:57 +01:00
parent a4298b29e7
commit 37519398c7
3 changed files with 66 additions and 11 deletions

View File

@@ -330,4 +330,18 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
.request("http://localhost/foo").build());
}
@Test
public void customHostHeaderIsIncluded() throws IOException {
this.snippet.expectCurlRequest("custom-host-header")
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i" + " -H 'Host: api.example.com'"
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
new CurlRequestSnippet().document(
operationBuilder("custom-host-header").request("http://localhost/foo")
.header(HttpHeaders.HOST, "api.example.com")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.header("a", "alpha").build());
}
}

View File

@@ -332,4 +332,18 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
.request("http://localhost/foo").build());
}
@Test
public void customHostHeaderIsIncluded() throws IOException {
this.snippet.expectHttpieRequest("custom-host-header")
.withContents(codeBlock("bash").content(
"$ http GET 'http://localhost/foo' 'Host:api.example.com'"
+ " 'Content-Type:application/json' 'a:alpha'"));
new HttpieRequestSnippet().document(
operationBuilder("custom-host-header").request("http://localhost/foo")
.header(HttpHeaders.HOST, "api.example.com")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.header("a", "alpha").build());
}
}