Skip to content

Commit

Permalink
[Curl] add data parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
edigonzales committed Dec 13, 2024
1 parent 75d8ae0 commit fdac671
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void geodienste_Ok() throws Exception {
File projectDirectory = new File(System.getProperty("user.dir") + "/src/integrationTest/jobs/CurlGeodienste");
GradleVariable[] variables = { GradleVariable.newGradleProperty("mockWebServerPort", String.valueOf(mockWebServer.getPort())) };

// Execute task
IntegrationTestUtil.executeTestRunner(projectDirectory, variables);

// Validate result
Expand Down Expand Up @@ -73,6 +74,7 @@ public void geodienste_Fail() throws Exception {
File projectDirectory = new File(System.getProperty("user.dir") + "/src/integrationTest/jobs/CurlGeodienste");
GradleVariable[] variables = { GradleVariable.newGradleProperty("mockWebServerPort", String.valueOf(mockWebServer.getPort())) };

// Execute task
assertThrows(Throwable.class, () -> {
IntegrationTestUtil.executeTestRunner(projectDirectory, variables);
});
Expand All @@ -88,6 +90,7 @@ public void planregister_Ok() throws Exception {
File projectDirectory = new File(System.getProperty("user.dir") + "/src/integrationTest/jobs/CurlPlanregister");
GradleVariable[] variables = { GradleVariable.newGradleProperty("mockWebServerPort", String.valueOf(mockWebServer.getPort())) };

// Execute task
IntegrationTestUtil.executeTestRunner(projectDirectory, variables);

// Validate result
Expand All @@ -100,10 +103,13 @@ public void planregister_Ok() throws Exception {

@Test
public void downloadFile_Ok() throws Exception {
// Prepare
File projectDirectory = new File(System.getProperty("user.dir") + "/src/integrationTest/jobs/CurlDownload");

// Execute task
IntegrationTestUtil.executeTestRunner(projectDirectory);

// Validate result
String content = new String(Files.readAllBytes(Paths.get(projectDirectory + "/README.md")));
assertTrue(content.contains("_GRETL_"));
assertTrue(content.contains("Licencse"));
Expand Down
69 changes: 48 additions & 21 deletions gretl/src/main/java/ch/so/agi/gretl/tasks/Curl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;

import ch.so.agi.gretl.logging.GretlLogger;
Expand All @@ -36,73 +39,81 @@

import javax.annotation.Nullable;


// README.md eventuell
// abstract class geht erst mit 5.6 oder so. Nicht mit 5.1.1
// Dann kommen aber viele Warnungen von anderen Tasks wegen fehlendem Getter o.ae.
// Publisher-Ansatz geht nicht, weil dann wird wirklich ein Objekt vom Typ Property erwartet.
// Das ist fuer Anwender doof.

public class Curl extends DefaultTask {
protected GretlLogger log;

private String serverUrl;
private MethodType method;
private int expectedStatusCode;
private Integer expectedStatusCode;
private String expectedBody;
private Map<String,Object> formData; // curl [URL] -F key1=value1 -F file1=@my_file.xtf
private File outputFile; // curl [URL] -o
private File dataBinary; // curl [URL] --data-binary / ueberschreibt formData, siehe setEntity (glaub)
private String data; // curl [URL] --data
private Map<String,String> headers; // curl [URL] -H ... -H ...
private String user;
private String password;

@Internal
@Input
public String getServerUrl() {
return serverUrl;
}

@Internal
@Input
@Optional
public MethodType getMethod() {
return method;
}

@Internal
public int getExpectedStatusCode() {
@Input
public Integer getExpectedStatusCode() {
return expectedStatusCode;
}

@Internal
@Input
@Optional
public String getExpectedBody() {
return expectedBody;
}

@Internal
@Input
@Optional
public Map<String, Object> getFormData() {
return formData;
}

@Internal
@OutputFile
@Optional
public File getOutputFile() {
return outputFile;
}

@Internal
@InputFile
@Optional
public File getDataBinary() {
return dataBinary;
}

@Input
@Optional
public String getData() {
return data;
}

@Internal
@Input
@Optional
public Map<String, String> getHeaders() {
return headers;
}

@Internal
@Input
@Optional
public String getUser() {
return user;
}

@Internal
@Input
@Optional
public String getPassword() {
return password;
}
Expand Down Expand Up @@ -134,6 +145,10 @@ public void setOutputFile(File outputFile) {
public void setDataBinary(File dataBinary) {
this.dataBinary = dataBinary;
}

public void setData(String data) {
this.data = data;
}

public void setHeaders(Map<String, String> headers) {
this.headers = headers;
Expand All @@ -150,7 +165,15 @@ public void setPassword(String password) {
@TaskAction
public void request() throws ClientProtocolException, IOException {
log = LogEnvironment.getLogger(Curl.class);

if (serverUrl == null) {
throw new IllegalArgumentException("serverUrl must not be null");
}

if (expectedStatusCode == null) {
throw new IllegalArgumentException("expectedStatusCode must not be null");
}

RequestBuilder requestBuilder;
if (method.equals(MethodType.GET)) {
requestBuilder = RequestBuilder.get();
Expand Down Expand Up @@ -178,8 +201,12 @@ public void request() throws ClientProtocolException, IOException {
}

if (dataBinary != null) {
byte[] data = Files.readAllBytes(dataBinary.toPath());
requestBuilder.setEntity(EntityBuilder.create().setBinary(data).build());
byte[] data = Files.readAllBytes(((File)dataBinary).toPath());
requestBuilder.setEntity(EntityBuilder.create().setBinary(data).build());
}

if (data != null) {
requestBuilder.setEntity(EntityBuilder.create().setText(data).build());
}

if (headers != null) {
Expand Down

0 comments on commit fdac671

Please sign in to comment.