From a7cae2e766f2603dc3d2629607848779985c7060 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Sun, 29 May 2022 09:52:09 +0200 Subject: [PATCH] JAVA-9609: Create manual test for POSTing with HttpURLConnection (#12263) --- .../PostJSONWithHttpURLConnection.java | 46 ---------------- ...stJSONWithHttpURLConnectionManualTest.java | 52 +++++++++++++++++++ 2 files changed, 52 insertions(+), 46 deletions(-) delete mode 100644 core-java-modules/core-java-networking-2/src/main/java/com/baeldung/urlconnection/PostJSONWithHttpURLConnection.java create mode 100644 core-java-modules/core-java-networking-2/src/test/java/com/baeldung/urlconnection/PostJSONWithHttpURLConnectionManualTest.java diff --git a/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/urlconnection/PostJSONWithHttpURLConnection.java b/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/urlconnection/PostJSONWithHttpURLConnection.java deleted file mode 100644 index 38b4a0411d..0000000000 --- a/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/urlconnection/PostJSONWithHttpURLConnection.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.baeldung.urlconnection; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.HttpURLConnection; -import java.net.URL; - -public class PostJSONWithHttpURLConnection { - - public static void main (String []args) throws IOException{ - //Change the URL with any other publicly accessible POST resource, which accepts JSON request body - URL url = new URL ("https://reqres.in/api/users"); - - HttpURLConnection con = (HttpURLConnection)url.openConnection(); - con.setRequestMethod("POST"); - - con.setRequestProperty("Content-Type", "application/json; utf-8"); - con.setRequestProperty("Accept", "application/json"); - - con.setDoOutput(true); - - //JSON String need to be constructed for the specific resource. - //We may construct complex JSON using any third-party JSON libraries such as jackson or org.json - String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}"; - - try(OutputStream os = con.getOutputStream()){ - byte[] input = jsonInputString.getBytes("utf-8"); - os.write(input, 0, input.length); - } - - int code = con.getResponseCode(); - System.out.println(code); - - try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))){ - StringBuilder response = new StringBuilder(); - String responseLine = null; - while ((responseLine = br.readLine()) != null) { - response.append(responseLine.trim()); - } - System.out.println(response.toString()); - } - } - -} diff --git a/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/urlconnection/PostJSONWithHttpURLConnectionManualTest.java b/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/urlconnection/PostJSONWithHttpURLConnectionManualTest.java new file mode 100644 index 0000000000..e1a73a8de9 --- /dev/null +++ b/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/urlconnection/PostJSONWithHttpURLConnectionManualTest.java @@ -0,0 +1,52 @@ +package com.baeldung.urlconnection; + +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PostJSONWithHttpURLConnectionManualTest { + + @Test + public void givenValidURLAndPayload_whenPost_ThenSuccess() throws IOException { + //Change the URL with any other publicly accessible POST resource, which accepts JSON request body + URL url = new URL("https://reqres.in/api/users"); + + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("User-Agent", "AnyAgent"); + + con.setRequestProperty("Content-Type", "application/json"); + con.setRequestProperty("Accept", "application/json"); + + con.setDoOutput(true); + + //JSON String need to be constructed for the specific resource. + //We may construct complex JSON using any third-party JSON libraries such as jackson or org.json + String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}"; + + try (OutputStream os = con.getOutputStream()) { + byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8); + os.write(input, 0, input.length); + } + + assertThat(con.getResponseCode()).isEqualTo(201); + + try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) { + StringBuilder response = new StringBuilder(); + String responseLine = null; + while ((responseLine = br.readLine()) != null) { + response.append(responseLine.trim()); + } + assertThat(response).contains("createdAt"); + } + } + +}