diff --git a/src/main/java/jenkins/plugins/http_request/HttpRequestExecution.java b/src/main/java/jenkins/plugins/http_request/HttpRequestExecution.java index 442b433..ab56feb 100644 --- a/src/main/java/jenkins/plugins/http_request/HttpRequestExecution.java +++ b/src/main/java/jenkins/plugins/http_request/HttpRequestExecution.java @@ -489,6 +489,18 @@ private void responseCodeIsValid(ResponseContentSupplier response) throws AbortE return; } } + // Log response body to help debug errors from REST APIs (only if not already logged) + if (!consoleLogResponseBody) { + try { + String content = response.getContent(); + if (content != null && !content.isEmpty()) { + logger().println("Response: \n" + content); + } + } catch (Exception e) { + // Ignore errors when trying to read response content + logger().println("Unable to read response body: " + e.getMessage()); + } + } throw new AbortException("Fail: Status code " + response.getStatus() + " is not in the accepted range: " + validResponseCodes + " while calling " + url); } diff --git a/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java b/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java index fd1cf84..80f2bd8 100644 --- a/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java +++ b/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java @@ -249,6 +249,32 @@ void invalidResponseCodeFailsTheBuild() throws Exception { j.assertLogContains("Throwing status 400 for test",run); j.assertLogContains("Fail: Status code 400 is not in the accepted range: 100:399", run); j.assertLogContains(" while calling " + baseURL(), run); + // Not logged twice: consoleLogResponseBody already printed it, the + // failure-path logging must not print the body again + assertEquals(1, j.getLog(run).split("Response: \n", -1).length - 1); + } + + @Test + void invalidResponseCodeLogsResponseBody() throws Exception { + // Prepare the server + registerInvalidStatusCode(); + + // Configure the build (consoleLogResponseBody not set, defaults to false) + WorkflowJob proj = j.jenkins.createProject(WorkflowJob.class, "proj"); + proj.setDefinition(new CpsFlowDefinition( + "def response = httpRequest url:'"+baseURL()+"/invalidStatusCode'\n" + + "println('Status: '+response.getStatus())\n", + true)); + + // Execute the build + WorkflowRun run = proj.scheduleBuild2(0).get(); + + // Check expectations: the response body is logged on failure even + // without consoleLogResponseBody, so the error explanation from the + // server is not lost + j.assertBuildStatus(Result.FAILURE, run); + j.assertLogContains("Throwing status 400 for test",run); + j.assertLogContains("Fail: Status code 400 is not in the accepted range: 100:399", run); } @Test diff --git a/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java b/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java index a3b5969..d2f2242 100644 --- a/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java +++ b/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java @@ -410,6 +410,27 @@ void invalidResponseCodeFailsTheBuild() throws Exception { this.j.assertLogContains("Fail: Status code 400 is not in the accepted range: 100:399", build); } + @Test + void invalidResponseCodeLogsResponseBody() throws Exception { + // Prepare the server + registerInvalidStatusCode(); + + // Prepare HttpRequest (consoleLogResponseBody not set, defaults to false) + HttpRequest httpRequest = new HttpRequest(baseURL() + "/invalidStatusCode"); + + // Run build + FreeStyleProject project = this.j.createFreeStyleProject(); + project.getBuildersList().add(httpRequest); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + // Check expectations: the response body is logged on failure even + // without consoleLogResponseBody, so the error explanation from the + // server is not lost + this.j.assertBuildStatus(Result.FAILURE, build); + this.j.assertLogContains("Throwing status 400 for test", build); + this.j.assertLogContains("Fail: Status code 400 is not in the accepted range: 100:399", build); + } + @Test void invalidResponseCodeIsAccepted() throws Exception { // Prepare the server