diff --git a/src/main/java/jenkins/plugins/http_request/ResponseContentSupplier.java b/src/main/java/jenkins/plugins/http_request/ResponseContentSupplier.java index b9e4b16f..9dde527e 100644 --- a/src/main/java/jenkins/plugins/http_request/ResponseContentSupplier.java +++ b/src/main/java/jenkins/plugins/http_request/ResponseContentSupplier.java @@ -6,6 +6,8 @@ import java.io.InputStreamReader; import java.io.Serializable; import java.nio.charset.Charset; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -111,6 +113,20 @@ public String getContent() { } } + public String getContentSha1Sum() { + try { + MessageDigest md = MessageDigest.getInstance("SHA-1"); + byte[] result = md.digest(content.getBytes()); + StringBuilder hexString = new StringBuilder(); + for (byte b : result) { + hexString.append(String.format("%02x", b)); + } + return hexString.toString(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + return null; + } + } @Whitelisted public InputStream getContentStream() { return contentStream; diff --git a/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java b/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java index f594a778..699dde15 100644 --- a/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java +++ b/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java @@ -277,6 +277,26 @@ public void invalidResponseCodeIsAccepted() throws Exception { j.assertLogNotContains(" while calling " + baseURL(), run); } + @Test + public void responseHashSumCalculated() throws Exception { + // Prepare the server + registerRequestChecker(HttpMode.GET); + + // Configure the build + WorkflowJob proj = j.jenkins.createProject(WorkflowJob.class, "proj"); + proj.setDefinition(new CpsFlowDefinition( + "def response = httpRequest '"+baseURL()+"/doGET'\n" + + " consoleLogResponseBody: true\n" + + "println('sha1sum: '+response.getContentSha1Sum())\n", + true)); + + // Execute the build + WorkflowRun run = proj.scheduleBuild2(0).get(); + + // Check expectations + j.assertLogContains("sha1sum: ", run); + } + @Test public void reverseRangeFailsTheBuild() throws Exception { // Prepare the server