Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mgawinec committed Sep 28, 2017
2 parents aac8dee + 5679a33 commit 1d73119
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 30 deletions.
60 changes: 36 additions & 24 deletions src/main/java/com/github/dzieciou/testing/curl/Http2Curl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,38 @@

package com.github.dzieciou.testing.curl;

import io.restassured.internal.multipart.RestAssuredMultiPartEntity;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.impl.client.RequestWrapper;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.impl.client.RequestWrapper;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.restassured.internal.multipart.RestAssuredMultiPartEntity;


/**
Expand Down Expand Up @@ -309,26 +313,34 @@ private void handlePart(FormBodyPart bodyPart, CurlCommand curl) {
}

private void handleNotIgnoredHeaders(List<Header> headers, Set<String> ignoredHeaders,
CurlCommand curl) {
CurlCommand curl) {
headers
.stream()
.filter(h -> !ignoredHeaders.contains(h.getName()))
.forEach(h -> curl.addHeader(h.getName(), h.getValue()));
}

private List<Header> handleAuthenticationHeader(List<Header> headers, CurlCommand curl) {
headers.stream()
.filter(Http2Curl::isBasicAuthentication)
.forEach(h ->
{

List<Header> remainingHeaders = new ArrayList<>(headers);
Iterator<Header> it = remainingHeaders.iterator();
while (it.hasNext()) {
Header h = it.next();
if (isBasicAuthentication(h)) {
try {
String credentials = h.getValue().replaceAll("Basic ", "");
String decodedCredentials = new String(Base64.getDecoder().decode(credentials));
String[] userAndPassword = decodedCredentials.split(":");
String[] userAndPassword = decodedCredentials.split(":", -1);
curl.setServerAuthentication(userAndPassword[0], userAndPassword[1]);
});

headers = headers.stream().filter(h -> !isBasicAuthentication(h)).collect(Collectors.toList());
return headers;
it.remove();
break; // There can be only one authentication headers
} catch (IllegalArgumentException | IndexOutOfBoundsException e) {
log.warn("This is not valid Basic authentication header: {}", h.getValue());
}
}
}
return remainingHeaders;
}


}
33 changes: 27 additions & 6 deletions src/test/java/com/github/dzieciou/testing/curl/Http2CurlTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package com.github.dzieciou.testing.curl;


import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
Expand All @@ -18,6 +12,13 @@
import org.apache.http.message.BasicNameValuePair;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

@Test(groups = "unit")
public class Http2CurlTest {

Expand All @@ -37,6 +38,25 @@ public void shouldPrintBasicAuthnUserCredentials() throws Exception {
equalTo("curl 'http://test.com:8080/items/query?x=y#z' -u 'xx:yy' --compressed -k -v"));
}

@Test
public void shouldPrintBasicAuthnUserCredentialsWithoutPassword() throws Exception {
HttpGet getRequest = new HttpGet("http://test.com:8080/items/query?x=y#z");
String invalidEncodedCredentials = Base64.getEncoder().encodeToString("xx:".getBytes());
getRequest.addHeader("Authorization", "Basic " + invalidEncodedCredentials);
assertThat(getNonWindowsHttp2Curl().generateCurl(getRequest),
equalTo("curl 'http://test.com:8080/items/query?x=y#z' -u 'xx:' --compressed -k -v"));
}


@Test
public void shouldNotPrintInvalidBasicAuthnUserCredentials() throws Exception {
HttpGet getRequest = new HttpGet("http://test.com:8080/items/query?x=y#z");
String invalidEncodedCredentials = "xxx";
getRequest.addHeader("Authorization", "Basic " + invalidEncodedCredentials);
assertThat(getNonWindowsHttp2Curl().generateCurl(getRequest),
equalTo("curl 'http://test.com:8080/items/query?x=y#z' -H 'Authorization: Basic xxx' --compressed -k -v"));
}

@Test
public void shouldPrintPostRequestProperly() throws Exception {
HttpPost postRequest = new HttpPost("http://google.pl/");
Expand Down Expand Up @@ -135,4 +155,5 @@ public Http2Curl getNonWindowsHttp2Curl() {
return new Http2Curl(
Options.builder().targetPlatform(Platform.UNIX).useShortForm().printSingleliner().build());
}

}

0 comments on commit 1d73119

Please sign in to comment.