Improve handling of empty parameters in curl and HTTP request snippets

Previously, both the curl and HTTP request snippets would ignore
a parameter with no value, for example from the query string of the
url http://localhost:8080/foo?bar.

This commit updates both snippets so that such parameters are
included in the generated snippet, including a multi-part request
that is uploading form data and a field in the form has no value.
Additions have been made to the tests for both snippets.

While the request parameters snippet correctly handled parameters with
no value, there was no test verifying that this was the case. One
has been added in this commit.

Closes gh-200
This commit is contained in:
Andy Wilkinson
2016-02-15 15:41:09 +00:00
parent 4d44401efa
commit 40201e2e99
7 changed files with 154 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@ package org.springframework.restdocs.curl;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import org.springframework.restdocs.operation.Parameters;
@@ -58,10 +60,18 @@ public class QueryStringParser {
private void processParameter(String parameter, Parameters parameters) {
String[] components = parameter.split("=");
if (components.length == 2) {
String name = components[0];
String value = components[1];
parameters.add(decode(name), decode(value));
if (components.length > 0 && components.length < 3) {
if (components.length == 2) {
String name = components[0];
String value = components[1];
parameters.add(decode(name), decode(value));
}
else {
List<String> values = parameters.get(components[0]);
if (values == null) {
parameters.put(components[0], new LinkedList<String>());
}
}
}
else {
throw new IllegalArgumentException(

View File

@@ -127,10 +127,16 @@ public class HttpRequestSnippet extends TemplatedSnippet {
private void writeParts(OperationRequest request, PrintWriter writer) {
writer.println();
for (Entry<String, List<String>> parameter : request.getParameters().entrySet()) {
for (String value : parameter.getValue()) {
if (parameter.getValue().isEmpty()) {
writePartBoundary(writer);
writePart(parameter.getKey(), value, null, writer);
writer.println();
writePart(parameter.getKey(), "", null, writer);
}
else {
for (String value : parameter.getValue()) {
writePartBoundary(writer);
writePart(parameter.getKey(), value, null, writer);
writer.println();
}
}
}
for (OperationRequestPart part : request.getParts()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,17 +40,33 @@ public class Parameters extends LinkedMultiValueMap<String, String> {
public String toQueryString() {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, List<String>> entry : entrySet()) {
for (String value : entry.getValue()) {
if (sb.length() > 0) {
sb.append("&");
if (entry.getValue().isEmpty()) {
append(sb, entry.getKey());
}
else {
for (String value : entry.getValue()) {
append(sb, entry.getKey(), value);
}
sb.append(urlEncodeUTF8(entry.getKey())).append('=')
.append(urlEncodeUTF8(value));
}
}
return sb.toString();
}
private static void append(StringBuilder sb, String key) {
append(sb, key, "");
}
private static void append(StringBuilder sb, String key, String value) {
doAppend(sb, urlEncodeUTF8(key) + "=" + urlEncodeUTF8(value));
}
private static void doAppend(StringBuilder sb, String toAppend) {
if (sb.length() > 0) {
sb.append("&");
}
sb.append(toAppend);
}
private static String urlEncodeUTF8(String s) {
try {
return URLEncoder.encode(s, "UTF-8");