From 8b5cd64e82d7c9844f216143b1773456338c2645 Mon Sep 17 00:00:00 2001 From: Haroon Sheikh Date: Mon, 3 Aug 2020 16:46:35 +0100 Subject: [PATCH] Fixes an issue with body when empty or null fields provided --- CHANGELOG.md | 6 ++++++ pom.xml | 2 +- .../sitture/unirestcurl/CurlBuilder.java | 2 +- .../sitture/unirestcurl/CurlBuilderTest.java | 20 +++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3a0b3f..8d8de8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pom.xml b/pom.xml index 1ef7ad2..36a05b2 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ unirest-curl ${project.artifactId} jar - 0.1.0 + 0.1.2 An interceptor to log Unirest requests as curl requests. https://github.com/sitture/unirest-curl diff --git a/src/main/java/com/github/sitture/unirestcurl/CurlBuilder.java b/src/main/java/com/github/sitture/unirestcurl/CurlBuilder.java index 321becd..7a02a9c 100644 --- a/src/main/java/com/github/sitture/unirestcurl/CurlBuilder.java +++ b/src/main/java/com/github/sitture/unirestcurl/CurlBuilder.java @@ -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() diff --git a/src/test/java/com/github/sitture/unirestcurl/CurlBuilderTest.java b/src/test/java/com/github/sitture/unirestcurl/CurlBuilderTest.java index 5526358..9336574 100644 --- a/src/test/java/com/github/sitture/unirestcurl/CurlBuilderTest.java +++ b/src/test/java/com/github/sitture/unirestcurl/CurlBuilderTest.java @@ -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; @@ -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 bodyMap = new ConcurrentHashMap<>();