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
|
// isNonBinaryContent detect if the content belong to binary
|
||||||
func isNonBinaryContent(contentType string) bool {
|
func isNonBinaryContent(contentType string) bool {
|
||||||
|
if IsJSONCompatileType(contentType) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
switch contentType {
|
switch contentType {
|
||||||
case util.JSON, util.YAML, util.Plain, util.OCIImageIndex:
|
case util.JSON, util.YAML, util.Plain, util.OCIImageIndex:
|
||||||
return true
|
return true
|
||||||
|
@ -378,3 +382,7 @@ func isNonBinaryContent(contentType string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsJSONCompatileType(contentType string) bool {
|
||||||
|
return strings.HasSuffix(contentType, "+json")
|
||||||
|
}
|
||||||
|
|
|
@ -575,6 +575,9 @@ func TestIsStructContent(t *testing.T) {
|
||||||
}, {
|
}, {
|
||||||
contentType: util.OCIImageIndex,
|
contentType: util.OCIImageIndex,
|
||||||
expectOk: true,
|
expectOk: true,
|
||||||
|
}, {
|
||||||
|
contentType: "application/problem+json",
|
||||||
|
expectOk: true,
|
||||||
}}
|
}}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.contentType, func(t *testing.T) {
|
t.Run(tt.contentType, func(t *testing.T) {
|
||||||
|
|
|
@ -89,6 +89,10 @@ type BodyGetter interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBodyVerify(contentType string, body BodyGetter) BodyVerifier {
|
func NewBodyVerify(contentType string, body BodyGetter) BodyVerifier {
|
||||||
|
if IsJSONCompatileType(contentType) {
|
||||||
|
contentType = util.JSON
|
||||||
|
}
|
||||||
|
|
||||||
switch contentType {
|
switch contentType {
|
||||||
case util.JSON:
|
case util.JSON:
|
||||||
return &jsonBodyVerifier{body: body}
|
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) {
|
func (v *jsonBodyVerifier) Verify(data []byte) (err error) {
|
||||||
|
if v.body == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
for key, expectVal := range v.body.GetBodyFieldsExpect() {
|
for key, expectVal := range v.body.GetBodyFieldsExpect() {
|
||||||
result := gjson.Get(string(data), key)
|
result := gjson.Get(string(data), key)
|
||||||
if result.Exists() {
|
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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with 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) {
|
t.Run("verify YAML contentType", func(t *testing.T) {
|
||||||
assert.Nil(t, runner.NewBodyVerify("fake", nil))
|
assert.Nil(t, runner.NewBodyVerify("fake", nil))
|
||||||
verfier := runner.NewBodyVerify(util.YAML, nil)
|
verifer := runner.NewBodyVerify(util.YAML, nil)
|
||||||
assert.NotNil(t, verfier)
|
assert.NotNil(t, verifer)
|
||||||
|
|
||||||
obj, err := verfier.Parse([]byte(`name: linuxsuren`))
|
obj, err := verifer.Parse([]byte(`name: linuxsuren`))
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, map[string]interface{}{
|
assert.Equal(t, expectJSONObj, obj)
|
||||||
"name": "linuxsuren",
|
assert.NoError(t, verifer.Verify(nil))
|
||||||
}, obj)
|
})
|
||||||
assert.NoError(t, verfier.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