| 1 | package jsontransformer |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/DefangLabs/secret-detector/pkg/transformers/tests" |
| 7 | ) |
| 8 | |
| 9 | var testCases = []tests.TestCase{ |
| 10 | {"illegal json - empty input", "", nil}, |
| 11 | {"illegal json - simple string", "hello world", nil}, |
| 12 | {"illegal json - double quoted string", `"hello world"`, nil}, |
| 13 | {"illegal json - single quoted string", `'hello world'`, nil}, |
| 14 | {"illegal json - missing bracket", `{"key": "value"`, nil}, |
| 15 | {"illegal json - wrong brackets", `["key": "value"]`, nil}, |
| 16 | {"illegal json - missing key quotes", `{key: "value"}`, nil}, |
| 17 | {"illegal json - missing value quotes", `{"key": value}`, nil}, |
| 18 | {"illegal json - wrong separator", `{"key" = "value"}`, nil}, |
| 19 | {"illegal json - missing comma", `{"k1": "v1" "k2": "v2"}`, nil}, |
| 20 | {"illegal json - redundant comma", `{"key": "value", }`, nil}, |
| 21 | {"empty json", `{}`, map[string]string{}}, |
| 22 | {"value types", `{"s": "a string", "i": 10, "neg": -10, "f": 3.14, "e": 1e3, "b1": true, "b2": false, "u": "\u003a\u0029", "nil": null}`, |
| 23 | map[string]string{"s": "a string", "i": "10", "neg": "-10", "f": "3.14", "e": "1000", "b1": "true", "b2": "false", "u": ":)", "nil": "<nil>"}}, |
| 24 | {"special characters", `{ |
| 25 | "spa ce": "val ue", |
| 26 | "ta\tb": "val\tue", |
| 27 | "new\nline": "val\nue", |
| 28 | "sla/sh": "val/ue", |
| 29 | "back\\slash": "val\\ue", |
| 30 | "quo\"te": "val\"ue" |
| 31 | }`, |
| 32 | map[string]string{ |
| 33 | "spa ce": "val ue", |
| 34 | "ta\tb": "val\tue", |
| 35 | "new\nline": "val\nue", |
| 36 | "sla/sh": "val/ue", |
| 37 | "back\\slash": "val\\ue", |
| 38 | "quo\"te": "val\"ue", |
| 39 | }}, |
| 40 | {"nesting objects", ` |
| 41 | { |
| 42 | "a": { |
| 43 | "1": {"a": "v1"}, |
| 44 | "2": "v2" |
| 45 | }, |
| 46 | "b": "v3" |
| 47 | }`, |
| 48 | map[string]string{"a.1.a": "v1", "a.2": "v2", "b": "v3"}}, |
| 49 | {"arrays", ` |
| 50 | { |
| 51 | "arr1": [1,2,3], |
| 52 | "arr2": [ |
| 53 | {"1": {"a": "v1"} }, |
| 54 | {"2": "v2"}, |
| 55 | "3"] |
| 56 | }`, |
| 57 | map[string]string{ |
| 58 | "arr1[0]": "1", |
| 59 | "arr1[1]": "2", |
| 60 | "arr1[2]": "3", |
| 61 | "arr2[0].1.a": "v1", |
| 62 | "arr2[1].2": "v2", |
| 63 | "arr2[2]": "3", |
| 64 | }}, |
| 65 | {"json with CR/LF", |
| 66 | `{"k1": "v1",` + "\r\n" + `"k2": "v2"}`, |
| 67 | map[string]string{"k1": "v1", "k2": "v2"}}, |
| 68 | } |
| 69 | |
| 70 | var jsonTransformer = NewTransformer() |
| 71 | |
| 72 | func TestTransform(t *testing.T) { |
| 73 | tests.TestTransform(t, jsonTransformer, testCases) |
| 74 | } |