-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test case causing panic on request body validation
- Loading branch information
1 parent
688b7a2
commit 41cb2de
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package requests | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/pb33f/libopenapi/datamodel/high/base" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateRequestSchema(t *testing.T) { | ||
for name, tc := range map[string]struct { | ||
request *http.Request | ||
schema *base.Schema | ||
renderedSchema, jsonSchema []byte | ||
assertValidRequestSchema assert.BoolAssertionFunc | ||
expectedErrorsCount int | ||
}{ | ||
"FailRequestBodyValidation": { | ||
// KeywordLocation: /allOf/1/$ref/properties/properties/additionalProperties/$dynamicRef/allOf/3/$ref/properties/exclusiveMinimum/type | ||
// Message: expected number, but got boolean | ||
request: &http.Request{ | ||
Method: http.MethodPost, | ||
Body: io.NopCloser(strings.NewReader(`{"exclusiveNumber": 13}`)), | ||
}, | ||
schema: &base.Schema{ | ||
Type: []string{"object"}, | ||
}, | ||
renderedSchema: []byte(`type: object | ||
properties: | ||
exclusiveNumber: | ||
type: number | ||
description: This number starts its journey where most numbers are too scared to begin! | ||
exclusiveMinimum: true | ||
minimum: !!float 10`), | ||
jsonSchema: []byte(`{"properties":{"exclusiveNumber":{"description":"This number starts its journey where most numbers are too scared to begin!","exclusiveMinimum":true,"minimum":10,"type":"number"}},"type":"object"}`), | ||
assertValidRequestSchema: assert.False, | ||
expectedErrorsCount: 1, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
valid, errors := ValidateRequestSchema(tc.request, tc.schema, tc.renderedSchema, tc.jsonSchema) | ||
|
||
tc.assertValidRequestSchema(t, valid) | ||
assert.Len(t, errors, tc.expectedErrorsCount) | ||
}) | ||
} | ||
} |