diff --git a/cmd/root_test.go b/cmd/root_test.go index dccc2c7..1387568 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -211,3 +211,25 @@ Bye.`, }) } } + +func TestIsJSON(t *testing.T) { + tests := []struct { + input string + expected bool + }{ + {"{}", true}, + {"[]", true}, + {" {}", true}, + {"\t[", true}, + {"", false}, + {"", false}, // Bug #160: should not match [ in CDATA + {"plain text", false}, + } + + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + result := utils.IsJSON(tt.input) + assert.Equal(t, tt.expected, result) + }) + } +} diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 2597b2a..7f74e06 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -519,7 +519,7 @@ func IsHTML(input string) bool { func IsJSON(input string) bool { input = strings.ToLower(input) - matched, _ := regexp.MatchString(`\s*[{\[]`, input) + matched, _ := regexp.MatchString(`^\s*[{\[]`, input) return matched }