Skip to content

Commit

Permalink
Add goxml2json library (#11)
Browse files Browse the repository at this point in the history
* Add goxml2json library

* Add xml to json request body templating parser

* Run go mod vendor
  • Loading branch information
tommysitu committed Apr 26, 2024
1 parent b64224f commit f299ae2
Show file tree
Hide file tree
Showing 13 changed files with 560 additions and 12 deletions.
12 changes: 12 additions & 0 deletions core/templating/templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,18 @@ func Test_ApplyTemplate_Request_Body_Jsonpath_LargeInt(t *testing.T) {
Expect(template).To(Equal("5553686208582"))
}

func Test_ApplyTemplate_Request_Body_Jsonpath_From_Xml(t *testing.T) {
RegisterTestingT(t)

template, err := ApplyTemplate(&models.RequestDetails{
Body: `<?xml version="1.0" encoding="UTF-8"?><name>Johnny</name>`,
}, make(map[string]string), `{{ Request.Body 'jsonpathfromxml' '$.name' }}`)

Expect(err).To(BeNil())

Expect(template).To(Equal("Johnny"))
}

func Test_ApplyTemplate_ReplaceStringInQueryParams(t *testing.T) {
RegisterTestingT(t)

Expand Down
32 changes: 20 additions & 12 deletions core/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strconv"
"time"

xj "github.com/basgys/goxml2json"
"github.com/tdewolff/minify/v2"
mjson "github.com/tdewolff/minify/v2/json"
"github.com/tdewolff/minify/v2/xml"
Expand Down Expand Up @@ -374,24 +375,22 @@ func RandStringFromTimestamp(length int) string {
func FetchFromRequestBody(queryType, query, toMatch string) interface{} {

if queryType == "jsonpath" {
result := jsonPath(query, toMatch)
var data interface{}
err := json.Unmarshal([]byte(result), &data)

arrayData, ok := data.([]interface{})

if err != nil || !ok {
return result
}
return arrayData
return jsonPath(query, toMatch)
} else if queryType == "xpath" {
return xPath(query, toMatch)
} else if queryType == "jsonpathfromxml" {
xmlReader := strings.NewReader(toMatch)
jsonBytes, err := xj.Convert(xmlReader)
if err != nil {
return ""
}
return jsonPath(query, jsonBytes.String())
}
log.Errorf("Unknown query type \"%s\" for templating Request.Body", queryType)
return ""
}

func jsonPath(query, toMatch string) string {
func jsonPath(query, toMatch string) interface{} {
query = PrepareJsonPathQuery(query)

result, err := JsonPathExecution(query, toMatch)
Expand All @@ -408,7 +407,16 @@ func jsonPath(query, toMatch string) string {
result = strconv.Itoa(intResult)
}

return result
// convert to array data if applicable
var data interface{}
err = json.Unmarshal([]byte(result), &data)

arrayData, ok := data.([]interface{})

if err != nil || !ok {
return result
}
return arrayData
}

func xPath(query, toMatch string) string {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/SpectoLabs/goproxy/ext v0.0.0-20220724221645-71c396c297b7
github.com/SpectoLabs/raymond v2.0.3-0.20240313210732-e0e216cf0920+incompatible
github.com/antonholmquist/jason v1.0.1-0.20160829104012-962e09b85496
github.com/basgys/goxml2json v1.1.0
github.com/beevik/etree v1.1.0
github.com/boltdb/bolt v1.2.1-0.20160424201119-d97499360d1e
github.com/brianvoe/gofakeit/v6 v6.19.0
Expand Down Expand Up @@ -46,6 +47,7 @@ require (
require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/aymerick/raymond v2.0.2+incompatible // indirect
github.com/bitly/go-simplejson v0.5.1 // indirect
github.com/corpix/uarand v0.0.0-20170903190822-2b8494104d86 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ github.com/antonholmquist/jason v1.0.1-0.20160829104012-962e09b85496 h1:dESITduf
github.com/antonholmquist/jason v1.0.1-0.20160829104012-962e09b85496/go.mod h1:+GxMEKI0Va2U8h3os6oiUAetHAlGMvxjdpAH/9uvUMA=
github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0=
github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/basgys/goxml2json v1.1.0 h1:4ln5i4rseYfXNd86lGEB+Vi652IsIXIvggKM/BhUKVw=
github.com/basgys/goxml2json v1.1.0/go.mod h1:wH7a5Np/Q4QoECFIU8zTQlZwZkrilY0itPfecMw41Dw=
github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs=
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
github.com/bitly/go-simplejson v0.5.1 h1:xgwPbetQScXt1gh9BmoJ6j9JMr3TElvuIyjR8pgdoow=
github.com/bitly/go-simplejson v0.5.1/go.mod h1:YOPVLzCfwK14b4Sff3oP1AmGhI9T9Vsg84etUnlyp+Q=
github.com/boltdb/bolt v1.2.1-0.20160424201119-d97499360d1e h1:ZjpTXDvUplNMT6aktSoMffTkAGIQJGNGPQW/xL4kPwA=
github.com/boltdb/bolt v1.2.1-0.20160424201119-d97499360d1e/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/brianvoe/gofakeit/v6 v6.19.0 h1:g+yJ+meWVEsAmR+bV4mNM/eXI0N+0pZ3D+Mi+G5+YQo=
Expand Down
25 changes: 25 additions & 0 deletions vendor/github.com/basgys/goxml2json/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/basgys/goxml2json/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions vendor/github.com/basgys/goxml2json/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/basgys/goxml2json/converter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f299ae2

Please sign in to comment.