Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(core): share the token decode in Pagination (#6431)
Kenshin Okinaka8ef9e1ddbf28
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java b/src/main/java/io/github/hectorvent/floci/core/common/Pagination.javaindex 564d8cae14..a9fea3d89c 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java@@ -117,14 +117,8 @@ public final class Pagination {* need to tell a token they issued under another scope from one that is not theirs at all.*/public static boolean tokenStartsWith(String token, String prefix) {- if (token == null || token.isEmpty()) {- return false;- }- try {- return new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8).startsWith(prefix);- } catch (IllegalArgumentException e) {- return false;- }+ String decoded = decodeToken(token);+ return decoded != null && decoded.startsWith(prefix);}private static String encodeToken(String cursor) {@@ -136,15 +130,21 @@ public final class Pagination {if (token == null || token.isEmpty()) {return null;}- String decoded;- try {- decoded = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);- } catch (IllegalArgumentException e) {- throw invalidToken.apply(token);- }- if (!decoded.startsWith(prefix)) {+ String decoded = decodeToken(token);+ if (decoded == null || !decoded.startsWith(prefix)) {throw invalidToken.apply(token);}return decoded.substring(prefix.length());}++ private static String decodeToken(String token) {+ if (token == null || token.isEmpty()) {+ return null;+ }+ try {+ return new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);+ } catch (IllegalArgumentException e) {+ return null;+ }+ }}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.javaindex 724d7d0e49..7611732f3f 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java@@ -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);}