Skip to content

Commit

Permalink
JSONPath scrubber (prototype).
Browse files Browse the repository at this point in the history
  • Loading branch information
bardzusny committed Jun 10, 2022
1 parent 32d5677 commit 79be0b8
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
9 changes: 9 additions & 0 deletions approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,15 @@ func (v verifyOptions) WithRegexScrubber(scrubber *regexp.Regexp, replacer strin
return v
}

// WithJSONPathScrubber allows you to 'scrub' dynamic data such as timestamps within your test input
// and replace it with a static placeholder
func (v verifyOptions) WithJSONPathScrubber(path string, replacer string) verifyOptions {
v.scrubbers = append(v.scrubbers, func(s string) string {
return scrubJSONPath(s, path, replacer)
})
return v
}

// WithExtension overrides the default file extension (.txt) for approval files.
func (v verifyOptions) WithExtension(extension string) verifyOptions {
v.extWithDot = extension
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/approvals/go-approval-tests

go 1.12

require github.com/bitly/go-simplejson v0.5.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
71 changes: 71 additions & 0 deletions scrubber_json_path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package approvals

import (
"strings"

"github.com/bitly/go-simplejson"
)

// converts received JSON string into a simpleJSON struct, scrubs the path with passed "scrubVal" using a separate util function, turns it into a string again
// (pretty naive implementation)
func scrubJSONPath(jsonStr string, path string, scrubVal interface{}) string {
js, err := simplejson.NewJson([]byte(jsonStr))
if err != nil {
panic(err)
}

scrubJSONPathSimpleJSON(js, path, scrubVal)

resS, err := js.EncodePretty()
if err != nil {
panic(err)
}
return string(resS)
}

// limited implementation of JSONPath for scrubbing values in simplejson structs:
//
// ScrubJSONPath(data, "foo.bar", "<bar_scrubbed>")
// { foo: { bar: "123" } } -> { foo: { bar: "<bar_scrubbed>" }
//
// - ScrubJSONPath(data, "foo[*].bar", "<bar_arr_scrubbed>")
// { foo: [{ bar: "123" }, { bar: "234" }] } -> { foo: [{ bar: "<bar_arr_scrubbed>" }, { bar: "<bar_arr_scrubbed>" }]) }
func scrubJSONPathSimpleJSON(data *simplejson.Json, path string, scrubVal interface{}) {
// "foo[*].bar" -> ["foo", "[*]", "bar"]
var elems []string
for _, elDot := range strings.Split(path, ".") {
if elDot == "[*]" {
elems = append(elems, "[*]")
} else if strings.HasSuffix(elDot, "[*]") {
elems = append(elems, strings.TrimSuffix(elDot, "[*]"))
elems = append(elems, "[*]")
} else {
elems = append(elems, elDot)
}
}

// end of recursive iteration, set the value (if key is present) and return:
if len(elems) == 1 {
if _, found := data.CheckGet(elems[0]); found {
data.Set(elems[0], scrubVal)
}
return
}

leftPath := path
for _, el := range elems {
leftPath = strings.TrimPrefix(leftPath, el)
leftPath = strings.TrimPrefix(leftPath, ".")

kjkj // special key for array object keys:
if el == "[*]" {
for i := 0; i < len(data.MustArray()); i++ {
scrubJSONPathSimpleJSON(data.GetIndex(i), leftPath, scrubVal)
}
} else {
if _, found := data.CheckGet(el); found {
scrubJSONPathSimpleJSON(data.Get(el), leftPath, scrubVal)
}
}
}
}
14 changes: 14 additions & 0 deletions scrubber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,17 @@ func TestVerifyAllWithRegexScrubber(t *testing.T) {
xs := []string{"Christopher", "Llewellyn"}
approvals.VerifyAll(t, "uppercase", xs, func(x interface{}) string { return fmt.Sprintf("%s => %s", x, strings.ToUpper(x.(string))) }, opts)
}

func TestVerifyJSONBytesWithJSONPathScrubber(t *testing.T) {
opts := approvals.Options().
WithJSONPathScrubber("greeting", "Hi!!!!").
WithJSONPathScrubber("list[*].greeting", "Why hello!!!")

jb := []byte(`{
"greeting":"Hello",
"list":[
{ "greeting": "Hello 1" },
{ "greeting": "Hello 2"}]
}`)
approvals.VerifyJSONBytes(t, jb, opts)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"greeting": "Hi!!!!",
"list": [
{
"greeting": "Why hello!!!"
},
{
"greeting": "Why hello!!!"
}
]
}

0 comments on commit 79be0b8

Please sign in to comment.