Skip to content

Commit

Permalink
Add test for invalid response body
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW authored and daveshanley committed Apr 21, 2024
1 parent f29d820 commit 285c6d3
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions responses/validate_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,51 @@ paths:
//assert.Len(t, errors[0].SchemaValidationErrors, 2)
}

func TestValidateBody_InvalidResponse(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_InvalidBasicSchema_SetPath(t *testing.T) {
spec := `openapi: 3.1.0
paths:
Expand Down

0 comments on commit 285c6d3

Please sign in to comment.