Skip to content

Commit

Permalink
relax on cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
leleuj committed Nov 23, 2023
1 parent b577e23 commit df766ed
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
*/
public class CasOIDCLoginTest extends CasLoginTest {

protected static final String DISSESSION_OAUTH_OIDC = "DISSESSIONOauthOidcServerSupport";

public void run() throws Exception {
startTimer();

Expand All @@ -30,7 +28,7 @@ public void run() throws Exception {
execute();
assertStatus(302);
val loginUrl = getLocation();
val casSessionId = getCookie(DISSESSION_OAUTH_OIDC);
val casSessionId = getCookie(DISSESSION);

_request = get(loginUrl);
execute();
Expand All @@ -40,15 +38,15 @@ public void run() throws Exception {
val callbackUrl = getLocation();
val tgc = getCookie(TGC);

_cookies.put(DISSESSION_OAUTH_OIDC, casSessionId);
_cookies.put(TGC, tgc);
_cookies.put(casSessionId.getLeft(), casSessionId.getRight());
_cookies.put(TGC, tgc.getRight());
_request = get(callbackUrl);
execute();
assertStatus(302);
val authorizeUrl2 = getLocation();

_cookies.put(DISSESSION_OAUTH_OIDC, casSessionId);
_cookies.put(TGC, tgc);
_cookies.put(casSessionId.getLeft(), casSessionId.getRight());
_cookies.put(TGC, tgc.getRight());
_request = get(authorizeUrl2);
execute();
assertStatus(302);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void run() throws Exception {
val tgc = getCookie(TGC);

// call callback
_cookies.put(JSESSIONID, casSessionId);
_cookies.put(TGC, tgc);
_cookies.put(casSessionId.getLeft(), casSessionId.getRight());
_cookies.put(TGC, tgc.getRight());
_request = get(samlCallbackUrl);
execute();
assertStatus(200);
Expand All @@ -53,14 +53,14 @@ public void run() throws Exception {

_data.put("SAMLResponse", samlResponse);
_data.put("RelayState", getRelayState());
_cookies.put("JSESSIONID", pac4jSessionId);
_cookies.put("JSESSIONID", pac4jSessionId.getRight());
_request = post(pac4jCallbackUrl);
execute();
assertStatus(303);
val protectedUrl = getLocation();
val newPac4jSessionId = getCookie(JSESSIONID);

_cookies.put(JSESSIONID, newPac4jSessionId);
_cookies.put(JSESSIONID, newPac4jSessionId.getRight());
_request = get(protectedUrl);
execute();
assertStatus(200);
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/casinthecloud/simpleperf/test/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.val;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.pac4j.core.util.CommonHelper;

import java.net.URI;
Expand Down Expand Up @@ -80,11 +82,13 @@ protected String getLocation() {
return _headers.get("Location").get(0);
}

protected String getCookie(final String name) {
protected Pair<String, String> getCookie(final String name) {
val listHeaders = _headers.get("set-cookie");
for (val header : listHeaders) {
if (header.startsWith(name + "=")) {
return between(header, name + "=", ";");
if (header.startsWith(name)) {
val key = before(header, "=");
val value = between(header, "=", ";");
return new ImmutablePair<String, String>(key, value);
}
}
return null;
Expand Down Expand Up @@ -154,6 +158,10 @@ protected String after(final String s1, final String s2) {
return StringUtils.substringAfter(s1, s2);
}

protected String before(final String s1, final String s2) {
return StringUtils.substringBefore(s1, s2);
}

protected String urlEncode(final String s) {
return URLEncoder.encode(s, StandardCharsets.UTF_8);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public abstract class CasTest extends WebTest {

protected static final String TGC = "TGC";
protected static final String DISSESSION = "DISSESSION";

protected void executePostCasCredentials(final String casLoginUrl) throws Exception {
val webflow = between(_body, "name=\"execution\" value=\"", "\"/>");
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/casinthecloud/simpleperf/test/RandomTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.casinthecloud.simpleperf.test;

import lombok.val;

import java.net.http.HttpClient;
import java.util.concurrent.atomic.AtomicLong;

/**
* Randomly run test.
*
* @author Jerome LELEU
* @since 1.0.0
*/
public class RandomTest extends BaseTest {

private BaseTest[] tests;

public RandomTest(final BaseTest... tests) {
this.tests = tests;
}

@Override
public void run() throws Exception {
val r = random(tests.length);

tests[r].run();
}

public void setClient(final HttpClient client) {
for (val test : tests) {
test.setClient(client);
}
}

public void setTime(final AtomicLong time) {
for (val test : tests) {
test.setTime(time);
}
}
}

0 comments on commit df766ed

Please sign in to comment.