diff --git a/pkg/runner/http.go b/pkg/runner/http.go index 9171f03..548d422 100644 --- a/pkg/runner/http.go +++ b/pkg/runner/http.go @@ -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") +} diff --git a/pkg/runner/http_test.go b/pkg/runner/http_test.go index 8199f43..c600201 100644 --- a/pkg/runner/http_test.go +++ b/pkg/runner/http_test.go @@ -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) { diff --git a/pkg/runner/verify.go b/pkg/runner/verify.go index e06525e..9b7e186 100644 --- a/pkg/runner/verify.go +++ b/pkg/runner/verify.go @@ -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() { diff --git a/pkg/runner/verify_test.go b/pkg/runner/verify_test.go index 073b549..da69367 100644 --- a/pkg/runner/verify_test.go +++ b/pkg/runner/verify_test.go @@ -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", +}