Skip to content

Commit

Permalink
Merge pull request #691 from krakendio/integration_schema
Browse files Browse the repository at this point in the history
Check the output body of the integration tests against a json-schema definition.
  • Loading branch information
kpacha authored Mar 16, 2023
2 parents 64fb2a2 + bed3378 commit ff24d0d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
38 changes: 38 additions & 0 deletions tests/fixtures/specs/integration_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"in": {
"method": "GET",
"url": "http://localhost:8080/static"
},
"out": {
"status_code": 200,
"@comment": "schema is used over the body",
"body": "{\"foo\":42,\"bar\":\"foobar\"}",
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"foo": {
"type": "integer"
},
"bar": {
"type": "string"
}
},
"required": [
"foo",
"bar"
]
},
"header": {
"content-type": [
"application/json; charset=utf-8"
],
"Cache-Control": [
"public, max-age=3600"
],
"X-Krakend-Completed": [
"true"
]
}
}
}
42 changes: 36 additions & 6 deletions tests/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"strings"
"sync"
"time"

"github.com/xeipuuv/gojsonschema"
)

var (
Expand Down Expand Up @@ -62,9 +64,10 @@ type Input struct {

// Output contains the data required to verify the response received in a given TestCase
type Output struct {
StatusCode int `json:"status_code"`
Body interface{} `json:"body"`
Header map[string][]string `json:"header"`
StatusCode int `json:"status_code"`
Body interface{} `json:"body"`
Header map[string][]string `json:"header"`
Schema map[string]interface{} `json:"schema"`
}

// CmdBuilder defines an interface for building the cmd to be managed by the Runner
Expand Down Expand Up @@ -274,7 +277,7 @@ func assertResponse(actual *http.Response, expected Output) error {
}

var body interface{}

var bodyBytes []byte
if actual.Body != nil {
b, err := io.ReadAll(actual.Body)
if err != nil {
Expand All @@ -288,10 +291,37 @@ func assertResponse(actual *http.Response, expected Output) error {
default:
_ = json.Unmarshal(b, &body)
}
bodyBytes = b
}

if !reflect.DeepEqual(body, expected.Body) {
errMsgs = append(errMsgs, fmt.Sprintf("unexpected body.\n\t\thave: %v\n\t\twant: %v", body, expected.Body))
if len(expected.Schema) != 0 {
s, err := json.Marshal(expected.Schema)
if err != nil {
return responseError{
errMessage: append(errMsgs, fmt.Sprintf("problem marshaling the user provided json-schema: %s", err)),
}
}
schema, err := gojsonschema.NewSchema(gojsonschema.NewBytesLoader(s))
if err != nil {
return responseError{
errMessage: append(errMsgs, fmt.Sprintf("problem generating json-schema schema: %s", err)),
}
}
result, err := schema.Validate(gojsonschema.NewBytesLoader(bodyBytes))
if err != nil {
return responseError{
errMessage: append(errMsgs, fmt.Sprintf("problem creating the json-schema validator: %s", err)),
}
}
if !result.Valid() {
return responseError{
errMessage: append(errMsgs, fmt.Sprintf("the result is not valid: %s", result.Errors())),
}
}
} else if expected.Body != "" {
if !reflect.DeepEqual(body, expected.Body) {
errMsgs = append(errMsgs, fmt.Sprintf("unexpected body.\n\t\thave: %v\n\t\twant: %v", body, expected.Body))
}
}
if len(errMsgs) == 0 {
return nil
Expand Down

0 comments on commit ff24d0d

Please sign in to comment.