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

Support range definition for response codes. #65

Merged
merged 3 commits into from
Apr 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: 7 additions & 1 deletion responses/validate_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package responses

import (
"fmt"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -47,7 +48,12 @@ func (v *responseBodyValidator) ValidateResponseBody(
mediaTypeSting, _, _ := helpers.ExtractContentType(contentType)

// check if the response code is in the contract
foundResponse := operation.Responses.FindResponseByCode(httpCode)
foundResponse := operation.Responses.Codes.GetOrZero(fmt.Sprintf("%d", httpCode))
if foundResponse == nil {
// check range definition for response codes
foundResponse = operation.Responses.Codes.GetOrZero(fmt.Sprintf("%dXX", httpCode/100))
}

if foundResponse != nil {
if foundResponse.Content != nil { // only validate if we have content types.
// check content type has been defined in the contract
Expand Down
107 changes: 104 additions & 3 deletions responses/validate_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ package responses
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/paths"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

func TestValidateBody_MissingContentType(t *testing.T) {
Expand Down Expand Up @@ -374,6 +376,96 @@ paths:
//assert.Len(t, errors[0].SchemaValidationErrors, 2)
}

func TestValidateBody_InvalidResponseBodyNil(t *testing.T) {
spec := `openapi: 3.1.0
paths:
/burgers/createBurger:
post:
responses:
'200':
content:
application/json:
schema:
type: object
properties:
name:
type: string
patties:
type: integer
vegetarian:
type: boolean`

doc, _ := libopenapi.NewDocument([]byte(spec))

m, _ := doc.BuildV3Model()
v := NewResponseBodyValidator(&m.Model)

// build a request
request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger", http.NoBody)
request.Header.Set(helpers.ContentTypeHeader, helpers.JSONContentType)

// invalid response
response := &http.Response{
Header: http.Header{},
StatusCode: http.StatusOK,
Body: nil, // invalid response body
}
response.Header.Set(helpers.ContentTypeHeader, helpers.JSONContentType)

// validate!
valid, errors := v.ValidateResponseBody(request, response)
// doubletap to hit cache
_, _ = v.ValidateResponseBody(request, response)

assert.False(t, valid)
assert.Len(t, errors, 1)
}

func TestValidateBody_InvalidResponseBodyError(t *testing.T) {
spec := `openapi: 3.1.0
paths:
/burgers/createBurger:
post:
responses:
'200':
content:
application/json:
schema:
type: object
properties:
name:
type: string
patties:
type: integer
vegetarian:
type: boolean`

doc, _ := libopenapi.NewDocument([]byte(spec))

m, _ := doc.BuildV3Model()
v := NewResponseBodyValidator(&m.Model)

// build a request
request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger", http.NoBody)
request.Header.Set(helpers.ContentTypeHeader, helpers.JSONContentType)

// invalid response
response := &http.Response{
Header: http.Header{},
StatusCode: http.StatusOK,
Body: &errorReader{},
}
response.Header.Set(helpers.ContentTypeHeader, helpers.JSONContentType)

// validate!
valid, errors := v.ValidateResponseBody(request, response)
// doubletap to hit cache
_, _ = v.ValidateResponseBody(request, response)

assert.False(t, valid)
assert.Len(t, errors, 1)
}

func TestValidateBody_InvalidBasicSchema_SetPath(t *testing.T) {
spec := `openapi: 3.1.0
paths:
Expand Down Expand Up @@ -1090,3 +1182,12 @@ paths:
assert.Len(t, errors, 0)

}

type errorReader struct{}

func (er *errorReader) Read(p []byte) (n int, err error) {
return 0, errors.New("some io error")
}
func (er *errorReader) Close() error {
return nil
}
43 changes: 43 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1170,3 +1170,46 @@ func TestNewValidator_PetStore_PetFindByStatusGet200_Valid_responseOnly(t *testi
assert.True(t, valid)
assert.Len(t, errors, 0)
}

func TestNewValidator_ValidateHttpResponse_RangeResponseCode(t *testing.T) {

spec := `openapi: 3.1.0
paths:
/burgers:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
type: object
properties:
name:
type: string
patties:
type: integer
vegetarian:
type: boolean
'4XX':
description: Bad request
'5XX':
description: Server error`

doc, _ := libopenapi.NewDocument([]byte(spec))

v, _ := NewValidator(doc)

request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers", nil)
request.Header.Set("Content-Type", "application/json")
response := &http.Response{
StatusCode: 400,
Header: http.Header{"Content-Type": []string{"application/json"}},
}
valid, errors := v.ValidateHttpResponse(request, response)

assert.True(t, valid)
assert.Len(t, errors, 0)
}
Loading