Skip to content

Commit

Permalink
Fixes an issue with body when empty or null fields provided
Browse files Browse the repository at this point in the history
  • Loading branch information
haroon-sheikh committed Aug 3, 2020
1 parent 289c04f commit 8b5cd64
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ 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.2

### Fixed

- Fixes an issue with body when empty or null fields provided.

## 0.1.1

### Updated
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>unirest-curl</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<version>0.1.0</version>
<version>0.1.2</version>
<description>An interceptor to log Unirest requests as curl requests.</description>
<url>https://github.com/sitture/unirest-curl</url>
<developers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private String getHeaders() {
private String getBody() {
String processedBody = "";
if (request.getBody().isPresent()) {
if (request.getBody().get().multiParts().size() == 0) {
if (request.getBody().get().multiParts().size() == 0 && null != request.getBody().get().uniPart()) {
processedBody = String.format(REQUEST_BODY, request.getBody().get().uniPart().getValue());
} else {
processedBody = request.getBody().get().multiParts().stream()
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/github/sitture/unirestcurl/CurlBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import kong.unirest.Unirest;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -51,6 +52,25 @@ public void canTransformPostRequestWithHeadersAndStringBody() {
assertEquals(expectedCurl, generatedCurl, UNEXPECTED_ERROR);
}

@Test
public void canTransformPostRequestWithHeadersAndNullBodyMap() {
final HttpRequest<?> request = Unirest.post(TEST_URL)
.header("content-type", "application/xml")
.fields(null);
final String generatedCurl = new CurlBuilder(request).build();
final String expectedCurl = String.format("curl --verbose --request POST --url \"%s\" --header \"content-type: application/xml\"", TEST_URL);
assertEquals(expectedCurl, generatedCurl, UNEXPECTED_ERROR);
}

@Test
public void canTransformPostRequestWithHeadersAndEmptyBodyMap() {
final HttpRequest<?> request = Unirest.post(TEST_URL)
.fields(Collections.emptyMap());
final String generatedCurl = new CurlBuilder(request).build();
final String expectedCurl = String.format("curl --verbose --request POST --url \"%s\"", TEST_URL);
assertEquals(expectedCurl, generatedCurl, UNEXPECTED_ERROR);
}

@Test
public void canTransformPostRequestWithHeadersAndFieldsBodyMap() {
final Map<String, Object> bodyMap = new ConcurrentHashMap<>();
Expand Down

0 comments on commit 8b5cd64

Please sign in to comment.