Skip to content

Commit

Permalink
Fix handling of empty querty string parameters
Browse files Browse the repository at this point in the history
Previously empty parameters in a query string were handled
inconsistently such that they sometimes had a value of "" in a list
and sometimes were represented as an empty list. This inconsistency
lead to the parameter appearing twice in the cURL request snippet.

This commit removes the inconsistency by always using an empty string
to represent an empty query parameter value.

Fixes gh-647
  • Loading branch information
wilkinsona committed Oct 4, 2019
1 parent 92926d6 commit 97ddf3d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
Expand Up @@ -64,10 +64,8 @@ private void processParameter(String parameter, Parameters parameters) {
parameters.add(decode(name), decode(value));
}
else {
List<String> values = parameters.get(components[0]);
if (values == null) {
parameters.put(components[0], new LinkedList<String>());
}
List<String> values = parameters.computeIfAbsent(components[0], (p) -> new LinkedList<String>());
values.add("");
}
}
else {
Expand Down
Expand Up @@ -81,4 +81,18 @@ public void malformedParameter() {
this.queryStringParser.parse(URI.create("http://localhost?a=apple=avocado"));
}

@Test
public void emptyParameter() {
Parameters parameters = this.queryStringParser.parse(URI.create("http://localhost?a="));
assertThat(parameters.size()).isEqualTo(1);
assertThat(parameters).containsEntry("a", Arrays.asList(""));
}

@Test
public void emptyAndNotEmptyParameter() {
Parameters parameters = this.queryStringParser.parse(URI.create("http://localhost?a=&a=alpha"));
assertThat(parameters.size()).isEqualTo(1);
assertThat(parameters).containsEntry("a", Arrays.asList("", "alpha"));
}

}
Expand Up @@ -185,6 +185,19 @@ public void curlSnippetWithQueryStringOnPost() throws Exception {
+ " -H 'Accept: application/json' \\%n" + " -d 'a=alpha'"))));
}

@Test
public void curlSnippetWithEmptyParameterQueryString() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(get("/").param("a", "").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(document("curl-snippet-with-empty-parameter-query-string"));
assertThat(
new File("build/generated-snippets/curl-snippet-with-empty-parameter-query-string/curl-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ curl 'http://localhost:8080/?a=' -i -X GET \\%n"
+ " -H 'Accept: application/json'"))));
}

@Test
public void curlSnippetWithContentAndParametersOnPost() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
Expand Down
Expand Up @@ -120,6 +120,18 @@ public void curlSnippetWithCookies() throws Exception {
+ " -H 'Content-Type: " + contentType + "' \\%n" + " --cookie 'cookieName=cookieVal'"))));
}

@Test
public void curlSnippetWithEmptyParameterQueryString() throws Exception {
given().port(tomcat.getPort()).filter(documentationConfiguration(this.restDocumentation))
.filter(document("curl-snippet-with-empty-parameter-query-string")).accept("application/json")
.param("a", "").get("/").then().statusCode(200);
assertThat(
new File("build/generated-snippets/curl-snippet-with-empty-parameter-query-string/curl-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ curl 'http://localhost:" + tomcat.getPort()
+ "/?a=' -i -X GET \\%n -H 'Accept: application/json'"))));
}

@Test
public void curlSnippetWithQueryStringOnPost() throws Exception {
given().port(tomcat.getPort()).filter(documentationConfiguration(this.restDocumentation))
Expand Down
Expand Up @@ -165,6 +165,17 @@ public void curlSnippetWithCookies() throws Exception {
+ " -H 'Accept: application/json' \\%n" + " --cookie 'cookieName=cookieVal'"))));
}

@Test
public void curlSnippetWithEmptyParameterQueryString() throws Exception {
this.webTestClient.get().uri("/?a=").accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk()
.expectBody().consumeWith(document("curl-snippet-with-empty-parameter-query-string"));
assertThat(
new File("build/generated-snippets/curl-snippet-with-empty-parameter-query-string/curl-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ curl 'https://api.example.com/?a=' -i -X GET \\%n"
+ " -H 'Accept: application/json'"))));
}

@Test
public void httpieSnippetWithCookies() throws Exception {
this.webTestClient.get().uri("/").cookie("cookieName", "cookieVal").accept(MediaType.APPLICATION_JSON)
Expand Down

0 comments on commit 97ddf3d

Please sign in to comment.