Polish contribution
- Make commons-codec an optional dependency - Isolate knowledge of basic auth header to code that produces the -u option Closes gh-120
This commit is contained in:
@@ -59,6 +59,7 @@ subprojects {
|
||||
dependencies {
|
||||
dependency 'com.fasterxml.jackson.core:jackson-databind:2.4.6'
|
||||
dependency 'com.samskivert:jmustache:1.10'
|
||||
dependency 'commons-codec:commons-codec:1.10'
|
||||
dependency 'javax.servlet:javax.servlet-api:3.1.0'
|
||||
dependency 'javax.validation:validation-api:1.1.0.Final'
|
||||
dependency 'junit:junit:4.12'
|
||||
|
||||
@@ -27,11 +27,11 @@ dependencies {
|
||||
compile 'junit:junit'
|
||||
compile 'org.springframework:spring-webmvc'
|
||||
compile 'javax.servlet:javax.servlet-api'
|
||||
compile 'commons-codec:commons-codec:1.10'
|
||||
compile files(jmustacheRepackJar)
|
||||
jarjar 'com.googlecode.jarjar:jarjar:1.3'
|
||||
jmustache 'com.samskivert:jmustache@jar'
|
||||
optional 'javax.validation:validation-api'
|
||||
optional 'commons-codec:commons-codec'
|
||||
testCompile 'org.mockito:mockito-core'
|
||||
testCompile 'org.hamcrest:hamcrest-core'
|
||||
testCompile 'org.hamcrest:hamcrest-library'
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.restdocs.operation.Operation;
|
||||
import org.springframework.restdocs.operation.OperationRequest;
|
||||
@@ -74,10 +75,10 @@ public class CurlRequestSnippet extends TemplatedSnippet {
|
||||
private String getOptions(Operation operation) {
|
||||
StringWriter command = new StringWriter();
|
||||
PrintWriter printer = new PrintWriter(command);
|
||||
writeOptionToIncludeHeadersInOutput(printer);
|
||||
writeHttpBasicAuthorization(operation.getRequest(), printer);
|
||||
writeIncludeHeadersInOutputOption(printer);
|
||||
HttpHeaders headers = writeUserOptionIfNecessary(operation.getRequest(), printer);
|
||||
writeHttpMethodIfNecessary(operation.getRequest(), printer);
|
||||
writeHeaders(operation.getRequest(), printer);
|
||||
writeHeaders(headers, printer);
|
||||
writePartsIfNecessary(operation.getRequest(), printer);
|
||||
|
||||
writeContent(operation.getRequest(), printer);
|
||||
@@ -85,34 +86,37 @@ public class CurlRequestSnippet extends TemplatedSnippet {
|
||||
return command.toString();
|
||||
}
|
||||
|
||||
private void writeOptionToIncludeHeadersInOutput(PrintWriter writer) {
|
||||
private void writeIncludeHeadersInOutputOption(PrintWriter writer) {
|
||||
writer.print("-i");
|
||||
}
|
||||
|
||||
private HttpHeaders writeUserOptionIfNecessary(OperationRequest request,
|
||||
PrintWriter writer) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.putAll(request.getHeaders());
|
||||
String authorization = headers.getFirst(HttpHeaders.AUTHORIZATION);
|
||||
if (isAuthorizationBasicHeader(authorization)) {
|
||||
String credentials = new String(Base64Utils.decodeFromString(authorization
|
||||
.substring(5).trim()));
|
||||
writer.print(String.format(" -u '%s'", credentials));
|
||||
headers.remove(HttpHeaders.AUTHORIZATION);
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
private boolean isAuthorizationBasicHeader(String header) {
|
||||
return header != null && header.startsWith("Basic");
|
||||
}
|
||||
|
||||
private void writeHttpMethodIfNecessary(OperationRequest request, PrintWriter writer) {
|
||||
if (!HttpMethod.GET.equals(request.getMethod())) {
|
||||
writer.print(String.format(" -X %s", request.getMethod()));
|
||||
}
|
||||
}
|
||||
|
||||
private void writeHttpBasicAuthorization(OperationRequest request, PrintWriter writer) {
|
||||
for (Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
|
||||
private void writeHeaders(HttpHeaders headers, PrintWriter writer) {
|
||||
for (Entry<String, List<String>> entry : headers.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));
|
||||
}
|
||||
}
|
||||
@@ -160,8 +164,4 @@ public class CurlRequestSnippet extends TemplatedSnippet {
|
||||
|| HttpMethod.POST.equals(request.getMethod());
|
||||
}
|
||||
|
||||
private boolean isAuthBasicHeader(String key, String value) {
|
||||
return ("Authorization".equals(key) && value.startsWith("Basic"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -260,12 +260,15 @@ public class CurlRequestSnippetTests {
|
||||
}
|
||||
|
||||
@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())
|
||||
public void basicAuthCredentialsAreSuppliedUsingUserOption() throws IOException {
|
||||
this.snippet.expectCurlRequest("basic-auth").withContents(
|
||||
codeBlock("bash").content(
|
||||
"$ curl 'http://localhost/foo' -i -u 'user:secret'"));
|
||||
new CurlRequestSnippet().document(new OperationBuilder("basic-auth", this.snippet
|
||||
.getOutputDirectory())
|
||||
.request("http://localhost/foo")
|
||||
.header(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString("user:secret".getBytes()))
|
||||
.header(HttpHeaders.AUTHORIZATION,
|
||||
"Basic " + Base64Utils.encodeToString("user:secret".getBytes()))
|
||||
.build());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user