refactor(core): share the token decode in Pagination (#6431)

tokenStartsWith and decodeCursor each decoded the base64url token and
caught the IllegalArgumentException of an unreadable one. Both now use a
private decodeToken that returns the decoded cursor, or null for an absent
or unreadable token. Behaviour is unchanged; PaginationTest now covers
tokenStartsWith directly.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-10-24 13:36:01 -04:00
1 parent e1796b33b1
commit 8ef9e1ddbf
2 files changed
+36 -15

No files matched your search

@@ -11,9 +11,11 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PaginationTest {
@@ -93,6 +95,25 @@ class PaginationTest {
assertEquals("Invalid Marker.", e.getMessage());
}
@Test
void tokenStartsWith_matchesTheDecodedCursorPrefix() {
String token = page(1, null, "ns").nextToken();
assertTrue(Pagination.tokenStartsWith(token, "ns:"));
assertFalse(Pagination.tokenStartsWith(token, "other:"));
}
@Test
void tokenStartsWith_isFalseForATokenThatDoesNotDecode() {
assertFalse(Pagination.tokenStartsWith("!!!", ""));
}
@Test
void tokenStartsWith_isFalseForAnAbsentToken() {
assertFalse(Pagination.tokenStartsWith(null, ""));
assertFalse(Pagination.tokenStartsWith("", ""));
}
private static PaginatedResult<String> page(int limit, String token, String namespace) {
return Pagination.paginate(ITEMS, Function.identity(), limit, token, namespace, INVALID_TOKEN);
}