Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,18 @@
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()) {

Check warning on line 496 in src/main/java/jenkins/plugins/http_request/HttpRequestExecution.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 496 is only partially covered, 2 branches are missing
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());

Check warning on line 501 in src/main/java/jenkins/plugins/http_request/HttpRequestExecution.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 499-501 are not covered by tests
}
}
throw new AbortException("Fail: Status code " + response.getStatus() + " is not in the accepted range: " + validResponseCodes + " while calling " + url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/jenkins/plugins/http_request/HttpRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down