From 166344d7939e8a7ea2bc343083b296e1ed747120 Mon Sep 17 00:00:00 2001 From: seb <45340011+KaulSe@users.noreply.github.com> Date: Sat, 31 Jul 2021 23:48:14 +0200 Subject: [PATCH] add and fix tests for json --- v2/pkg/operators/extractors/extract.go | 8 +-- v2/pkg/protocols/http/operators_test.go | 74 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/v2/pkg/operators/extractors/extract.go b/v2/pkg/operators/extractors/extract.go index c1c9fbd6e..81f995b1a 100644 --- a/v2/pkg/operators/extractors/extract.go +++ b/v2/pkg/operators/extractors/extract.go @@ -66,13 +66,11 @@ func (e *Extractor) ExtractJson(corpus string) map[string]struct{} { if _, ok := v.(error); ok { break } - bytes, err := json.Marshal(v) - if err != nil { - break + itemString := types.ToString(v) + if _, ok := results[itemString]; !ok { + results[itemString] = struct{}{} } - results[string(bytes)] = struct{}{} } } - return results } diff --git a/v2/pkg/protocols/http/operators_test.go b/v2/pkg/protocols/http/operators_test.go index 20ff31d9b..d658edc78 100644 --- a/v2/pkg/protocols/http/operators_test.go +++ b/v2/pkg/protocols/http/operators_test.go @@ -1,6 +1,7 @@ package http import ( + "fmt" "net/http" "testing" "time" @@ -168,6 +169,20 @@ func TestHTTPOperatorExtract(t *testing.T) { require.Greater(t, len(data), 0, "could not extractor kval valid response") require.Equal(t, map[string]struct{}{"Test-Response": {}}, data, "could not extract correct kval data") }) + + t.Run("json", func(t *testing.T) { + extractor := &extractors.Extractor{ + Type: "json", + Json: []string{".batters | .batter | .[] | .id"}, + } + err = extractor.CompileExtractors() + require.Nil(t, err, "could not compile json extractor") + + event["body"] = exampleJSONResponseBody + data := request.Extract(event, extractor) + require.Greater(t, len(data), 0, "could not extractor json valid response") + require.Equal(t, map[string]struct{}{"1001": {}, "1002": {}, "1003": {}, "1004": {}}, data, "could not extract correct json data") + }) } func TestHTTPMakeResult(t *testing.T) { @@ -305,3 +320,62 @@ const exampleResponseBody = ` ` + +const exampleJSONResponseBody = ` +{ + "id": "0001", + "type": "donut", + "name": "Cake", + "ppu": 0.55, + "batters": { + "batter": [ + { + "id": "1001", + "type": "Regular" + }, + { + "id": "1002", + "type": "Chocolate" + }, + { + "id": "1003", + "type": "Blueberry" + }, + { + "id": "1004", + "type": "Devil's Food" + } + ] + }, + "topping": [ + { + "id": "5001", + "type": "None" + }, + { + "id": "5002", + "type": "Glazed" + }, + { + "id": "5005", + "type": "Sugar" + }, + { + "id": "5007", + "type": "Powdered Sugar" + }, + { + "id": "5006", + "type": "Chocolate with Sprinkles" + }, + { + "id": "5003", + "type": "Chocolate" + }, + { + "id": "5004", + "type": "Maple" + } + ] +} +`