Skip to content

Commit 761173b

Browse files
authored
Merge pull request #2040 from gchq/feature/add-toggle-plus-to-urldecode
Add toggle "+" character to URLDecode operation
2 parents 7ecf8df + 3a55b34 commit 761173b

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

src/core/operations/URLDecode.mjs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ class URLDecode extends Operation {
2323
this.infoURL = "https://wikipedia.org/wiki/Percent-encoding";
2424
this.inputType = "string";
2525
this.outputType = "string";
26-
this.args = [];
26+
this.args = [
27+
{
28+
"name": "Treat \"+\" as space",
29+
"type": "boolean",
30+
"value": true
31+
},
32+
];
2733
this.checks = [
2834
{
2935
pattern: ".*(?:%[\\da-f]{2}.*){4}",
@@ -39,7 +45,8 @@ class URLDecode extends Operation {
3945
* @returns {string}
4046
*/
4147
run(input, args) {
42-
const data = input.replace(/\+/g, "%20");
48+
const plusIsSpace = args[0];
49+
const data = plusIsSpace ? input.replace(/\+/g, "%20") : input;
4350
try {
4451
return decodeURIComponent(data);
4552
} catch (err) {

tests/operations/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ import "./tests/TranslateDateTimeFormat.mjs";
163163
import "./tests/Typex.mjs";
164164
import "./tests/UnescapeString.mjs";
165165
import "./tests/Unicode.mjs";
166+
import "./tests/URLEncodeDecode.mjs";
166167
import "./tests/RSA.mjs";
167168
import "./tests/CBOREncode.mjs";
168169
import "./tests/CBORDecode.mjs";
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* URLEncode and URLDecode tests.
3+
*
4+
* @author es45411 [[email protected]]
5+
*
6+
* @copyright Crown Copyright 2018
7+
* @license Apache-2.0
8+
*/
9+
10+
import TestRegister from "../../lib/TestRegister.mjs";
11+
12+
TestRegister.addTests([
13+
// URL Decode
14+
{
15+
name: "URLDecode: nothing",
16+
input: "",
17+
expectedOutput: "",
18+
recipeConfig: [
19+
{
20+
op: "URL Decode",
21+
args: [],
22+
},
23+
],
24+
},
25+
{
26+
name: "URLDecode: spaces without special chars",
27+
input: "Hello%20world%21",
28+
expectedOutput: "Hello world!",
29+
recipeConfig: [
30+
{
31+
op: "URL Decode",
32+
args: [],
33+
},
34+
],
35+
},
36+
{
37+
name: "URLDecode: spaces with special chars",
38+
input: "Hello%20world!",
39+
expectedOutput: "Hello world!",
40+
recipeConfig: [
41+
{
42+
op: "URL Decode",
43+
args: [],
44+
},
45+
],
46+
},
47+
{
48+
name: "URLDecode: decode plus as space",
49+
input: "Hello%20world!",
50+
expectedOutput: "Hello world!",
51+
recipeConfig: [
52+
{
53+
op: "URL Decode",
54+
args: [],
55+
},
56+
],
57+
},
58+
// URL Encode
59+
{
60+
name: "URLEncode: nothing",
61+
input: "",
62+
expectedOutput: "",
63+
recipeConfig: [
64+
{
65+
op: "URL Encode",
66+
args: [],
67+
},
68+
],
69+
},
70+
{
71+
name: "URLEncode: spaces without special chars",
72+
input: "Hello world!",
73+
expectedOutput: "Hello%20world!",
74+
recipeConfig: [
75+
{
76+
op: "URL Encode",
77+
args: [],
78+
},
79+
],
80+
},
81+
{
82+
name: "URLEncode: spaces with special chars",
83+
input: "Hello world!",
84+
expectedOutput: "Hello%20world%21",
85+
recipeConfig: [
86+
{
87+
op: "URL Encode",
88+
args: [true],
89+
},
90+
],
91+
},
92+
]);

0 commit comments

Comments
 (0)