Skip to content

Commit

Permalink
Merge pull request #14 from Kissy/master
Browse files Browse the repository at this point in the history
Addition of Request Body parameter
  • Loading branch information
janario committed Jun 9, 2016
2 parents 1120104 + a2e6cb4 commit 1745d29
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 51 deletions.
13 changes: 12 additions & 1 deletion src/main/java/jenkins/plugins/http_request/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class HttpRequest extends Builder {
private Integer timeout = DescriptorImpl.timeout;
private Boolean consoleLogResponseBody = DescriptorImpl.consoleLogResponseBody;
private String authentication = DescriptorImpl.authentication;
private String requestBody = DescriptorImpl.requestBody;
private List<HttpRequestNameValuePair> customHeaders = DescriptorImpl.customHeaders;

@DataBoundConstructor
Expand Down Expand Up @@ -131,6 +132,11 @@ public void setAuthentication(String authentication) {
this.authentication = authentication;
}

@DataBoundSetter
public void setRequestBody(String requestBody) {
this.requestBody = requestBody;
}

@DataBoundSetter
public void setCustomHeaders(List<HttpRequestNameValuePair> customHeaders) {
this.customHeaders = customHeaders;
Expand Down Expand Up @@ -201,6 +207,10 @@ public String getValidResponseContent() {
return validResponseContent;
}

public String getRequestBody() {
return requestBody;
}

@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException
Expand Down Expand Up @@ -232,7 +242,7 @@ public ResponseContentSupplier performHttpRequest(Run<?,?> run, TaskListener lis
logger.println(String.format("URL: %s", evaluatedUrl));

DefaultHttpClient httpclient = new SystemDefaultHttpClient();
RequestAction requestAction = new RequestAction(new URL(evaluatedUrl), httpMode, params);
RequestAction requestAction = new RequestAction(new URL(evaluatedUrl), httpMode, requestBody, params);
HttpClientUtil clientUtil = new HttpClientUtil();
HttpRequestBase httpRequestBase = getHttpRequestBase(logger, requestAction, clientUtil);
HttpContext context = new BasicHttpContext();
Expand Down Expand Up @@ -370,6 +380,7 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
public static final int timeout = 0;
public static final Boolean consoleLogResponseBody = false;
public static final String authentication = "";
public static final String requestBody = "";
public static final List <HttpRequestNameValuePair> customHeaders = Collections.<HttpRequestNameValuePair>emptyList();

public DescriptorImpl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public final class HttpRequestStep extends AbstractStepImpl {
private Integer timeout = DescriptorImpl.timeout;
private Boolean consoleLogResponseBody = DescriptorImpl.consoleLogResponseBody;
private String authentication = DescriptorImpl.authentication;
private String requestBody = DescriptorImpl.requestBody;
private List<HttpRequestNameValuePair> customHeaders = DescriptorImpl.customHeaders;

@DataBoundConstructor
Expand Down Expand Up @@ -160,6 +161,7 @@ public static final class DescriptorImpl extends AbstractStepDescriptorImpl {
public static final int timeout = HttpRequest.DescriptorImpl.timeout;
public static final Boolean consoleLogResponseBody = HttpRequest.DescriptorImpl.consoleLogResponseBody;
public static final String authentication = HttpRequest.DescriptorImpl.authentication;
public static final String requestBody = HttpRequest.DescriptorImpl.requestBody;
public static final List <HttpRequestNameValuePair> customHeaders = Collections.<HttpRequestNameValuePair>emptyList();

public DescriptorImpl() {
Expand Down Expand Up @@ -225,6 +227,7 @@ protected ResponseContentSupplier run() throws Exception {
httpRequest.setTimeout(step.timeout);
httpRequest.setConsoleLogResponseBody(step.consoleLogResponseBody);
httpRequest.setAuthentication(step.authentication);
httpRequest.setRequestBody(step.requestBody);
httpRequest.setCustomHeaders(step.customHeaders);
ResponseContentSupplier response = httpRequest.performHttpRequest(run, listener);
return response;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package jenkins.plugins.http_request.util;

import com.google.common.base.Strings;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.json.JsonWriter;
import hudson.FilePath;
import jenkins.plugins.http_request.HttpMode;
import net.sf.json.JSONArray;
import net.sf.json.util.JSONBuilder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
Expand All @@ -10,6 +16,8 @@
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;

Expand All @@ -18,6 +26,7 @@
import java.nio.charset.Charset;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -45,9 +54,12 @@ public HttpRequestBase createRequestBase(RequestAction requestAction) throws IOE
return makePost(requestAction);
}

private HttpEntity makeEntity(List<HttpRequestNameValuePair> params) throws
private HttpEntity makeEntity(RequestAction requestAction) throws
UnsupportedEncodingException {
return new UrlEncodedFormEntity(params);
if (!Strings.isNullOrEmpty(requestAction.getRequestBody())) {
return new StringEntity(requestAction.getRequestBody());
}
return new UrlEncodedFormEntity(requestAction.getParams());
}

public HttpGet makeGet(RequestAction requestAction) throws IOException {
Expand All @@ -56,7 +68,7 @@ public HttpGet makeGet(RequestAction requestAction) throws IOException {

if (!requestAction.getParams().isEmpty()) {
sb.append(url.contains("?") ? "&" : "?");
final HttpEntity entity = makeEntity(requestAction.getParams());
final HttpEntity entity = makeEntity(requestAction);

final BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent(), Charset.forName("UTF-8")));
String s;
Expand All @@ -75,15 +87,15 @@ public HttpHead makeHead(RequestAction requestAction) throws UnsupportedEncoding
}

public HttpPost makePost(RequestAction requestAction) throws UnsupportedEncodingException {
final HttpEntity httpEntity = makeEntity(requestAction.getParams());
final HttpEntity httpEntity = makeEntity(requestAction);
final HttpPost httpPost = new HttpPost(requestAction.getUrl().toString());
httpPost.setEntity(httpEntity);

return httpPost;
}

public HttpPut makePut(RequestAction requestAction) throws UnsupportedEncodingException {
final HttpEntity entity = makeEntity(requestAction.getParams());
final HttpEntity entity = makeEntity(requestAction);
final HttpPut httpPut = new HttpPut(requestAction.getUrl().toString());
httpPut.setEntity(entity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ public class RequestAction extends AbstractDescribableImpl<RequestAction> {

private final URL url;
private final HttpMode mode;
private final String requestBody;
private final List<HttpRequestNameValuePair> params;

@DataBoundConstructor
public RequestAction(URL url, HttpMode mode, List<HttpRequestNameValuePair> params) {
public RequestAction(URL url, HttpMode mode, String requestBody, List<HttpRequestNameValuePair> params) {
this.url = url;
this.mode = mode;
this.requestBody = requestBody;
this.params = params == null ? new ArrayList<HttpRequestNameValuePair>() : params;
}

Expand All @@ -43,6 +45,10 @@ public List<HttpRequestNameValuePair> getParams() {
return Collections.unmodifiableList(params);
}

public String getRequestBody() {
return requestBody;
}

@Extension
public static class ActionFormAuthenticationDescriptor extends Descriptor<RequestAction> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,48 @@
<f:entry field="httpMode" title="HTTP mode" help="/plugin/http_request/help-httpMode.html">
<f:select />
</f:entry>
<f:entry field="passBuildParameters" title="Pass build params to URL?">
<f:booleanRadio />
</f:entry>

<f:advanced>
<f:entry field="validResponseCodes" title="Response codes expected" help="/plugin/http_request/help-validResponseCodes.html">
<f:textbox default="${descriptor.validResponseCodes}"/>
</f:entry>
<f:entry field="validResponseContent" title="Response content expected" help="/plugin/http_request/help-validResponseContent.html">
<f:textbox />
</f:entry>
<f:entry field="acceptType" title="Accept" help="/plugin/http_request/help-acceptType.html">
<f:select />
</f:entry>
<f:entry field="contentType" title="Content-type" help="/plugin/http_request/help-contentType.html">
<f:select />
</f:entry>
<f:entry field="outputFile" title="Output response to file">
<f:textbox />
</f:entry>
<f:entry field="timeout" title="Connection timeout" help="/plugin/http_request/help-timeout.html">
<f:number default="${descriptor.timeout}"/>
</f:entry>

<f:entry field="consoleLogResponseBody" title="Response body in console?" help="/plugin/http_request/help-consoleLogResponseBody.html">
<f:booleanRadio />
</f:entry>
<f:entry field="authentication" title="Authenticate" help="/plugin/http_request/help-authentication.html">
<f:select />
</f:entry>
<f:entry title="Custom headers">
<f:repeatableProperty field="customHeaders" />
</f:entry>
<f:section title="Authorization">
<f:entry field="authentication" title="Authenticate" help="/plugin/http_request/help-authentication.html">
<f:select />
</f:entry>
</f:section>
<f:section title="Headers">
<f:entry field="acceptType" title="Accept" help="/plugin/http_request/help-acceptType.html">
<f:select />
</f:entry>
<f:entry field="contentType" title="Content-type" help="/plugin/http_request/help-contentType.html">
<f:select />
</f:entry>
<f:entry title="Custom headers">
<f:repeatableProperty field="customHeaders" />
</f:entry>
</f:section>
<f:section title="Body">
<f:entry field="passBuildParameters" title="Pass build params to URL?" help="/plugin/http_request/help-passBuildParameters.html">
<f:booleanRadio />
</f:entry>
<f:entry field="requestBody" title="Request body" help="/plugin/http_request/help-requestBody.html">
<f:textarea />
</f:entry>
</f:section>
<f:section title="Response">
<f:entry field="timeout" title="Connection timeout" help="/plugin/http_request/help-timeout.html">
<f:number default="${descriptor.timeout}"/>
</f:entry>
<f:entry field="validResponseCodes" title="Response codes expected" help="/plugin/http_request/help-validResponseCodes.html">
<f:textbox default="${descriptor.validResponseCodes}"/>
</f:entry>
<f:entry field="validResponseContent" title="Response content expected" help="/plugin/http_request/help-validResponseContent.html">
<f:textbox />
</f:entry>
<f:entry field="outputFile" title="Output response to file" help="/plugin/http_request/help-outputFile.html">
<f:textbox />
</f:entry>
<f:entry field="consoleLogResponseBody" title="Response body in console?" help="/plugin/http_request/help-consoleLogResponseBody.html">
<f:booleanRadio />
</f:entry>
</f:section>
</f:advanced>
</j:jelly>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<div>
<p>Should build parameters be passed to the URL being called?</p>
<p>Ignored when request body is set.</p>
</div>
3 changes: 3 additions & 0 deletions src/main/webapp/help-requestBody.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The raw body of the request.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void configRoundtripGroup3() throws Exception {
params.add(new HttpRequestNameValuePair("param1","value1"));
params.add(new HttpRequestNameValuePair("param2","value2"));

RequestAction action = new RequestAction(new URL("http://www.domain.com/"),HttpMode.GET,params);
RequestAction action = new RequestAction(new URL("http://www.domain.com/"),HttpMode.GET,null,params);
List<RequestAction> actions = new ArrayList<RequestAction>();
actions.add(action);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void configRoundtripGroup3() throws Exception {
params.add(new HttpRequestNameValuePair("param1","value1"));
params.add(new HttpRequestNameValuePair("param2","value2"));

RequestAction action = new RequestAction(new URL("http://www.domain.com/"),HttpMode.GET,params);
RequestAction action = new RequestAction(new URL("http://www.domain.com/"),HttpMode.GET,null,params);
List<RequestAction> actions = new ArrayList<RequestAction>();
actions.add(action);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public void canDoFormAuthentication() throws Exception {
params.add(new HttpRequestNameValuePair("param1","value1"));
params.add(new HttpRequestNameValuePair("param2","value2"));

RequestAction action = new RequestAction(new URL(baseURL+"/reqAction"),HttpMode.GET,params);
RequestAction action = new RequestAction(new URL(baseURL+"/reqAction"),HttpMode.GET,null,params);
List<RequestAction> actions = new ArrayList<RequestAction>();
actions.add(action);

Expand Down Expand Up @@ -523,7 +523,7 @@ public void rejectedFormCredentialsFailTheBuild() throws Exception {
params.add(new HttpRequestNameValuePair("param1","value1"));
params.add(new HttpRequestNameValuePair("param2","value2"));

RequestAction action = new RequestAction(new URL(baseURL+"/formAuthBad"),HttpMode.GET,params);
RequestAction action = new RequestAction(new URL(baseURL+"/formAuthBad"),HttpMode.GET,null,params);
List<RequestAction> actions = new ArrayList<RequestAction>();
actions.add(action);

Expand Down Expand Up @@ -561,7 +561,7 @@ public void invalidKeyFormAuthenticationFailsTheBuild() throws Exception {
params.add(new HttpRequestNameValuePair("param2","value2"));

// The request action won't be sent but we need to prepare it
RequestAction action = new RequestAction(new URL(baseURL+"/non-existent"),HttpMode.GET,params);
RequestAction action = new RequestAction(new URL(baseURL+"/non-existent"),HttpMode.GET,null,params);
List<RequestAction> actions = new ArrayList<RequestAction>();
actions.add(action);

Expand Down
53 changes: 50 additions & 3 deletions src/test/java/jenkins/plugins/http_request/HttpRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,53 @@ public void doNotPassBuildParametersWithBuildParameters() throws Exception {
j.assertLogContains(allIsWellMessage,build);
}

@Test
public void passRequestBodyWhenRequestIsPostAndBodyIsPresent() throws Exception {
// Prepare the server
final HttpHost target = start();
final String baseURL = "http://localhost:" + target.getPort();

// Prepare HttpRequest
HttpRequest httpRequest = new HttpRequest(baseURL+"/checkRequestBody");
httpRequest.setConsoleLogResponseBody(true);

// Activate requsetBody
httpRequest.setHttpMode(HttpMode.POST);
httpRequest.setRequestBody("TestRequestBody");

// Run build
FreeStyleProject project = j.createFreeStyleProject();
project.getBuildersList().add(httpRequest);
FreeStyleBuild build = project.scheduleBuild2(0).get();

// Check expectations
j.assertBuildStatusSuccess(build);
j.assertLogContains(allIsWellMessage,build);
}

@Test
public void doNotPassRequestBodyWhenMethodIsGet() throws Exception {
// Prepare the server
final HttpHost target = start();
final String baseURL = "http://localhost:" + target.getPort();

// Prepare HttpRequest
HttpRequest httpRequest = new HttpRequest(baseURL+"/doGET");
httpRequest.setConsoleLogResponseBody(true);

// Activate passBuildParameters
httpRequest.setRequestBody("TestRequestBody");

// Run build
FreeStyleProject project = j.createFreeStyleProject();
project.getBuildersList().add(httpRequest);
FreeStyleBuild build = project.scheduleBuild2(0).get();

// Check expectations
j.assertBuildStatusSuccess(build);
j.assertLogContains(allIsWellMessage,build);
}

@Test
public void doAllRequestTypes() throws Exception {
for (HttpMode mode: HttpMode.values()) {
Expand Down Expand Up @@ -576,7 +623,7 @@ public void canDoFormAuthentication() throws Exception {
params.add(new HttpRequestNameValuePair("param1","value1"));
params.add(new HttpRequestNameValuePair("param2","value2"));

RequestAction action = new RequestAction(new URL(baseURL+"/reqAction"),HttpMode.GET,params);
RequestAction action = new RequestAction(new URL(baseURL+"/reqAction"),HttpMode.GET,null,params);
List<RequestAction> actions = new ArrayList<RequestAction>();
actions.add(action);

Expand Down Expand Up @@ -609,7 +656,7 @@ public void rejectedFormCredentialsFailTheBuild() throws Exception {
params.add(new HttpRequestNameValuePair("param1","value1"));
params.add(new HttpRequestNameValuePair("param2","value2"));

RequestAction action = new RequestAction(new URL(baseURL+"/formAuthBad"),HttpMode.GET,params);
RequestAction action = new RequestAction(new URL(baseURL+"/formAuthBad"),HttpMode.GET,null,params);
List<RequestAction> actions = new ArrayList<RequestAction>();
actions.add(action);

Expand Down Expand Up @@ -645,7 +692,7 @@ public void invalidKeyFormAuthenticationFailsTheBuild() throws Exception {
params.add(new HttpRequestNameValuePair("param2","value2"));

// The request action won't be sent but we need to prepare it
RequestAction action = new RequestAction(new URL(baseURL+"/non-existent"),HttpMode.GET,params);
RequestAction action = new RequestAction(new URL(baseURL+"/non-existent"),HttpMode.GET,null,params);
List<RequestAction> actions = new ArrayList<RequestAction>();
actions.add(action);

Expand Down
Loading

0 comments on commit 1745d29

Please sign in to comment.