BAEL-2452: Using curl from Java (#6113)

* Renamed from Unit Test to Live Test

* Update JavaCurlExamplesLiveTest.java
This commit is contained in:
Olaniyi Anjola 2019-01-10 18:51:30 -05:00 committed by maibin
parent c5415eecac
commit c4edb95d4e
1 changed files with 12 additions and 17 deletions

View File

@ -3,19 +3,16 @@ package com.baeldung.curltojava;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class JavaCurlExamplesUnitTest { public class JavaCurlExamplesLiveTest {
@Test @Test
public void givenCommand_whenCalled_thenProduceZeroExitCode() throws IOException { public void givenCommand_whenCalled_thenProduceZeroExitCode() throws IOException {
String command = "curl --location --request GET \"https://postman-echo.com/get?foo1=bar1&foo2=bar2\""; String command = "curl -X GET https://postman-echo.com/get?foo1=bar1&foo2=bar2";
ProcessBuilder processBuilder = new ProcessBuilder(command.replaceAll("\"", "").split(" ")); ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
processBuilder.directory(new File("/home/")); processBuilder.directory(new File("/home/"));
Process process = processBuilder.start(); Process process = processBuilder.start();
InputStream inputStream = process.getInputStream(); InputStream inputStream = process.getInputStream();
@ -28,8 +25,8 @@ public class JavaCurlExamplesUnitTest {
@Test @Test
public void givenNewCommands_whenCalled_thenCheckIfIsAlive() throws IOException { public void givenNewCommands_whenCalled_thenCheckIfIsAlive() throws IOException {
String command = "curl --location --request GET \"https://postman-echo.com/get?foo1=bar1&foo2=bar2\""; String command = "curl -X GET https://postman-echo.com/get?foo1=bar1&foo2=bar2";
ProcessBuilder processBuilder = new ProcessBuilder(command.replaceAll("\"", "").split(" ")); ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
processBuilder.directory(new File("/home/")); processBuilder.directory(new File("/home/"));
Process process = processBuilder.start(); Process process = processBuilder.start();
@ -40,16 +37,14 @@ public class JavaCurlExamplesUnitTest {
} }
@Test @Test
public void whenRequestGet_thenReturnSuccessResponseCode() throws IOException { public void whenRequestPost_thenCheckIfReturnContent() throws IOException {
String url = "https://postman-echo.com/get?foo1=bar1&foo2=bar2"; String command = "curl -X POST https://postman-echo.com/post --data foo1=bar1&foo2=bar2";
URL urlObj = new URL(url); Process process = Runtime.getRuntime().exec(command);
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
connection.connect();
Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); // Get the POST result
String content = JavaCurlExamples.inputStreamToString(process.getInputStream());
Assert.assertTrue(null != content && !content.isEmpty());
} }
} }