|
| 1 | +package requests |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/pb33f/libopenapi/datamodel/high/base" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestValidateRequestSchema(t *testing.T) { |
| 14 | + for name, tc := range map[string]struct { |
| 15 | + request *http.Request |
| 16 | + schema *base.Schema |
| 17 | + renderedSchema, jsonSchema []byte |
| 18 | + assertValidRequestSchema assert.BoolAssertionFunc |
| 19 | + expectedErrorsCount int |
| 20 | + }{ |
| 21 | + "FailRequestBodyValidation": { |
| 22 | + // KeywordLocation: /allOf/1/$ref/properties/properties/additionalProperties/$dynamicRef/allOf/3/$ref/properties/exclusiveMinimum/type |
| 23 | + // Message: expected number, but got boolean |
| 24 | + request: &http.Request{ |
| 25 | + Method: http.MethodPost, |
| 26 | + Body: io.NopCloser(strings.NewReader(`{"exclusiveNumber": 13}`)), |
| 27 | + }, |
| 28 | + schema: &base.Schema{ |
| 29 | + Type: []string{"object"}, |
| 30 | + }, |
| 31 | + renderedSchema: []byte(`type: object |
| 32 | +properties: |
| 33 | + exclusiveNumber: |
| 34 | + type: number |
| 35 | + description: This number starts its journey where most numbers are too scared to begin! |
| 36 | + exclusiveMinimum: true |
| 37 | + minimum: !!float 10`), |
| 38 | + 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"}`), |
| 39 | + assertValidRequestSchema: assert.False, |
| 40 | + expectedErrorsCount: 1, |
| 41 | + }, |
| 42 | + } { |
| 43 | + tc := tc |
| 44 | + t.Run(name, func(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + |
| 47 | + valid, errors := ValidateRequestSchema(tc.request, tc.schema, tc.renderedSchema, tc.jsonSchema) |
| 48 | + |
| 49 | + tc.assertValidRequestSchema(t, valid) |
| 50 | + assert.Len(t, errors, tc.expectedErrorsCount) |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
0 commit comments