From 9014b018fe3a13dba4cad2c9b737890e211da0e1 Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Thu, 14 Nov 2024 15:50:05 +0100 Subject: [PATCH 1/4] Add option "url_safe" to "to_base64" function (#384) - add test - update README --- README.md | 6 ++++- .../org/metafacture/metafix/FixMethod.java | 8 ++++++- .../metafix/MetafixMethodTest.java | 22 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c86640c6..64a20ded 100644 --- a/README.md +++ b/README.md @@ -905,8 +905,12 @@ to_json(""[, pretty: ""][, error_string: ""]) Replaces the value with its Base64 encoding. +Options: + +-`url_safe`: Whether to encode a URL. (Default: `false`) + ```perl -to_base64("") +to_base64(""[, url_safe: ""]) ``` [Example in Playground](https://metafacture.org/playground/?example=to_base64) diff --git a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java index 644e9cc3..50cbcaa3 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java @@ -680,7 +680,13 @@ public void apply(final Metafix metafix, final Record record, final List to_base64 { @Override public void apply(final Metafix metafix, final Record record, final List params, final Map options) { - record.transform(params.get(0), s -> Base64.getEncoder().encodeToString(s.getBytes())); + final boolean urlSafe = getBoolean(options, "url_safe"); + if (!urlSafe) { + record.transform(params.get(0), s -> Base64.getEncoder().encodeToString(s.getBytes())); + } + else { + record.transform(params.get(0), s -> Base64.getUrlEncoder().encodeToString(s.getBytes())); + } } }, to_json { diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java index 8b167257..9f58ecce 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java @@ -4084,6 +4084,28 @@ public void shouldTransformStringToBase64() { ); } + @Test + public void shouldTransformUrlToBase64() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "to_base64('data.title', url_safe:'true')" + ), + i -> { + i.startRecord("1"); + i.startEntity("data"); + i.literal("title", "https://www.youtube.com/watch?v=daLgsPSvD9A"); + i.endEntity(); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().startEntity("data"); + o.get().literal("title", "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g_dj1kYUxnc1BTdkQ5QQ=="); + o.get().endEntity(); + o.get().endRecord(); + } + ); + } + @Test // checkstyle-disable-line JavaNCSS public void shouldCreateVariableFromLiteralValue() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( From fd3967f29c6b13a0ce8fe69e9b0e9b8c553d28d3 Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Thu, 14 Nov 2024 17:23:19 +0100 Subject: [PATCH 2/4] Update README.md Co-authored-by: Jens Wille --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 64a20ded..bbdfa2c2 100644 --- a/README.md +++ b/README.md @@ -907,7 +907,7 @@ Replaces the value with its Base64 encoding. Options: --`url_safe`: Whether to encode a URL. (Default: `false`) +-`url_safe`: Perform URL-safe encoding (uses Base64URL format). (Default: `false`) ```perl to_base64(""[, url_safe: ""]) From e49cdd7239eac2883b1197adf04c051be481d50c Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Thu, 14 Nov 2024 17:23:57 +0100 Subject: [PATCH 3/4] Update metafix/src/main/java/org/metafacture/metafix/FixMethod.java Co-authored-by: Jens Wille --- .../src/main/java/org/metafacture/metafix/FixMethod.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java index 50cbcaa3..273b4d41 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java @@ -681,12 +681,9 @@ public void apply(final Metafix metafix, final Record record, final List @Override public void apply(final Metafix metafix, final Record record, final List params, final Map options) { final boolean urlSafe = getBoolean(options, "url_safe"); - if (!urlSafe) { - record.transform(params.get(0), s -> Base64.getEncoder().encodeToString(s.getBytes())); - } - else { - record.transform(params.get(0), s -> Base64.getUrlEncoder().encodeToString(s.getBytes())); - } + final Base64.Encoder encoder = urlSafe ? Base64.getUrlEncoder() : Base64.getEncoder(); + + record.transform(params.get(0), s -> encoder.encodeToString(s.getBytes())); } }, to_json { From 8fe3804d572f83118badc2498a5dc0e5fc6e9e2a Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Thu, 14 Nov 2024 18:27:46 +0100 Subject: [PATCH 4/4] Add test to encode a URL as Base64 as default Complements 9014b018fe3a13dba4cad2c9b737890e211da0e1. --- .../metafix/MetafixMethodTest.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java index 9f58ecce..d5886ebd 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java @@ -36,6 +36,8 @@ @ExtendWith(MetafixToDo.Extension.class) public class MetafixMethodTest { + private static final String YOUTUBE_URL = "https://www.youtube.com/watch?v=daLgsPSvD9A"; + @Mock private StreamReceiver streamReceiver; @@ -4085,21 +4087,30 @@ public void shouldTransformStringToBase64() { } @Test - public void shouldTransformUrlToBase64() { + public void shouldTransformUrlSafeToBase64() { + urlToBase64(",url_safe:'true'", "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g_dj1kYUxnc1BTdkQ5QQ=="); + } + + @Test + public void shouldTransformNotUrlSafeToBase64AsDefault() { + urlToBase64("", "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kYUxnc1BTdkQ5QQ=="); + } + + private void urlToBase64(final String option, final String expected) { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( - "to_base64('data.title', url_safe:'true')" + "to_base64('data.title'" + option + ")" ), i -> { i.startRecord("1"); i.startEntity("data"); - i.literal("title", "https://www.youtube.com/watch?v=daLgsPSvD9A"); + i.literal("title", YOUTUBE_URL); i.endEntity(); i.endRecord(); }, o -> { o.get().startRecord("1"); o.get().startEntity("data"); - o.get().literal("title", "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g_dj1kYUxnc1BTdkQ5QQ=="); + o.get().literal("title", expected); o.get().endEntity(); o.get().endRecord(); }