From 1c7e27416fbfffcacf7b6f9a8765e9dd9671c2b7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Sat, 7 May 2016 10:24:21 +0200 Subject: [PATCH] upd: refactor tests to use convenience get-stuff --- tests/org/cacert/gigi/LoginTest.java | 3 +- tests/org/cacert/gigi/TestLanguage.java | 9 ++-- .../org/cacert/gigi/TestSecurityHeaders.java | 7 ++- .../pages/admin/TestSEAdminPageDetails.java | 4 +- .../TestSEAdminPageUserDomainSearch.java | 48 ++----------------- .../admin/TestSEAdminPageUserMailSearch.java | 14 +----- .../gigi/pages/orga/TestOrgaManagement.java | 18 +++---- .../cacert/gigi/pages/wot/TestAssurance.java | 18 ++----- tests/org/cacert/gigi/pages/wot/TestTTP.java | 9 ++-- .../cacert/gigi/pages/wot/TestTTPAdmin.java | 15 +++--- tests/org/cacert/gigi/ping/TestDNS.java | 9 ++-- tests/org/cacert/gigi/ping/TestHTTP.java | 12 ++--- tests/org/cacert/gigi/ping/TestSSL.java | 10 ++-- .../cacert/gigi/testUtils/ManagedTest.java | 2 +- tests/org/cacert/gigi/testUtils/PingTest.java | 16 +++---- 15 files changed, 53 insertions(+), 141 deletions(-) diff --git a/tests/org/cacert/gigi/LoginTest.java b/tests/org/cacert/gigi/LoginTest.java index f02f0ad0..29456c7b 100644 --- a/tests/org/cacert/gigi/LoginTest.java +++ b/tests/org/cacert/gigi/LoginTest.java @@ -3,7 +3,6 @@ package org.cacert.gigi; import static org.junit.Assert.*; import java.io.IOException; -import java.net.URL; import org.cacert.gigi.testUtils.ManagedTest; import org.junit.Test; @@ -43,7 +42,7 @@ public class LoginTest extends ManagedTest { } private void logout(String cookie) throws IOException { - cookie(new URL("https://" + getServerName() + "/logout").openConnection(), cookie).getHeaderField("Location"); + get(cookie, "/logout").getHeaderField("Location"); } } diff --git a/tests/org/cacert/gigi/TestLanguage.java b/tests/org/cacert/gigi/TestLanguage.java index 1cac1452..55a3d126 100644 --- a/tests/org/cacert/gigi/TestLanguage.java +++ b/tests/org/cacert/gigi/TestLanguage.java @@ -4,7 +4,6 @@ import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import java.io.IOException; -import java.net.URL; import java.util.Locale; import org.cacert.gigi.dbObjects.User; @@ -51,13 +50,13 @@ public class TestLanguage extends ManagedTest { @Test public void testSelectStandard() throws IOException { - String content = IOUtils.readURL(new URL("https://" + getServerName() + "/").openConnection()); + String content = IOUtils.readURL(get("cook", "/")); assertThat(content, containsString("Translations")); } @Test public void testSelectGerman() throws IOException { - String content = IOUtils.readURL(new URL("https://" + getServerName() + "/?lang=de").openConnection()); + String content = IOUtils.readURL(get("", "/?lang=de")); assertThat(content, containsString(Language.getInstance(Locale.GERMAN).getTranslation("Translations"))); } @@ -66,7 +65,7 @@ public class TestLanguage extends ManagedTest { setAcceptLanguage("de,en"); User u = User.getById(createVerifiedUser("fname", "lname", createUniqueName() + "@example.org", TEST_PASSWORD)); String cookie = login(u.getEmail(), TEST_PASSWORD); - String content = IOUtils.readURL(cookie(new URL("https://" + getServerName() + "/").openConnection(), cookie)); + String content = IOUtils.readURL(get(cookie, "/")); assertThat(content, containsString(Language.getInstance(Locale.GERMAN).getTranslation("Translations"))); } @@ -75,7 +74,7 @@ public class TestLanguage extends ManagedTest { setAcceptLanguage("fr,de,en"); User u = User.getById(createVerifiedUser("fname", "lname", createUniqueName() + "@example.org", TEST_PASSWORD)); String cookie = login(u.getEmail(), TEST_PASSWORD); - String content = IOUtils.readURL(cookie(new URL("https://" + getServerName() + "/").openConnection(), cookie)); + String content = IOUtils.readURL(get(cookie, "/")); assertThat(content, containsString(Language.getInstance(Locale.FRENCH).getTranslation("Translations"))); } } diff --git a/tests/org/cacert/gigi/TestSecurityHeaders.java b/tests/org/cacert/gigi/TestSecurityHeaders.java index 16f4f3a0..8149d322 100644 --- a/tests/org/cacert/gigi/TestSecurityHeaders.java +++ b/tests/org/cacert/gigi/TestSecurityHeaders.java @@ -4,7 +4,6 @@ import static org.junit.Assert.*; import java.io.IOException; import java.net.HttpURLConnection; -import java.net.URL; import org.cacert.gigi.testUtils.ManagedTest; import org.junit.Test; @@ -13,17 +12,17 @@ public class TestSecurityHeaders extends ManagedTest { @Test public void testSTS() throws IOException { - HttpURLConnection uc = (HttpURLConnection) new URL("https://" + getServerName()).openConnection(); + HttpURLConnection uc = get(null, "/"); assertNotNull(uc.getHeaderField("Strict-Transport-Security")); } public void testCSP() throws IOException { - HttpURLConnection uc = (HttpURLConnection) new URL("https://" + getServerName()).openConnection(); + HttpURLConnection uc = get(null, "/"); assertNotNull(uc.getHeaderField("Content-Security-Policy")); } public void testAllowOrigin() throws IOException { - HttpURLConnection uc = (HttpURLConnection) new URL("https://" + getServerName()).openConnection(); + HttpURLConnection uc = get(null, "/"); assertNotNull(uc.getHeaderField("Access-Control-Allow-Origin")); } diff --git a/tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java b/tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java index f27a26a4..1563adda 100644 --- a/tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java +++ b/tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java @@ -5,7 +5,6 @@ import static org.junit.Assert.*; import java.io.IOException; import java.net.MalformedURLException; -import java.net.URL; import java.net.URLConnection; import java.util.Locale; import java.util.regex.Matcher; @@ -37,8 +36,7 @@ public class TestSEAdminPageDetails extends ClientTest { String fname = "Först"; String lname = "Secönd"; int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD); - URLConnection uc = new URL("https://" + getServerName() + SupportUserDetailsPage.PATH + id).openConnection(); - uc.addRequestProperty("Cookie", cookie); + URLConnection uc = get(SupportUserDetailsPage.PATH + id); uc.setDoOutput(true); String res = IOUtils.readURL(uc); assertThat(res, containsString("")); diff --git a/tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java b/tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java index 966adc17..4337e332 100644 --- a/tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java +++ b/tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserDomainSearch.java @@ -4,10 +4,8 @@ import static org.junit.Assert.*; import static org.junit.Assume.*; import java.io.IOException; -import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; -import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; @@ -37,17 +35,8 @@ public class TestSEAdminPageUserDomainSearch extends ClientTest { User user = User.getById(id); String domainName = createUniqueName() + ".org"; new Domain(user, user, domainName); - URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); - uc.addRequestProperty("Cookie", cookie); - String csrf = getCSRF(uc, 0); + URLConnection uc = post(FindDomainPage.PATH, "process&domain=" + URLEncoder.encode(domainName, "UTF-8")); - uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); - uc.addRequestProperty("Cookie", cookie); - uc.setDoOutput(true); - OutputStream os = uc.getOutputStream(); - os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" // - + "process&domain=" + URLEncoder.encode(domainName, "UTF-8")).getBytes("UTF-8")); - os.flush(); assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location")); } @@ -58,33 +47,13 @@ public class TestSEAdminPageUserDomainSearch extends ClientTest { User user = User.getById(id); String domainName = createUniqueName() + ".org"; Domain d = new Domain(user, user, domainName); - URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); - uc.addRequestProperty("Cookie", cookie); - String csrf = getCSRF(uc, 0); - - uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); - uc.addRequestProperty("Cookie", cookie); - uc.setDoOutput(true); - OutputStream os = uc.getOutputStream(); - os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" // - + "process&domain=#" + d.getId()).getBytes("UTF-8")); - os.flush(); + URLConnection uc = post(FindDomainPage.PATH, "process&domain=#" + d.getId()); assertEquals("https://" + ServerConstants.getWwwHostNamePortSecure() + SupportUserDetailsPage.PATH + id, uc.getHeaderField("Location")); } @Test public void testDomainSearchNonExist() throws MalformedURLException, UnsupportedEncodingException, IOException, GigiApiException { - URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); - uc.addRequestProperty("Cookie", cookie); - String csrf = getCSRF(uc, 0); - - uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); - uc.addRequestProperty("Cookie", cookie); - uc.setDoOutput(true); - OutputStream os = uc.getOutputStream(); - os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" // - + "process&domain=" + URLEncoder.encode(createUniqueName() + ".de", "UTF-8")).getBytes("UTF-8")); - os.flush(); + URLConnection uc = post(FindDomainPage.PATH, "process&domain=" + URLEncoder.encode(createUniqueName() + ".de", "UTF-8")); assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc))); } @@ -102,16 +71,7 @@ public class TestSEAdminPageUserDomainSearch extends ClientTest { found = true; } assumeTrue(found); - URLConnection uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); - uc.addRequestProperty("Cookie", cookie); - String csrf = getCSRF(uc, 0); - uc = new URL("https://" + getServerName() + FindDomainPage.PATH).openConnection(); - uc.addRequestProperty("Cookie", cookie); - uc.setDoOutput(true); - OutputStream os = uc.getOutputStream(); - os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" // - + "process&domain=#" + id).getBytes("UTF-8")); - os.flush(); + URLConnection uc = post(FindDomainPage.PATH, "process&domain=#" + id); assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc))); } } diff --git a/tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserMailSearch.java b/tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserMailSearch.java index 95b9ec74..70e5bd4f 100644 --- a/tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserMailSearch.java +++ b/tests/org/cacert/gigi/pages/admin/TestSEAdminPageUserMailSearch.java @@ -4,10 +4,8 @@ import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import java.io.IOException; -import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; -import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; @@ -74,17 +72,7 @@ public class TestSEAdminPageUserMailSearch extends ClientTest { @Test public void testWildcardMailSearchNoRes() throws MalformedURLException, UnsupportedEncodingException, IOException { - URLConnection uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection(); - uc.addRequestProperty("Cookie", cookie); - String csrf = getCSRF(uc, 0); - - uc = new URL("https://" + getServerName() + FindUserPage.PATH).openConnection(); - uc.addRequestProperty("Cookie", cookie); - uc.setDoOutput(true); - OutputStream os = uc.getOutputStream(); - os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" // - + "process&email=" + URLEncoder.encode("%@_humpfelkumpf.org", "UTF-8")).getBytes("UTF-8")); - os.flush(); + URLConnection uc = post(FindUserPage.PATH, "process&email=" + URLEncoder.encode("%@_humpfelkumpf.org", "UTF-8")); assertNotNull(fetchStartErrorMessage(IOUtils.readURL(uc))); } diff --git a/tests/org/cacert/gigi/pages/orga/TestOrgaManagement.java b/tests/org/cacert/gigi/pages/orga/TestOrgaManagement.java index 3ecc7a21..6fe4f182 100644 --- a/tests/org/cacert/gigi/pages/orga/TestOrgaManagement.java +++ b/tests/org/cacert/gigi/pages/orga/TestOrgaManagement.java @@ -5,7 +5,6 @@ import static org.junit.Assert.*; import java.io.IOException; import java.net.HttpURLConnection; -import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.sql.SQLException; @@ -87,28 +86,25 @@ public class TestOrgaManagement extends ClientTest { o1.addAdmin(u2, u, false); String session2 = login(u2.getEmail(), TEST_PASSWORD); - URLConnection uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection(); - uc.addRequestProperty("Cookie", session2); + URLConnection uc = get(session2, ViewOrgPage.DEFAULT_PATH); assertEquals(403, ((HttpURLConnection) uc).getResponseCode()); - uc = new URL("https://" + getServerName() + MyDetails.PATH).openConnection(); - uc.addRequestProperty("Cookie", session2); + uc = get(session2, MyDetails.PATH); String content = IOUtils.readURL(uc); assertThat(content, containsString("name21")); assertThat(content, not(containsString("name12"))); - uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), session2); + uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()); assertEquals(403, ((HttpURLConnection) uc).getResponseCode()); - uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), session2); + uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()); assertEquals(403, ((HttpURLConnection) uc).getResponseCode()); - uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection(); - uc.addRequestProperty("Cookie", cookie); + uc = get(ViewOrgPage.DEFAULT_PATH); content = IOUtils.readURL(uc); assertThat(content, containsString("name21")); assertThat(content, containsString("name12")); - uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), cookie); + uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()); assertEquals(200, ((HttpURLConnection) uc).getResponseCode()); - uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), cookie); + uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()); assertEquals(200, ((HttpURLConnection) uc).getResponseCode()); o1.delete(); o2.delete(); diff --git a/tests/org/cacert/gigi/pages/wot/TestAssurance.java b/tests/org/cacert/gigi/pages/wot/TestAssurance.java index 58a791f3..ffc9ea57 100644 --- a/tests/org/cacert/gigi/pages/wot/TestAssurance.java +++ b/tests/org/cacert/gigi/pages/wot/TestAssurance.java @@ -7,7 +7,6 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; -import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.sql.SQLException; @@ -64,10 +63,8 @@ public class TestAssurance extends ManagedTest { } private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException { - URL u = new URL("https://" + getServerName() + AssurePage.PATH); - URLConnection uc = u.openConnection(); + URLConnection uc = get(cookie, AssurePage.PATH); uc.setDoOutput(true); - uc.addRequestProperty("Cookie", cookie); uc.getOutputStream().write(("search&" + query).getBytes("UTF-8")); uc.getOutputStream().flush(); @@ -186,8 +183,7 @@ public class TestAssurance extends ManagedTest { String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10"); assertNull(error); String cookie = login(assureeM, TEST_PASSWORD); - URLConnection url = new URL("https://" + getServerName() + MyPoints.PATH).openConnection(); - url.setRequestProperty("Cookie", cookie); + URLConnection url = get(cookie, MyPoints.PATH); String resp = IOUtils.readURL(url); resp = resp.split(Pattern.quote(""))[0]; assertThat(resp, containsString(uniqueLoc)); @@ -199,8 +195,7 @@ public class TestAssurance extends ManagedTest { String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10"); assertNull(error); String cookie = login(assurerM, TEST_PASSWORD); - URLConnection url = new URL("https://" + getServerName() + MyPoints.PATH).openConnection(); - url.setRequestProperty("Cookie", cookie); + URLConnection url = get(cookie, MyPoints.PATH); String resp = IOUtils.readURL(url); resp = resp.split(Pattern.quote(""))[1]; assertThat(resp, containsString(uniqueLoc)); @@ -219,15 +214,12 @@ public class TestAssurance extends ManagedTest { } public static URLConnection buildupAssureFormConnection(String cookie, String email, boolean doCSRF) throws MalformedURLException, IOException { - URL u = new URL("https://" + getServerName() + AssurePage.PATH); - URLConnection uc = u.openConnection(); - uc.addRequestProperty("Cookie", cookie); + URLConnection uc = get(cookie, AssurePage.PATH); uc.setDoOutput(true); uc.getOutputStream().write(("email=" + URLEncoder.encode(email, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8")); String csrf = getCSRF(uc); - uc = u.openConnection(); - uc.addRequestProperty("Cookie", cookie); + uc = get(cookie, AssurePage.PATH); uc.setDoOutput(true); if (doCSRF) { uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8")); diff --git a/tests/org/cacert/gigi/pages/wot/TestTTP.java b/tests/org/cacert/gigi/pages/wot/TestTTP.java index 7c8884ba..34b0ca6d 100644 --- a/tests/org/cacert/gigi/pages/wot/TestTTP.java +++ b/tests/org/cacert/gigi/pages/wot/TestTTP.java @@ -4,7 +4,6 @@ import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import java.io.IOException; -import java.net.URL; import org.cacert.gigi.GigiApiException; import org.cacert.gigi.dbObjects.Group; @@ -16,17 +15,15 @@ import org.junit.Test; public class TestTTP extends ClientTest { - URL ttpPage = new URL("https://" + getServerName() + RequestTTPPage.PATH); - public TestTTP() throws IOException {} @Test public void testTTPApply() throws IOException { - String ttp = IOUtils.readURL(cookie(ttpPage.openConnection(), cookie)); + String ttp = IOUtils.readURL(get(RequestTTPPage.PATH)); assertThat(ttp, containsString("dns\\s*success"); assertTrue(newcontent, !successDNS ^ pat.matcher(newcontent).find()); pat = Pattern.compile("email\\s*success"); diff --git a/tests/org/cacert/gigi/ping/TestHTTP.java b/tests/org/cacert/gigi/ping/TestHTTP.java index da9892b4..31fecd94 100644 --- a/tests/org/cacert/gigi/ping/TestHTTP.java +++ b/tests/org/cacert/gigi/ping/TestHTTP.java @@ -18,7 +18,6 @@ import org.cacert.gigi.GigiApiException; import org.cacert.gigi.dbObjects.Domain; import org.cacert.gigi.dbObjects.DomainPingConfiguration; import org.cacert.gigi.dbObjects.DomainPingType; -import org.cacert.gigi.pages.account.domain.DomainOverview; import org.cacert.gigi.testUtils.IOUtils; import org.cacert.gigi.testUtils.PingTest; import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail; @@ -58,8 +57,7 @@ public class TestHTTP extends PingTest { String test = getTestProps().getProperty("domain.http"); assumeNotNull(test); - URL u = new URL("https://" + getServerName() + DomainOverview.PATH); - Matcher m = initailizeDomainForm(u); + Matcher m = initailizeDomainForm(); updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http"); String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + // @@ -69,7 +67,7 @@ public class TestHTTP extends PingTest { "&ssl-type-2=direct&ssl-port-2=" + // "&ssl-type-3=direct&ssl-port-3=" + // "&adddomain&csrf=" + csrf; - URL u2 = sendDomainForm(u, content); + String p2 = sendDomainForm(content); TestMail mail = getMailReciever().receive(); if (emailVariant == 0) { @@ -77,14 +75,14 @@ public class TestHTTP extends PingTest { } waitForPings(2); - String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie)); + String newcontent = IOUtils.readURL(get(p2)); Pattern pat = Pattern.compile("http\\s*success"); assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find()); pat = Pattern.compile("email\\s*success"); assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find()); if (successHTTP) { // give it a second try - int id = Integer.parseInt(u2.toString().replaceFirst("^.*/([0-9]+)$", "$1")); + int id = Integer.parseInt(p2.replaceFirst("^.*/([0-9]+)$", "$1")); Domain d = Domain.getById(id); DomainPingConfiguration dpc = null; for (DomainPingConfiguration conf : d.getConfiguredPings()) { @@ -96,7 +94,7 @@ public class TestHTTP extends PingTest { if (dpc == null) { fail("Http config not found"); } - String res = executeBasicWebInteraction(cookie, u2.getPath(), "configId=" + dpc.getId()); + String res = executeBasicWebInteraction(cookie, p2, "configId=" + dpc.getId()); assertThat(res, containsString("only allowed after")); } } diff --git a/tests/org/cacert/gigi/ping/TestSSL.java b/tests/org/cacert/gigi/ping/TestSSL.java index 82d1c820..62e652aa 100644 --- a/tests/org/cacert/gigi/ping/TestSSL.java +++ b/tests/org/cacert/gigi/ping/TestSSL.java @@ -6,7 +6,6 @@ import static org.junit.Assume.*; import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.Socket; -import java.net.URL; import java.net.URLEncoder; import java.security.GeneralSecurityException; import java.security.KeyManagementException; @@ -42,7 +41,6 @@ import org.cacert.gigi.dbObjects.Certificate.CSRType; import org.cacert.gigi.dbObjects.CertificateProfile; import org.cacert.gigi.dbObjects.Digest; import org.cacert.gigi.dbObjects.User; -import org.cacert.gigi.pages.account.domain.DomainOverview; import org.cacert.gigi.testUtils.IOUtils; import org.cacert.gigi.testUtils.PingTest; import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail; @@ -154,9 +152,7 @@ public class TestSSL extends PingTest { private void testEmailAndSSL(int sslVariant, int emailVariant, boolean successMail) throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException { String test = getTestProps().getProperty("domain.local"); assumeNotNull(test); - URL u = new URL("https://" + getServerName() + DomainOverview.PATH); - - Matcher m = initailizeDomainForm(u); + Matcher m = initailizeDomainForm(); String value = m.group(2); if (self) { @@ -182,7 +178,7 @@ public class TestSSL extends PingTest { "&ssl-type-2=direct&ssl-port-2=" + // "&ssl-type-3=direct&ssl-port-3=" + // "&adddomain&csrf=" + csrf; - URL u2 = sendDomainForm(u, content); + String p2 = sendDomainForm(content); boolean firstSucceeds = sslVariant != 0 && sslVariant != 2; AsyncTask ass = new AsyncTask() { @@ -206,7 +202,7 @@ public class TestSSL extends PingTest { } waitForPings(3); - String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie)); + String newcontent = IOUtils.readURL(get(p2)); Pattern pat = Pattern.compile("ssl\\s*success"); Matcher matcher = pat.matcher(newcontent); assertTrue(newcontent, firstSucceeds ^ matcher.find()); diff --git a/tests/org/cacert/gigi/testUtils/ManagedTest.java b/tests/org/cacert/gigi/testUtils/ManagedTest.java index 96b4e61b..098c257f 100644 --- a/tests/org/cacert/gigi/testUtils/ManagedTest.java +++ b/tests/org/cacert/gigi/testUtils/ManagedTest.java @@ -474,7 +474,7 @@ public class ManagedTest extends ConfiguredTest { return (HttpURLConnection) uc; } - public HttpURLConnection get(String cookie, String path) throws IOException { + public static HttpURLConnection get(String cookie, String path) throws IOException { URLConnection uc = new URL("https://" + getServerName() + path).openConnection(); uc.addRequestProperty("Cookie", cookie); return (HttpURLConnection) uc; diff --git a/tests/org/cacert/gigi/testUtils/PingTest.java b/tests/org/cacert/gigi/testUtils/PingTest.java index 31f45c99..0d15f2e8 100644 --- a/tests/org/cacert/gigi/testUtils/PingTest.java +++ b/tests/org/cacert/gigi/testUtils/PingTest.java @@ -46,9 +46,8 @@ public abstract class PingTest extends ClientTest { } } - protected URL sendDomainForm(URL u, String content) throws IOException, MalformedURLException { - URLConnection openConnection = u.openConnection(); - openConnection.setRequestProperty("Cookie", cookie); + protected String sendDomainForm(String content) throws IOException, MalformedURLException { + URLConnection openConnection = get(DomainOverview.PATH); openConnection.setDoOutput(true); openConnection.getOutputStream().write(content.getBytes("UTF-8")); openConnection.getHeaderField("Location"); @@ -56,20 +55,17 @@ public abstract class PingTest extends ClientTest { throw new Error(IOUtils.readURL(openConnection)); } - String newcontent = IOUtils.readURL(cookie(u.openConnection(), cookie)); + String newcontent = IOUtils.readURL(get(DomainOverview.PATH)); Pattern dlink = Pattern.compile(DomainOverview.PATH + "([0-9]+)'>"); Matcher m1 = dlink.matcher(newcontent); if ( !m1.find()) { throw new Error(newcontent); } - URL u2 = new URL(u.toString() + m1.group(1)); - return u2; + return DomainOverview.PATH + m1.group(1); } - protected Matcher initailizeDomainForm(URL u) throws IOException, Error { - URLConnection openConnection = u.openConnection(); - openConnection.setRequestProperty("Cookie", cookie); - String content1 = IOUtils.readURL(openConnection); + protected Matcher initailizeDomainForm() throws IOException, Error { + String content1 = IOUtils.readURL(get(DomainOverview.PATH)); csrf = getCSRF(1, content1); Pattern p = Pattern.compile("([A-Za-z0-9]+)._cacert._auth IN TXT ([A-Za-z0-9]+)"); -- 2.39.2