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

@@ -36,6 +36,7 @@ import org.springframework.restdocs.templates.TemplateResourceResolver;
import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import org.springframework.util.Base64Utils;
/**
* Tests for {@link CurlRequestSnippet}
@@ -44,6 +45,7 @@ import org.springframework.restdocs.test.OperationBuilder;
* @author Yann Le Guern
* @author Dmitriy Mayboroda
* @author Jonathan Pearlin
* @author Paul-Christian Volkmer
*/
public class CurlRequestSnippetTests {
@@ -257,4 +259,14 @@ public class CurlRequestSnippetTests {
.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());
}
}