33import java .io .File ;
44import java .io .IOException ;
55import java .io .InputStream ;
6- import java .net .HttpURLConnection ;
7- import java .net .URL ;
86
97import org .junit .Assert ;
108import org .junit .Test ;
119
12- public class JavaCurlExamplesUnitTest {
13-
10+ public class JavaCurlExamplesLiveTest {
1411
1512 @ Test
1613 public void givenCommand_whenCalled_thenProduceZeroExitCode () throws IOException {
17- String command = "curl --location --request GET \" https://postman-echo.com/get?foo1=bar1&foo2=bar2\" " ;
18- ProcessBuilder processBuilder = new ProcessBuilder (command .replaceAll ( " \" " , "" ). split (" " ));
14+ String command = "curl -X GET https://postman-echo.com/get?foo1=bar1&foo2=bar2" ;
15+ ProcessBuilder processBuilder = new ProcessBuilder (command .split (" " ));
1916 processBuilder .directory (new File ("/home/" ));
2017 Process process = processBuilder .start ();
2118 InputStream inputStream = process .getInputStream ();
@@ -28,8 +25,8 @@ public void givenCommand_whenCalled_thenProduceZeroExitCode() throws IOException
2825
2926 @ Test
3027 public void givenNewCommands_whenCalled_thenCheckIfIsAlive () throws IOException {
31- String command = "curl --location --request GET \" https://postman-echo.com/get?foo1=bar1&foo2=bar2\" " ;
32- ProcessBuilder processBuilder = new ProcessBuilder (command .replaceAll ( " \" " , "" ). split (" " ));
28+ String command = "curl -X GET https://postman-echo.com/get?foo1=bar1&foo2=bar2" ;
29+ ProcessBuilder processBuilder = new ProcessBuilder (command .split (" " ));
3330 processBuilder .directory (new File ("/home/" ));
3431 Process process = processBuilder .start ();
3532
@@ -40,16 +37,14 @@ public void givenNewCommands_whenCalled_thenCheckIfIsAlive() throws IOException
4037 }
4138
4239 @ Test
43- public void whenRequestGet_thenReturnSuccessResponseCode () throws IOException {
44- String url = "https://postman-echo.com/get?foo1=bar1&foo2=bar2" ;
45- URL urlObj = new URL (url );
46- HttpURLConnection connection = (HttpURLConnection ) urlObj .openConnection ();
47- connection .setDoOutput (true );
48- connection .setInstanceFollowRedirects (false );
49- connection .setRequestMethod ("GET" );
50- connection .connect ();
40+ public void whenRequestPost_thenCheckIfReturnContent () throws IOException {
41+ String command = "curl -X POST https://postman-echo.com/post --data foo1=bar1&foo2=bar2" ;
42+ Process process = Runtime .getRuntime ().exec (command );
5143
52- Assert .assertEquals (HttpURLConnection .HTTP_OK , connection .getResponseCode ());
44+ // Get the POST result
45+ String content = JavaCurlExamples .inputStreamToString (process .getInputStream ());
46+
47+ Assert .assertTrue (null != content && !content .isEmpty ());
5348 }
5449
5550}
0 commit comments