feat: support JSON compaitble contentType parse (#496)
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
parent
dabb95542d
commit
6495a50f51
|
@ -371,6 +371,10 @@ func runJob(job *testing.Job, ctx interface{}, current interface{}) (err error)
|
|||
|
||||
// isNonBinaryContent detect if the content belong to binary
|
||||
func isNonBinaryContent(contentType string) bool {
|
||||
if IsJSONCompatileType(contentType) {
|
||||
return true
|
||||
}
|
||||
|
||||
switch contentType {
|
||||
case util.JSON, util.YAML, util.Plain, util.OCIImageIndex:
|
||||
return true
|
||||
|
@ -378,3 +382,7 @@ func isNonBinaryContent(contentType string) bool {
|
|||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func IsJSONCompatileType(contentType string) bool {
|
||||
return strings.HasSuffix(contentType, "+json")
|
||||
}
|
||||
|
|
|
@ -575,6 +575,9 @@ func TestIsStructContent(t *testing.T) {
|
|||
}, {
|
||||
contentType: util.OCIImageIndex,
|
||||
expectOk: true,
|
||||
}, {
|
||||
contentType: "application/problem+json",
|
||||
expectOk: true,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.contentType, func(t *testing.T) {
|
||||
|
|
|
@ -89,6 +89,10 @@ type BodyGetter interface {
|
|||
}
|
||||
|
||||
func NewBodyVerify(contentType string, body BodyGetter) BodyVerifier {
|
||||
if IsJSONCompatileType(contentType) {
|
||||
contentType = util.JSON
|
||||
}
|
||||
|
||||
switch contentType {
|
||||
case util.JSON:
|
||||
return &jsonBodyVerifier{body: body}
|
||||
|
@ -126,6 +130,9 @@ func (v *jsonBodyVerifier) Parse(data []byte) (obj interface{}, err error) {
|
|||
}
|
||||
|
||||
func (v *jsonBodyVerifier) Verify(data []byte) (err error) {
|
||||
if v.body == nil {
|
||||
return
|
||||
}
|
||||
for key, expectVal := range v.body.GetBodyFieldsExpect() {
|
||||
result := gjson.Get(string(data), key)
|
||||
if result.Exists() {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright 2023 API Testing Authors.
|
||||
Copyright 2023-2024 API Testing Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
@ -49,14 +49,36 @@ func TestVerify(t *testing.T) {
|
|||
|
||||
t.Run("verify YAML contentType", func(t *testing.T) {
|
||||
assert.Nil(t, runner.NewBodyVerify("fake", nil))
|
||||
verfier := runner.NewBodyVerify(util.YAML, nil)
|
||||
assert.NotNil(t, verfier)
|
||||
verifer := runner.NewBodyVerify(util.YAML, nil)
|
||||
assert.NotNil(t, verifer)
|
||||
|
||||
obj, err := verfier.Parse([]byte(`name: linuxsuren`))
|
||||
obj, err := verifer.Parse([]byte(`name: linuxsuren`))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, map[string]interface{}{
|
||||
"name": "linuxsuren",
|
||||
}, obj)
|
||||
assert.NoError(t, verfier.Verify(nil))
|
||||
assert.Equal(t, expectJSONObj, obj)
|
||||
assert.NoError(t, verifer.Verify(nil))
|
||||
})
|
||||
|
||||
t.Run("verify JSON compatible type", func(t *testing.T) {
|
||||
verifer := runner.NewBodyVerify("application/problem+json", nil)
|
||||
assert.NotNil(t, verifer)
|
||||
|
||||
obj, err := verifer.Parse([]byte(`{"name":"linuxsuren"}`))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectJSONObj, obj)
|
||||
assert.NoError(t, verifer.Verify(nil))
|
||||
})
|
||||
|
||||
t.Run("verify plain type", func(t *testing.T) {
|
||||
verifer := runner.NewBodyVerify(util.Plain, nil)
|
||||
assert.NotNil(t, verifer)
|
||||
|
||||
obj, err := verifer.Parse([]byte("hello"))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "hello", obj)
|
||||
assert.NoError(t, verifer.Verify(nil))
|
||||
})
|
||||
}
|
||||
|
||||
var expectJSONObj = map[string]interface{}{
|
||||
"name": "linuxsuren",
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue