Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set clusterKey cookie at first request #143

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
Expand Down Expand Up @@ -45,11 +46,14 @@ public static void setIfNeeded(HttpSession session,
Optional<Cookie> clusterKeyCookie = getCookie(request);
if (clusterKeyCookie.isEmpty()) {
String clusterKey = UUID.randomUUID().toString();
session.setAttribute(CurrentKey.COOKIE_NAME, clusterKey);
if (session != null) {
session.setAttribute(CurrentKey.COOKIE_NAME, clusterKey);
}
Cookie cookie = new Cookie(CurrentKey.COOKIE_NAME, clusterKey);
cookieConsumer.accept(cookie);
response.addCookie(cookie);
} else if (session.getAttribute(CurrentKey.COOKIE_NAME) == null) {
} else if (session != null
&& session.getAttribute(CurrentKey.COOKIE_NAME) == null) {
String clusterKey = clusterKeyCookie.get().getValue();
session.setAttribute(CurrentKey.COOKIE_NAME, clusterKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.function.Consumer;

Expand Down Expand Up @@ -72,10 +73,8 @@ protected void doFilter(HttpServletRequest request,
try {
HttpSession session = request.getSession(false);

if (session != null) {
SessionTrackerCookie.setIfNeeded(session, request, response,
cookieConsumer(request));
}
SessionTrackerCookie.setIfNeeded(session, request, response,
cookieConsumer(request));
super.doFilter(request, response, chain);

if (session != null && request.isRequestedSessionIdValid()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,23 @@ void getValue_valueIsReturned() {
assertTrue(value.isPresent());
assertEquals(clusterKey, value.get());
}


@Test
void setIfNeeded_nullCookiesAndSession_cookieIsConfigured() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getCookies()).thenReturn(null);
HttpServletResponse response = mock(HttpServletResponse.class);
@SuppressWarnings("unchecked")
Consumer<Cookie> cookieConsumer = (Consumer<Cookie>) mock(
Consumer.class);

SessionTrackerCookie.setIfNeeded(null, request, response,
cookieConsumer);

verify(cookieConsumer).accept(any());
verify(response).addCookie(any());
}


}