]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15388 - BBS Validation with missing response body
authorBelen Pruvost <belen.pruvost@sonarsource.com>
Tue, 14 Sep 2021 15:20:39 +0000 (17:20 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 14 Sep 2021 20:03:25 +0000 (20:03 +0000)
server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java
server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java

index 12b4c40572e5f1e08d2b813ca2672a15f1be3518..70a4024cef8dff32dc8fdac5d13caa80f2c96944 100644 (file)
@@ -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)
index 8106af46ba068b2a0e1151de23746d7893f26ae8..45b49116f3e588d18060babaf7fed327f80deb70 100644 (file)
@@ -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)