Instantly share code, notes, and snippets.
Last active
July 20, 2018 17:58
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save samtingleff/e3004b92f180a3939f514e0b2f0e0fcb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package st.digitru.cnames.test; | |
import java.util.Arrays; | |
import java.util.Base64; | |
import java.util.List; | |
import org.asynchttpclient.AsyncHttpClient; | |
import org.asynchttpclient.Response; | |
import org.junit.After; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import io.netty.handler.codec.http.cookie.Cookie; | |
import io.netty.handler.codec.http.cookie.DefaultCookie; | |
import st.digitru.error.SerializationException; | |
import st.digitru.model.Identity; | |
import st.digitru.model.Privacy; | |
import st.digitru.serializers.IdentityCookieDeserializer; | |
import st.digitru.serializers.IdentityCookieSerializer; | |
public abstract class BaseCookieTestCase { | |
protected static final String COOKIE_NAME = "DigiTrust.v1.identity"; | |
protected static final String TEST_PRODUCER = "SIx8cS71Eo"; | |
protected IdentityCookieSerializer identitySerializer = new IdentityCookieSerializer(); | |
protected IdentityCookieDeserializer identityDeserializer = new IdentityCookieDeserializer(); | |
protected AsyncHttpClient http; | |
public abstract String producer(); | |
public abstract String url(); | |
@Before | |
public void before() { | |
http = HttpClientRef.get(); | |
} | |
@After | |
public void after() throws Exception { | |
HttpClientRef.close(); | |
} | |
@Test | |
public void newCookie() throws Exception { | |
Response res = http.prepareGet(url()).execute().get(); | |
Assert.assertNotNull(res); | |
Assert.assertEquals(302, res.getStatusCode()); | |
List<Cookie> cookies = res.getCookies(); | |
Assert.assertEquals(1, cookies.size()); | |
assertValidCookie(cookies.get(0)); | |
} | |
@Test | |
public void existingCookie() throws Exception { | |
Identity i1 = new Identity("T4TpFhwaVlc=", 2, TEST_PRODUCER, null, new Privacy(false)); | |
String outboundCookieValue = identitySerializer.serialize(i1); | |
Response res = http.prepareGet(url()) | |
.addCookie(new DefaultCookie( | |
COOKIE_NAME, | |
outboundCookieValue)) | |
.execute() | |
.get(); | |
Assert.assertNotNull(res); | |
Assert.assertEquals(302, res.getStatusCode()); | |
Assert.assertEquals(0, res.getCookies().size()); | |
} | |
@Test | |
public void optout1() throws Exception { | |
// encodeURIComponent(btoa(JSON.stringify({ "id":null,"privacy":{"optout":true}}))); | |
String outboundCookieValue = "eyJpZCI6bnVsbCwicHJpdmFjeSI6eyJvcHRvdXQiOnRydWV9fQ%3D%3D"; | |
Response res = http.prepareGet(url()) | |
.addCookie(new DefaultCookie( | |
COOKIE_NAME, | |
outboundCookieValue)) | |
.execute() | |
.get(); | |
Assert.assertNotNull(res); | |
Assert.assertTrue(Arrays.asList(200, 204, 302).contains(res.getStatusCode())); | |
Assert.assertEquals(0, res.getCookies().size()); | |
} | |
@Test | |
public void optout2() throws Exception { | |
// encodeURIComponent(btoa(JSON.stringify({ "id":"","privacy":{"optout":true}}))); | |
String outboundCookieValue = "eyJpZCI6IiIsInByaXZhY3kiOnsib3B0b3V0Ijp0cnVlfX0%3D"; | |
Response res = http.prepareGet(url()) | |
.addCookie(new DefaultCookie( | |
COOKIE_NAME, | |
outboundCookieValue)) | |
.execute() | |
.get(); | |
Assert.assertNotNull(res); | |
Assert.assertTrue(Arrays.asList(200, 204, 302).contains(res.getStatusCode())); | |
Assert.assertEquals(0, res.getCookies().size()); | |
} | |
@Test | |
public void optout3() throws Exception { | |
// encodeURIComponent(btoa(JSON.stringify({"privacy":{"optout":true}}))); | |
String outboundCookieValue = "eyJwcml2YWN5Ijp7Im9wdG91dCI6dHJ1ZX19"; | |
Response res = http.prepareGet(url()) | |
.addCookie(new DefaultCookie( | |
COOKIE_NAME, | |
outboundCookieValue)) | |
.execute() | |
.get(); | |
Assert.assertNotNull(res); | |
Assert.assertTrue(Arrays.asList(200, 204, 302).contains(res.getStatusCode())); | |
Assert.assertEquals(0, res.getCookies().size()); | |
} | |
@Test | |
public void garbage() throws Exception { | |
String outboundCookieValue = "foobar"; | |
Response res = http.prepareGet(url()) | |
.addCookie(new DefaultCookie( | |
COOKIE_NAME, | |
outboundCookieValue)) | |
.execute() | |
.get(); | |
Assert.assertNotNull(res); | |
Assert.assertEquals(302, res.getStatusCode()); | |
List<Cookie> cookies = res.getCookies(); | |
Assert.assertEquals(1, cookies.size()); | |
Cookie cookie = cookies.get(0); | |
assertValidCookie(cookie); | |
} | |
private void assertValidCookie(Cookie cookie) throws SerializationException { | |
Assert.assertEquals(".digitru.st", cookie.domain()); | |
Assert.assertEquals(COOKIE_NAME, cookie.name()); | |
Identity i = identityDeserializer.deserialize(cookie.value()); | |
Assert.assertNotNull(i); | |
Assert.assertEquals(producer(), i.getProducer()); | |
Assert.assertEquals(null, i.getKeyv()); | |
Assert.assertFalse(i.getPrivacy().getOptout()); | |
Assert.assertEquals(8, Base64.getDecoder().decode(i.getId()).length); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment