package com.perforce.hws.core.test; import org.apache.commons.codec.binary.Base64; import spark.utils.IOUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; public class SimpleTestSpark { public static SimpleResponse get(URL url, String auth) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setRequestProperty("Authorization", auth); int count = 10; while (count > 0) { try { connection.connect(); break; } catch (IOException e) { try { Thread.sleep(20); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } count--; } String body = IOUtils.toString(connection.getInputStream()); SimpleResponse response = new SimpleResponse(connection.getResponseCode(), body); connection.disconnect(); return response; } public static SimpleResponse login(URL url, String username, String password) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); String authString = username + ":" + password; String encoded = Base64.encodeBase64String(authString.getBytes()); connection.setRequestProperty("Authorization", "Basic " + encoded); connection.setRequestMethod("POST"); connection.setDoOutput(true); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer body = new StringBuffer(); while ((inputLine = in.readLine()) != null) { body.append(inputLine); } in.close(); SimpleResponse response = new SimpleResponse(connection.getResponseCode(), body.toString()); connection.disconnect(); return response; } public static SimpleResponse post(URL url, String auth, String json) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Authorization", auth); OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream()); wr.write(json); wr.close(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuffer body = new StringBuffer(); String inputLine; while ((inputLine = in.readLine()) != null) { body.append(inputLine); } in.close(); SimpleResponse response = new SimpleResponse(connection.getResponseCode(), body.toString()); connection.disconnect(); return response; } public static SimpleResponse patch(URL url, String auth, String json) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Authorization", auth); OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream()); wr.write(json); wr.close(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuffer body = new StringBuffer(); String inputLine; while ((inputLine = in.readLine()) != null) { body.append(inputLine); } in.close(); SimpleResponse response = new SimpleResponse(connection.getResponseCode(), body.toString()); connection.disconnect(); return response; } public static SimpleResponse delete(URL url, String auth) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("DELETE"); connection.setDoOutput(true); connection.setRequestProperty("Authorization", auth); int count = 10; while (count > 0) { try { connection.connect(); break; } catch (IOException e) { try { Thread.sleep(20); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } count--; } String body = IOUtils.toString(connection.getInputStream()); SimpleResponse response = new SimpleResponse(connection.getResponseCode(), body); connection.disconnect(); return response; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 20138 | Paul Allen | Refactor module :api to :core | ||
//guest/paul_allen/hws/dev/api/src/test/java/com/perforce/hws/api/test/SimpleTestSpark.java | |||||
#3 | 20129 | Paul Allen | User DELETE test. | ||
#2 | 20104 | Paul Allen | User functional test for: POST, PUT, PATCH | ||
#1 | 20095 | Paul Allen |
Functional test API with Perforce Server and Spark Server. - Basic User/Login test. |