|
| 1 | +/** |
| 2 | + * Query String tests. |
| 3 | + * |
| 4 | + * @author Benjamin Altpeter [[email protected]] |
| 5 | + * @copyright Crown Copyright 2022 |
| 6 | + * @license Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +import TestRegister from "../../lib/TestRegister.mjs"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Small helper for JSON.stringify() with the correct settings. |
| 13 | + * @param {any} obj An object to stringify |
| 14 | + * @returns A stringified version of the object, indented by four spaces. |
| 15 | + */ |
| 16 | +const json = (obj) => JSON.stringify(obj, null, 4); |
| 17 | + |
| 18 | +TestRegister.addTests([ |
| 19 | + { |
| 20 | + name: "Query String Decode simple example (defaults)", |
| 21 | + input: "?a=b&c=1&d=e;f=g", |
| 22 | + expectedOutput: json({ |
| 23 | + a: "b", |
| 24 | + c: "1", |
| 25 | + d: "e;f=g", |
| 26 | + }), |
| 27 | + recipeConfig: [ |
| 28 | + { op: "Query String Decode", args: [5, 1000, "&", false, false] }, |
| 29 | + ], |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Query String Decode arrays and objects (defaults)", |
| 33 | + input: "a[]=b&a[2]=b&c[d]=e&f=g,h&i.j=k", |
| 34 | + expectedOutput: json({ |
| 35 | + a: ["b", "b"], |
| 36 | + c: { |
| 37 | + d: "e", |
| 38 | + }, |
| 39 | + f: "g,h", |
| 40 | + "i.j": "k", |
| 41 | + }), |
| 42 | + recipeConfig: [ |
| 43 | + { op: "Query String Decode", args: [5, 1000, "&", false, false] }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Query String Decode arrays and objects (extended)", |
| 48 | + input: "a[]=b&a[2]=b&c[d]=e&f=g,h&i.j=k", |
| 49 | + expectedOutput: json({ |
| 50 | + a: ["b", "b"], |
| 51 | + c: { |
| 52 | + d: "e", |
| 53 | + }, |
| 54 | + f: ["g", "h"], |
| 55 | + i: { |
| 56 | + j: "k", |
| 57 | + }, |
| 58 | + }), |
| 59 | + recipeConfig: [ |
| 60 | + { op: "Query String Decode", args: [5, 1000, "&", true, true] }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "Query String Decode delimiter", |
| 65 | + input: "a=b;c=d", |
| 66 | + expectedOutput: json({ |
| 67 | + a: "b", |
| 68 | + c: "d", |
| 69 | + }), |
| 70 | + recipeConfig: [ |
| 71 | + { op: "Query String Decode", args: [5, 1000, ";", false, false] }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "Query String Decode depth (default)", |
| 76 | + input: "a[b][c][d][e][f][g][h]=5", |
| 77 | + expectedOutput: json({ |
| 78 | + a: { |
| 79 | + b: { |
| 80 | + c: { |
| 81 | + d: { |
| 82 | + e: { |
| 83 | + f: { |
| 84 | + "[g][h]": "5", |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }), |
| 92 | + recipeConfig: [ |
| 93 | + { op: "Query String Decode", args: [5, 1000, "&", false, false] }, |
| 94 | + ], |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "Query String Decode depth (higher)", |
| 98 | + input: "a[b][c][d][e][f][g][h]=5", |
| 99 | + expectedOutput: json({ |
| 100 | + a: { |
| 101 | + b: { |
| 102 | + c: { |
| 103 | + d: { |
| 104 | + e: { |
| 105 | + f: { |
| 106 | + g: { |
| 107 | + h: "5", |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }), |
| 116 | + recipeConfig: [ |
| 117 | + { op: "Query String Decode", args: [7, 1000, "&", false, false] }, |
| 118 | + ], |
| 119 | + }, |
| 120 | +]); |
0 commit comments