From 95d5fd1789fd378ac3fcc1d99d90eea6f71b4a31 Mon Sep 17 00:00:00 2001 From: es45411 <135977478+es45411@users.noreply.github.com> Date: Mon, 12 May 2025 13:09:49 +0000 Subject: [PATCH 1/2] Add treat space as plus URLDecode option --- src/core/operations/URLDecode.mjs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/core/operations/URLDecode.mjs b/src/core/operations/URLDecode.mjs index 7d6544ac..bb6c0612 100644 --- a/src/core/operations/URLDecode.mjs +++ b/src/core/operations/URLDecode.mjs @@ -23,7 +23,13 @@ class URLDecode extends Operation { this.infoURL = "https://wikipedia.org/wiki/Percent-encoding"; this.inputType = "string"; this.outputType = "string"; - this.args = []; + this.args = [ + { + "name": "Treat \"+\" as space", + "type": "boolean", + "value": true + }, + ]; this.checks = [ { pattern: ".*(?:%[\\da-f]{2}.*){4}", @@ -39,7 +45,8 @@ class URLDecode extends Operation { * @returns {string} */ run(input, args) { - const data = input.replace(/\+/g, "%20"); + const plusIsSpace = args[0]; + const data = plusIsSpace ? input.replace(/\+/g, "%20") : input; try { return decodeURIComponent(data); } catch (err) { From 3a55b34214d32209ab918c0a05694972c177011a Mon Sep 17 00:00:00 2001 From: es45411 <135977478+es45411@users.noreply.github.com> Date: Mon, 12 May 2025 13:09:59 +0000 Subject: [PATCH 2/2] Add tests for URLDecode and URLEncode --- tests/operations/index.mjs | 1 + tests/operations/tests/URLEncodeDecode.mjs | 92 ++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 tests/operations/tests/URLEncodeDecode.mjs diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index ab1ceb8f..27151c3c 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -163,6 +163,7 @@ import "./tests/TranslateDateTimeFormat.mjs"; import "./tests/Typex.mjs"; import "./tests/UnescapeString.mjs"; import "./tests/Unicode.mjs"; +import "./tests/URLEncodeDecode.mjs"; import "./tests/RSA.mjs"; import "./tests/CBOREncode.mjs"; import "./tests/CBORDecode.mjs"; diff --git a/tests/operations/tests/URLEncodeDecode.mjs b/tests/operations/tests/URLEncodeDecode.mjs new file mode 100644 index 00000000..444f76d3 --- /dev/null +++ b/tests/operations/tests/URLEncodeDecode.mjs @@ -0,0 +1,92 @@ +/** + * URLEncode and URLDecode tests. + * + * @author es45411 [135977478+es45411@users.noreply.github.com] + * + * @copyright Crown Copyright 2018 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + // URL Decode + { + name: "URLDecode: nothing", + input: "", + expectedOutput: "", + recipeConfig: [ + { + op: "URL Decode", + args: [], + }, + ], + }, + { + name: "URLDecode: spaces without special chars", + input: "Hello%20world%21", + expectedOutput: "Hello world!", + recipeConfig: [ + { + op: "URL Decode", + args: [], + }, + ], + }, + { + name: "URLDecode: spaces with special chars", + input: "Hello%20world!", + expectedOutput: "Hello world!", + recipeConfig: [ + { + op: "URL Decode", + args: [], + }, + ], + }, + { + name: "URLDecode: decode plus as space", + input: "Hello%20world!", + expectedOutput: "Hello world!", + recipeConfig: [ + { + op: "URL Decode", + args: [], + }, + ], + }, + // URL Encode + { + name: "URLEncode: nothing", + input: "", + expectedOutput: "", + recipeConfig: [ + { + op: "URL Encode", + args: [], + }, + ], + }, + { + name: "URLEncode: spaces without special chars", + input: "Hello world!", + expectedOutput: "Hello%20world!", + recipeConfig: [ + { + op: "URL Encode", + args: [], + }, + ], + }, + { + name: "URLEncode: spaces with special chars", + input: "Hello world!", + expectedOutput: "Hello%20world%21", + recipeConfig: [ + { + op: "URL Encode", + args: [true], + }, + ], + }, +]);