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

@@ -175,6 +175,22 @@ public class MockMvcRestDocumentationIntegrationTests {
+ "-H 'Accept: application/json' -d 'a=alpha'"))));
}
@Test
public void curlSnippetWithContentAndParametersOnPost() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(post("/").param("a", "alpha").accept(MediaType.APPLICATION_JSON)
.content("some content")).andExpect(status().isOk())
.andDo(document("curl-snippet-with-content-and-parameters"));
assertThat(
new File(
"build/generated-snippets/curl-snippet-with-content-and-parameters/curl-request.adoc"),
is(snippet(asciidoctor())
.withContents(codeBlock(asciidoctor(), "bash").content("$ curl "
+ "'http://localhost:8080/?a=alpha' -i -X POST "
+ "-H 'Accept: application/json' -d 'some content'"))));
}
@Test
public void httpieSnippetWithContent() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
@@ -207,6 +223,22 @@ public class MockMvcRestDocumentationIntegrationTests {
+ "'Accept:application/json' 'a=alpha'"))));
}
@Test
public void httpieSnippetWithContentAndParametersOnPost() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(post("/").param("a", "alpha").content("some content")
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(document("httpie-snippet-post-with-content-and-parameters"));
assertThat(
new File(
"build/generated-snippets/httpie-snippet-post-with-content-and-parameters/httpie-request.adoc"),
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
.content("$ echo " + "'some content' | http POST "
+ "'http://localhost:8080/?a=alpha' "
+ "'Accept:application/json'"))));
}
@Test
public void linksSnippet() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)