Skip to content

feat: new getContentSha1Sum function #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -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;
Expand Down Expand Up @@ -111,6 +113,20 @@
}
}

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;

Check warning on line 127 in src/main/java/jenkins/plugins/http_request/ResponseContentSupplier.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 118-127 are not covered by tests
}
}
@Whitelisted
public InputStream getContentStream() {
return contentStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading