Skip to content

Commit

Permalink
Merge pull request #31 from jglick/InvalidContentTypeException
Browse files Browse the repository at this point in the history
Treat InvalidContentTypeException as a missing parameter value

(cherry picked from commit 924e865)
  • Loading branch information
jglick committed Mar 1, 2021
1 parent 11b128c commit 927022b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;

Expand Down Expand Up @@ -78,9 +79,18 @@ protected AbstractFileParameterDefinition(String name) {

@Override public ParameterValue createValue(StaplerRequest req) {
try {
FileItem src = req.getFileItem(getName());
FileItem src;
try {
src = req.getFileItem(getName());
} catch (ServletException x) {
if (x.getCause() instanceof FileUploadBase.InvalidContentTypeException) {
src = null;
} else {
throw x;
}
}
if (src == null) {
throw new RuntimeException("No file was uploaded");
return null;
}
AbstractFileParameterValue p;
try (InputStream in = src.getInputStream()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockAuthorizationStrategy;

Expand Down Expand Up @@ -95,4 +96,21 @@ public class AbstractFileParameterDefinitionTest {
r.assertLogContains("received myfile.txt: dXBsb2FkZWQgY29udGVudCBoZXJl", b);
}

@Issue("https://github.com/jenkinsci/file-parameters-plugin/issues/26")
@Test public void restMissingValue() throws Exception {
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
r.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.ADMINISTER).everywhere().to("admin"));
WorkflowJob p = r.createProject(WorkflowJob.class, "myjob");
p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE")));
p.setDefinition(new CpsFlowDefinition("echo(/received $env.FILE_FILENAME: $env.FILE/)", true));
// Like: curl -u $auth $jenkins/job/myjob/buildWithParameters
WebRequest req = new WebRequest(new URL(r.getURL() + "job/myjob/buildWithParameters"), HttpMethod.POST);
User.getById("admin", true); // TODO workaround for https://github.com/jenkinsci/jenkins-test-harness/pull/273
r.createWebClient().withBasicApiToken("admin").getPage(req);
r.waitUntilNoActivity();
WorkflowRun b = p.getBuildByNumber(1);
assertNotNull(b);
r.assertLogContains("received null: null", b);
}

}

0 comments on commit 927022b

Please sign in to comment.