Improve handling of requests with both parameters and content

Previously, if a MockMvc request was made with parameters and body
content, the parameters were omitted from the resulting curl, HTTPie
and HTTP request snippets.

This commit updates the affected snippets to ensure that the
parameters are included. The user-provided content is interpreted
as indicating that the parameters should be sent in the query string
rather than as form-encoded content.

Closes gh-239
This commit is contained in:
Andy Wilkinson
2016-06-23 17:53:32 +01:00
parent 37519398c7
commit 8ca1dfa1ad
7 changed files with 144 additions and 7 deletions

View File

@@ -61,6 +61,14 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
operationBuilder("get-request").request("http://localhost/foo").build());
}
@Test
public void getRequestWithParameter() throws IOException {
this.snippet.expectCurlRequest("get-request").withContents(
codeBlock("bash").content("$ curl 'http://localhost/foo?a=alpha' -i"));
new CurlRequestSnippet().document(operationBuilder("get-request")
.request("http://localhost/foo").param("a", "alpha").build());
}
@Test
public void nonGetRequest() throws IOException {
this.snippet.expectCurlRequest("non-get-request").withContents(
@@ -344,4 +352,17 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
.header("a", "alpha").build());
}
@Test
public void postWithContentAndParameters() throws IOException {
this.snippet.expectCurlRequest("post-with-content-and-parameters")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i "
+ "-X POST -d 'Some content'"));
new CurlRequestSnippet()
.document(operationBuilder("post-with-content-and-parameters")
.request("http://localhost/foo").param("a", "alpha")
.method("POST").param("b", "bravo").content("Some content")
.build());
}
}

View File

@@ -62,6 +62,14 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
operationBuilder("get-request").request("http://localhost/foo").build());
}
@Test
public void getRequestWithParameter() throws IOException {
this.snippet.expectHttpieRequest("get-request-with-parameter").withContents(
codeBlock("bash").content("$ http GET 'http://localhost/foo?a=alpha'"));
new HttpieRequestSnippet().document(operationBuilder("get-request-with-parameter")
.request("http://localhost/foo").param("a", "alpha").build());
}
@Test
public void nonGetRequest() throws IOException {
this.snippet.expectHttpieRequest("non-get-request").withContents(
@@ -346,4 +354,16 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
.header("a", "alpha").build());
}
@Test
public void postWithContentAndParameters() throws IOException {
this.snippet.expectHttpieRequest("post-with-content-and-parameters").withContents(
codeBlock("bash").content("$ echo 'Some content' | http POST "
+ "'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet()
.document(operationBuilder("post-with-content-and-parameters")
.request("http://localhost/foo").method("POST")
.param("a", "alpha").param("b", "bravo").content("Some content")
.build());
}
}

View File

@@ -59,6 +59,17 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
.request("http://localhost/foo").header("Alpha", "a").build());
}
@Test
public void getRequestWithParameters() throws IOException {
this.snippet.expectHttpRequest("get-request-with-parameters")
.withContents(httpRequest(RequestMethod.GET, "/foo?b=bravo")
.header("Alpha", "a").header(HttpHeaders.HOST, "localhost"));
new HttpRequestSnippet().document(operationBuilder("get-request-with-parameters")
.request("http://localhost/foo").header("Alpha", "a").param("b", "bravo")
.build());
}
@Test
public void getRequestWithPort() throws IOException {
this.snippet.expectHttpRequest("get-request")
@@ -103,6 +114,20 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
.request("http://localhost/foo").method("POST").content(content).build());
}
@Test
public void postRequestWithContentAndParameters() throws IOException {
String content = "Hello, world";
this.snippet.expectHttpRequest("post-request-with-content-and-parameters")
.withContents(httpRequest(RequestMethod.POST, "/foo?a=alpha")
.header(HttpHeaders.HOST, "localhost").content(content).header(
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
new HttpRequestSnippet()
.document(operationBuilder("post-request-with-content-and-parameters")
.request("http://localhost/foo").method("POST")
.param("a", "alpha").content(content).build());
}
@Test
public void postRequestWithCharset() throws IOException {
String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";