Skip to content

Commit

Permalink
added same site with same site enum value
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamhead committed Aug 30, 2023
1 parent d3d6abf commit 27a8438
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public static CookieAttribute sameSite(final String sameSite) {
return new SameSiteAttribute(name);
}

public static CookieAttribute sameSite(final SameSite sameSite) {
CookieHeaderNames.SameSite name = ofSameSite(sameSite.name());
return new SameSiteAttribute(name);
}


public enum SameSite {
STRICT,
LAX,
NONE
}

private static CookieHeaderNames.SameSite ofSameSite(final String name) {
if (name != null) {
for (CookieHeaderNames.SameSite each : CookieHeaderNames.SameSite.class.getEnumConstants()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void should_set_and_recognize_cookie_with_domain() throws Exception {
}

@Test
public void should_set_and_recognize_cookie_with_samesite() throws Exception {
public void should_set_and_recognize_cookie_with_samesite_text() throws Exception {
server.request(eq(cookie("loggedIn"), "true")).response(status(200));
server.response(cookie("loggedIn", "true", sameSite("NONE")), status(302));
running(server, () -> {
Expand All @@ -154,6 +154,18 @@ public void should_set_and_recognize_cookie_with_samesite() throws Exception {
});
}

@Test
public void should_set_and_recognize_cookie_with_samesite_enum() throws Exception {
server.request(eq(cookie("loggedIn"), "true")).response(status(200));
server.response(cookie("loggedIn", "true", sameSite(CookieAttribute.SameSite.NONE)), status(302));
running(server, () -> {
org.apache.hc.core5.http.HttpResponse response = helper.getResponse(root());
String value = response.getFirstHeader(HttpHeaders.SET_COOKIE).getValue();
Cookie decodeCookie = ClientCookieDecoder.STRICT.decode(value);
assertThat(((DefaultCookie)decodeCookie).sameSite(), is(CookieHeaderNames.SameSite.None));
});
}

@Test(expected = IllegalArgumentException.class)
public void should_throw_exception_for_unknown_samesite_value() throws Exception {
sameSite("Unknown");
Expand Down

0 comments on commit 27a8438

Please sign in to comment.