1 package club.wpia.gigi.testUtils;
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.net.HttpURLConnection;
7 import java.net.URLConnection;
8 import java.net.URLEncoder;
9 import java.security.GeneralSecurityException;
11 import club.wpia.gigi.GigiApiException;
12 import club.wpia.gigi.dbObjects.User;
15 * Superclass for testsuites in a scenario where there is an registered member,
16 * who is already logged on.
18 public abstract class ClientTest extends ManagedTest {
21 * Email of the member.
23 protected String email = createUniqueName() + "@example.org";
28 protected int id = createVerifiedUser("a", "b", email, TEST_PASSWORD);
31 * {@link User} object of the member
33 protected User u = User.getById(id);
36 * Session cookie of the member.
38 protected String cookie;
42 cookie = login(email, TEST_PASSWORD);
43 } catch (IOException e) {
48 public HttpURLConnection post(String path, String query) throws IOException {
49 return post(path, query, 0);
52 public HttpURLConnection post(String path, String query, int formIndex) throws IOException {
53 String server = getServerName();
54 if (loginCertificate != null) {
55 server = getSecureServerName();
57 URLConnection uc = new URL("https://" + server + path).openConnection();
58 authenticate((HttpURLConnection) uc);
59 String csrf = getCSRF(uc, formIndex);
61 uc = new URL("https://" + server + path).openConnection();
62 authenticate((HttpURLConnection) uc);
64 OutputStream os = uc.getOutputStream();
65 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
69 return (HttpURLConnection) uc;
72 public HttpURLConnection get(String path) throws IOException {
73 String server = getServerName();
74 if (loginCertificate != null) {
75 server = getSecureServerName();
77 URLConnection uc = new URL("https://" + server + path).openConnection();
78 authenticate((HttpURLConnection) uc);
79 return (HttpURLConnection) uc;
82 protected void authenticate(HttpURLConnection uc) throws IOException {
83 uc.addRequestProperty("Cookie", cookie);
84 if (loginCertificate != null) {
86 authenticateClientCert(loginPrivateKey, loginCertificate.cert(), uc);
87 } catch (GeneralSecurityException | GigiApiException e) {
88 throw new IOException(e);