add and fix tests for json

dev
seb 2021-07-31 23:48:14 +02:00
parent 3529cfa1d6
commit 166344d793
2 changed files with 77 additions and 5 deletions

View File

@ -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
}

View File

@ -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 = `
</body>
</html>
`
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"
}
]
}
`