From fe8a58c986883e69dc143ef51046074aab34916a Mon Sep 17 00:00:00 2001 From: Belen Pruvost Date: Tue, 14 Sep 2021 17:20:39 +0200 Subject: SONAR-15388 - BBS Validation with missing response body --- .../alm/client/bitbucketserver/BitbucketServerRestClient.java | 2 +- .../client/bitbucketserver/BitbucketServerRestClientTest.java | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java index 12b4c40572e..70a4024cef8 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java @@ -168,7 +168,7 @@ public class BitbucketServerRestClient { protected static String getErrorMessage(ResponseBody body) throws IOException { String bodyString = body.string(); - if (equals(MediaType.parse("application/json;charset=utf-8"), body.contentType())) { + if (equals(MediaType.parse("application/json;charset=utf-8"), body.contentType()) && !isNullOrEmpty(bodyString)) { try { return Stream.of(buildGson().fromJson(bodyString, Errors.class).errorData) .map(e -> e.exceptionName + " " + e.message) diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java index 8106af46ba0..45b49116f3e 100644 --- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java +++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java @@ -486,6 +486,17 @@ public class BitbucketServerRestClientTest { .extracting(e -> ((BitbucketServerException) e).getHttpStatus()).isEqualTo(404); } + @Test + public void fail_validate_url_when_body_is_empty() { + server.enqueue(new MockResponse().setResponseCode(404).setBody("")); + + String serverUrl = server.url("/").toString(); + assertThatThrownBy(() -> underTest.validateUrl(serverUrl)) + .isInstanceOf(BitbucketServerException.class) + .hasMessage("") + .extracting(e -> ((BitbucketServerException) e).getHttpStatus()).isEqualTo(404); + } + @Test public void fail_validate_url_when_validate_url_return_non_json_payload() { server.enqueue(new MockResponse().setResponseCode(400) -- cgit v1.2.3