Skip to content

Commit

Permalink
introduced sameSite in standalone cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamhead committed Aug 26, 2023
1 parent 8158e4c commit d39e1e3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ public static final class InternalCookieContainer {
private LatencyContainer maxAge;
private boolean secure;
private boolean httpOnly;
private String sameSite;
private String template;

public CookieContainer toContainer() {
return CookieContainer.newContainer(value, path, domain, maxAge, secure, httpOnly, template);
return CookieContainer.newContainer(value, path, domain, maxAge, secure, httpOnly, sameSite, template);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public final class CookieContainer implements Container {
private LatencyContainer maxAge;
private boolean secure;
private boolean httpOnly;
private String sameSite;
private String template;

public static CookieContainer newContainer(final String text) {
Expand All @@ -28,6 +29,7 @@ public static CookieContainer newContainer(final String text) {
public static CookieContainer newContainer(final String text, final String path,
final String domain, final LatencyContainer maxAge,
final boolean secure, final boolean httpOnly,
final String sameSite,
final String template) {
CookieContainer container = new CookieContainer();
container.value = text;
Expand All @@ -36,6 +38,7 @@ public static CookieContainer newContainer(final String text, final String path,
container.maxAge = maxAge;
container.secure = secure;
container.httpOnly = httpOnly;
container.sameSite = sameSite;
container.template = template;
return container;
}
Expand Down Expand Up @@ -74,6 +77,10 @@ public CookieAttribute[] getOptions() {
options.add(CookieAttribute.maxAge(this.maxAge.getLatency(), this.maxAge.getUnit()));
}

if (this.sameSite != null) {
options.add(CookieAttribute.sameSite(this.sameSite));
}

return options.toArray(new CookieAttribute[options.size()]);
}

Expand All @@ -87,6 +94,7 @@ public String toString() {
.add("max age", maxAge)
.add("secure", secure)
.add("HTTP only", httpOnly)
.add("same site", sameSite)
.add("template", template)
.toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.common.net.HttpHeaders;
import io.netty.handler.codec.http.cookie.ClientCookieDecoder;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.CookieHeaderNames;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -65,10 +67,19 @@ public void should_set_and_recognize_cookie_with_max_age() throws IOException {
assertThat(decodeCookie.maxAge(), is(3600L));
}

private Cookie getCookie(String uri) throws IOException {
@Test
public void should_set_and_recognize_cookie_with_same_site() throws IOException {
runWithConfiguration("cookie.json");
DefaultCookie decodeCookie = getCookie("/cookie-with-same-site");
assertThat(decodeCookie.name(), is("login"));
assertThat(decodeCookie.value(), is("true"));
assertThat(decodeCookie.sameSite(), is(CookieHeaderNames.SameSite.Lax));
}

private DefaultCookie getCookie(String uri) throws IOException {
org.apache.hc.core5.http.HttpResponse response = helper.getResponse(remoteUrl(uri));

String value = response.getFirstHeader(HttpHeaders.SET_COOKIE).getValue();
return ClientCookieDecoder.STRICT.decode(value);
return (DefaultCookie)ClientCookieDecoder.STRICT.decode(value);
}
}
13 changes: 13 additions & 0 deletions moco-runner/src/test/resources/cookie.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,18 @@
}
}
}
},
{
"request": {
"uri": "/cookie-with-same-site"
},
"response": {
"cookies": {
"login": {
"value": "true",
"sameSite": "Lax"
}
}
}
}
]

0 comments on commit d39e1e3

Please sign in to comment.