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:
committed by
Andy Wilkinson
parent
26adbb10ae
commit
d329e2da5d
@@ -27,6 +27,7 @@ dependencies {
|
|||||||
compile 'junit:junit'
|
compile 'junit:junit'
|
||||||
compile 'org.springframework:spring-webmvc'
|
compile 'org.springframework:spring-webmvc'
|
||||||
compile 'javax.servlet:javax.servlet-api'
|
compile 'javax.servlet:javax.servlet-api'
|
||||||
|
compile 'commons-codec:commons-codec:1.10'
|
||||||
compile files(jmustacheRepackJar)
|
compile files(jmustacheRepackJar)
|
||||||
jarjar 'com.googlecode.jarjar:jarjar:1.3'
|
jarjar 'com.googlecode.jarjar:jarjar:1.3'
|
||||||
jmustache 'com.samskivert:jmustache@jar'
|
jmustache 'com.samskivert:jmustache@jar'
|
||||||
|
|||||||
@@ -29,12 +29,14 @@ import org.springframework.restdocs.operation.OperationRequest;
|
|||||||
import org.springframework.restdocs.operation.OperationRequestPart;
|
import org.springframework.restdocs.operation.OperationRequestPart;
|
||||||
import org.springframework.restdocs.snippet.Snippet;
|
import org.springframework.restdocs.snippet.Snippet;
|
||||||
import org.springframework.restdocs.snippet.TemplatedSnippet;
|
import org.springframework.restdocs.snippet.TemplatedSnippet;
|
||||||
|
import org.springframework.util.Base64Utils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link Snippet} that documents the curl command for a request.
|
* A {@link Snippet} that documents the curl command for a request.
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Paul-Christian Volkmer
|
||||||
* @see CurlDocumentation#curlRequest()
|
* @see CurlDocumentation#curlRequest()
|
||||||
* @see CurlDocumentation#curlRequest(Map)
|
* @see CurlDocumentation#curlRequest(Map)
|
||||||
*/
|
*/
|
||||||
@@ -73,6 +75,7 @@ public class CurlRequestSnippet extends TemplatedSnippet {
|
|||||||
StringWriter command = new StringWriter();
|
StringWriter command = new StringWriter();
|
||||||
PrintWriter printer = new PrintWriter(command);
|
PrintWriter printer = new PrintWriter(command);
|
||||||
writeOptionToIncludeHeadersInOutput(printer);
|
writeOptionToIncludeHeadersInOutput(printer);
|
||||||
|
writeHttpBasicAuthorization(operation.getRequest(), printer);
|
||||||
writeHttpMethodIfNecessary(operation.getRequest(), printer);
|
writeHttpMethodIfNecessary(operation.getRequest(), printer);
|
||||||
writeHeaders(operation.getRequest(), printer);
|
writeHeaders(operation.getRequest(), printer);
|
||||||
writePartsIfNecessary(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) {
|
private void writeHeaders(OperationRequest request, PrintWriter writer) {
|
||||||
for (Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
|
for (Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
|
||||||
for (String header : entry.getValue()) {
|
for (String header : entry.getValue()) {
|
||||||
|
if (isAuthBasicHeader(entry.getKey(), header)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
writer.print(String.format(" -H '%s: %s'", entry.getKey(), header));
|
writer.print(String.format(" -H '%s: %s'", entry.getKey(), header));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,4 +160,8 @@ public class CurlRequestSnippet extends TemplatedSnippet {
|
|||||||
|| HttpMethod.POST.equals(request.getMethod());
|
|| HttpMethod.POST.equals(request.getMethod());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isAuthBasicHeader(String key, String value) {
|
||||||
|
return ("Authorization".equals(key) && value.startsWith("Basic"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -36,6 +36,7 @@ import org.springframework.restdocs.templates.TemplateResourceResolver;
|
|||||||
import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
|
import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
|
||||||
import org.springframework.restdocs.test.ExpectedSnippet;
|
import org.springframework.restdocs.test.ExpectedSnippet;
|
||||||
import org.springframework.restdocs.test.OperationBuilder;
|
import org.springframework.restdocs.test.OperationBuilder;
|
||||||
|
import org.springframework.util.Base64Utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link CurlRequestSnippet}
|
* Tests for {@link CurlRequestSnippet}
|
||||||
@@ -44,6 +45,7 @@ import org.springframework.restdocs.test.OperationBuilder;
|
|||||||
* @author Yann Le Guern
|
* @author Yann Le Guern
|
||||||
* @author Dmitriy Mayboroda
|
* @author Dmitriy Mayboroda
|
||||||
* @author Jonathan Pearlin
|
* @author Jonathan Pearlin
|
||||||
|
* @author Paul-Christian Volkmer
|
||||||
*/
|
*/
|
||||||
public class CurlRequestSnippetTests {
|
public class CurlRequestSnippetTests {
|
||||||
|
|
||||||
@@ -257,4 +259,14 @@ public class CurlRequestSnippetTests {
|
|||||||
.request("http://localhost/foo").build());
|
.request("http://localhost/foo").build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void httpBasicAuthorizationHeader() throws IOException {
|
||||||
|
this.snippet.expectCurlRequest("get-request")
|
||||||
|
.withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i -u 'user:secret'"));
|
||||||
|
new CurlRequestSnippet().document(new OperationBuilder("get-request", this.snippet.getOutputDirectory())
|
||||||
|
.request("http://localhost/foo")
|
||||||
|
.header(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString("user:secret".getBytes()))
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user