Skip to content

Commit

Permalink
Adds a space between header key and value
Browse files Browse the repository at this point in the history
  • Loading branch information
haroon-sheikh committed Aug 1, 2020
1 parent 281954c commit 289c04f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project are documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 0.1.1

### Updated

- Adds a space between header key and value. i.e. "--header Key: Value"
- Splits query params into separate `--data` items

## 0.1.0

### Added
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/github/sitture/unirestcurl/CurlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CurlBuilder {
private static final String CURL_PREFIX = "curl --verbose";
private static final String REQUEST_METHOD = "--request %s";
private static final String REQUEST_URL = "--url \"%s\"";
private static final String REQUEST_HEADER = "--header \"%s:%s\"";
private static final String REQUEST_HEADER = "--header \"%s: %s\"";
private static final String REQUEST_BODY = "--data '%s'";
private static final String SPACE_DELIMITER = " ";
private static final String EMPTY_STRING = "";
Expand Down Expand Up @@ -67,11 +67,10 @@ private String getBody() {
if (request.getBody().get().multiParts().size() == 0) {
processedBody = String.format(REQUEST_BODY, request.getBody().get().uniPart().getValue());
} else {
final String fields = request.getBody().get().multiParts().stream()
processedBody = request.getBody().get().multiParts().stream()
.filter(bodyPart -> bodyPart instanceof ParamPart)
.map(bodyPart -> String.format("%s=%s", bodyPart.getName(), bodyPart.getValue()))
.collect(Collectors.joining("&"));
processedBody = String.format(REQUEST_BODY, fields);
.map(bodyPart -> String.format("--data '%s=%s'", bodyPart.getName(), bodyPart.getValue()))
.collect(Collectors.joining(SPACE_DELIMITER));
}
}
return processedBody;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void canTransformGetRequestWithHeaders() {
.header("Content-Type", "application/json")
.basicAuth("username", "password");
final String generatedCurl = new CurlBuilder(request).build();
final String expectedCurl = String.format("curl --verbose --url \"%s\" --header \"Content-Type:application/json\" --header \"Authorization:Basic dXNlcm5hbWU6cGFzc3dvcmQ=\"", TEST_URL);
final String expectedCurl = String.format("curl --verbose --url \"%s\" --header \"Content-Type: application/json\" --header \"Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=\"", TEST_URL);
assertEquals(expectedCurl, generatedCurl, UNEXPECTED_ERROR);
}

Expand All @@ -37,7 +37,7 @@ public void canTransformPostRequestWithHeaders() {
final HttpRequest<?> request = Unirest.post(TEST_URL)
.header("Accept", "application/json");
final String generatedCurl = new CurlBuilder(request).build();
final String expectedCurl = String.format("curl --verbose --request POST --url \"%s\" --header \"Accept:application/json\"", TEST_URL);
final String expectedCurl = String.format("curl --verbose --request POST --url \"%s\" --header \"Accept: application/json\"", TEST_URL);
assertEquals(expectedCurl, generatedCurl, UNEXPECTED_ERROR);
}

Expand All @@ -47,7 +47,7 @@ public void canTransformPostRequestWithHeadersAndStringBody() {
.header("content-type", "application/xml")
.body("{\"test\": \"body\"}");
final String generatedCurl = new CurlBuilder(request).build();
final String expectedCurl = String.format("curl --verbose --request POST --url \"%s\" --header \"content-type:application/xml\" --data '{\"test\": \"body\"}'", TEST_URL);
final String expectedCurl = String.format("curl --verbose --request POST --url \"%s\" --header \"content-type: application/xml\" --data '{\"test\": \"body\"}'", TEST_URL);
assertEquals(expectedCurl, generatedCurl, UNEXPECTED_ERROR);
}

Expand All @@ -61,7 +61,7 @@ public void canTransformPostRequestWithHeadersAndFieldsBodyMap() {
.header("Content-Type", "application/x-www-form-urlencoded")
.fields(bodyMap);
final String generatedCurl = new CurlBuilder(request).build();
final String expectedCurl = String.format("curl --verbose --request POST --url \"%s\" --header \"Accept:application/json\" --header \"Content-Type:application/x-www-form-urlencoded\" --data 'key1=value&key2=2'", TEST_URL);
final String expectedCurl = String.format("curl --verbose --request POST --url \"%s\" --header \"Accept: application/json\" --header \"Content-Type: application/x-www-form-urlencoded\" --data 'key1=value' --data 'key2=2'", TEST_URL);
assertEquals(expectedCurl, generatedCurl, UNEXPECTED_ERROR);
}

Expand All @@ -72,7 +72,7 @@ public void canTransformPostRequestWithHeadersAndSingleFields() {
.field("key1", "value")
.field("key2", "value2");
final String generatedCurl = new CurlBuilder(request).build();
final String expectedCurl = String.format("curl --verbose --request POST --url \"%s\" --header \"Accept:application/xml\" --data 'key1=value&key2=value2'", TEST_URL);
final String expectedCurl = String.format("curl --verbose --request POST --url \"%s\" --header \"Accept: application/xml\" --data 'key1=value' --data 'key2=value2'", TEST_URL);
assertEquals(expectedCurl, generatedCurl, UNEXPECTED_ERROR);
}

Expand Down

0 comments on commit 289c04f

Please sign in to comment.