Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support JSON compaitble contentType parse #496

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/runner/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,18 @@ 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
default:
return false
}
}

func IsJSONCompatileType(contentType string) bool {
return strings.HasSuffix(contentType, "+json")
}
3 changes: 3 additions & 0 deletions pkg/runner/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/runner/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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() {
Expand Down
38 changes: 30 additions & 8 deletions pkg/runner/verify_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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