Use -u 'username:password' for basic auth in the curl request snippet

Previously, when a request was made that used basic auth, the curl
snippet would configure the authentication header with the
Base64-encoded header. This commit updates the snippet to use the
more human-friendly -u option to provide the username and password
in place of the authentication header.

Closes gh-122
This commit is contained in:
Paul-Christian Volkmer
2015-09-10 00:30:11 +02:00
committed by Andy Wilkinson
parent 26adbb10ae
commit d329e2da5d
3 changed files with 36 additions and 1 deletions

View File

@@ -29,12 +29,14 @@ import org.springframework.restdocs.operation.OperationRequest;
import org.springframework.restdocs.operation.OperationRequestPart;
import org.springframework.restdocs.snippet.Snippet;
import org.springframework.restdocs.snippet.TemplatedSnippet;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
/**
* A {@link Snippet} that documents the curl command for a request.
*
* @author Andy Wilkinson
* @author Paul-Christian Volkmer
* @see CurlDocumentation#curlRequest()
* @see CurlDocumentation#curlRequest(Map)
*/
@@ -73,6 +75,7 @@ public class CurlRequestSnippet extends TemplatedSnippet {
StringWriter command = new StringWriter();
PrintWriter printer = new PrintWriter(command);
writeOptionToIncludeHeadersInOutput(printer);
writeHttpBasicAuthorization(operation.getRequest(), printer);
writeHttpMethodIfNecessary(operation.getRequest(), printer);
writeHeaders(operation.getRequest(), printer);
writePartsIfNecessary(operation.getRequest(), printer);
@@ -92,9 +95,24 @@ public class CurlRequestSnippet extends TemplatedSnippet {
}
}
private void writeHttpBasicAuthorization(OperationRequest request, PrintWriter writer) {
for (Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
for (String header : entry.getValue()) {
if (isAuthBasicHeader(entry.getKey(), header)) {
String auth = new String(Base64Utils.decodeFromString(header.replace("Basic", "").trim()));
writer.print(String.format(" -u '%s'", auth));
break;
}
}
}
}
private void writeHeaders(OperationRequest request, PrintWriter writer) {
for (Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
for (String header : entry.getValue()) {
if (isAuthBasicHeader(entry.getKey(), header)) {
continue;
}
writer.print(String.format(" -H '%s: %s'", entry.getKey(), header));
}
}
@@ -142,4 +160,8 @@ public class CurlRequestSnippet extends TemplatedSnippet {
|| HttpMethod.POST.equals(request.getMethod());
}
}
private boolean isAuthBasicHeader(String key, String value) {
return ("Authorization".equals(key) && value.startsWith("Basic"));
}
}