]> WPIA git - gigi.git/blobdiff - tests/club/wpia/gigi/testUtils/ClientTest.java
add: ensure that for support actions certificate login is used
[gigi.git] / tests / club / wpia / gigi / testUtils / ClientTest.java
index 1cd12d2656ab7cc4e3386bf4418b2c29814ed774..17103f7c0a95e6dcd49eb461d27ce5233922ae7a 100644 (file)
@@ -1,8 +1,14 @@
 package club.wpia.gigi.testUtils;
 
 import java.io.IOException;
+import java.io.OutputStream;
 import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLEncoder;
+import java.security.GeneralSecurityException;
 
+import club.wpia.gigi.GigiApiException;
 import club.wpia.gigi.dbObjects.User;
 
 /**
@@ -44,11 +50,44 @@ public abstract class ClientTest extends ManagedTest {
     }
 
     public HttpURLConnection post(String path, String query, int formIndex) throws IOException {
-        return post(cookie, path, query, formIndex);
+        String server = getServerName();
+        if (loginCertificate != null) {
+            server = getSecureServerName();
+        }
+        URLConnection uc = new URL("https://" + server + path).openConnection();
+        authenticate((HttpURLConnection) uc);
+        String csrf = getCSRF(uc, formIndex);
+
+        uc = new URL("https://" + server + path).openConnection();
+        authenticate((HttpURLConnection) uc);
+        uc.setDoOutput(true);
+        OutputStream os = uc.getOutputStream();
+        os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
+                + query//
+        ).getBytes("UTF-8"));
+        os.flush();
+        return (HttpURLConnection) uc;
     }
 
     public HttpURLConnection get(String path) throws IOException {
-        return get(cookie, path);
+        String server = getServerName();
+        if (loginCertificate != null) {
+            server = getSecureServerName();
+        }
+        URLConnection uc = new URL("https://" + server + path).openConnection();
+        authenticate((HttpURLConnection) uc);
+        return (HttpURLConnection) uc;
+    }
+
+    protected void authenticate(HttpURLConnection uc) throws IOException {
+        uc.addRequestProperty("Cookie", cookie);
+        if (loginCertificate != null) {
+            try {
+                authenticateClientCert(loginPrivateKey, loginCertificate.cert(), uc);
+            } catch (GeneralSecurityException | GigiApiException e) {
+                throw new IOException(e);
+            }
+        }
     }
 
 }