|
| 1 | +package responses |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/pb33f/libopenapi/datamodel/high/base" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestValidateResponseSchema(t *testing.T) { |
| 15 | + for name, tc := range map[string]struct { |
| 16 | + request *http.Request |
| 17 | + response *http.Response |
| 18 | + schema *base.Schema |
| 19 | + renderedSchema, jsonSchema []byte |
| 20 | + version float32 |
| 21 | + assertValidResponseSchema assert.BoolAssertionFunc |
| 22 | + expectedErrorsCount int |
| 23 | + }{ |
| 24 | + "FailOnBooleanExclusiveMinimum": { |
| 25 | + request: postRequest(), |
| 26 | + response: responseWithBody(`{"exclusiveNumber": 13}`), |
| 27 | + schema: &base.Schema{ |
| 28 | + Type: []string{"object"}, |
| 29 | + }, |
| 30 | + renderedSchema: []byte(`type: object |
| 31 | +properties: |
| 32 | + exclusiveNumber: |
| 33 | + type: number |
| 34 | + description: This number starts its journey where most numbers are too scared to begin! |
| 35 | + exclusiveMinimum: true |
| 36 | + minimum: !!float 10`), |
| 37 | + 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"}`), |
| 38 | + version: 3.1, |
| 39 | + assertValidResponseSchema: assert.False, |
| 40 | + expectedErrorsCount: 1, |
| 41 | + }, |
| 42 | + "PassWithCorrectExclusiveMinimum": { |
| 43 | + request: postRequest(), |
| 44 | + response: responseWithBody(`{"exclusiveNumber": 15}`), |
| 45 | + schema: &base.Schema{ |
| 46 | + Type: []string{"object"}, |
| 47 | + }, |
| 48 | + renderedSchema: []byte(`type: object |
| 49 | +properties: |
| 50 | + exclusiveNumber: |
| 51 | + type: number |
| 52 | + description: This number is properly constrained by a numeric exclusive minimum. |
| 53 | + exclusiveMinimum: 12 |
| 54 | + minimum: 12`), |
| 55 | + jsonSchema: []byte(`{"properties":{"exclusiveNumber":{"type":"number","description":"This number is properly constrained by a numeric exclusive minimum.","exclusiveMinimum":12,"minimum":12}},"type":"object"}`), |
| 56 | + version: 3.1, |
| 57 | + assertValidResponseSchema: assert.True, |
| 58 | + expectedErrorsCount: 0, |
| 59 | + }, |
| 60 | + "PassWithValidStringType": { |
| 61 | + request: postRequest(), |
| 62 | + response: responseWithBody(`{"greeting": "Hello, world!"}`), |
| 63 | + schema: &base.Schema{ |
| 64 | + Type: []string{"object"}, |
| 65 | + }, |
| 66 | + renderedSchema: []byte(`type: object |
| 67 | +properties: |
| 68 | + greeting: |
| 69 | + type: string |
| 70 | + description: A simple greeting |
| 71 | + example: "Hello, world!"`), |
| 72 | + jsonSchema: []byte(`{"properties":{"greeting":{"type":"string","description":"A simple greeting","example":"Hello, world!"}},"type":"object"}`), |
| 73 | + version: 3.1, |
| 74 | + assertValidResponseSchema: assert.True, |
| 75 | + expectedErrorsCount: 0, |
| 76 | + }, |
| 77 | + "PassWithNullablePropertyInOpenAPI30": { |
| 78 | + request: postRequest(), |
| 79 | + response: responseWithBody(`{"name": "John", "middleName": null}`), |
| 80 | + schema: &base.Schema{ |
| 81 | + Type: []string{"object"}, |
| 82 | + }, |
| 83 | + renderedSchema: []byte(`type: object |
| 84 | +properties: |
| 85 | + name: |
| 86 | + type: string |
| 87 | + description: User's first name |
| 88 | + middleName: |
| 89 | + type: string |
| 90 | + nullable: true |
| 91 | + description: User's middle name (optional)`), |
| 92 | + jsonSchema: []byte(`{"properties":{"name":{"type":"string","description":"User's first name"},"middleName":{"type":"string","nullable":true,"description":"User's middle name (optional)"}},"type":"object"}`), |
| 93 | + version: 3.0, |
| 94 | + assertValidResponseSchema: assert.True, |
| 95 | + expectedErrorsCount: 0, |
| 96 | + }, |
| 97 | + "PassWithNullablePropertyInOpenAPI31": { |
| 98 | + request: postRequest(), |
| 99 | + response: responseWithBody(`{"name": "John", "middleName": null}`), |
| 100 | + schema: &base.Schema{ |
| 101 | + Type: []string{"object"}, |
| 102 | + }, |
| 103 | + renderedSchema: []byte(`type: object |
| 104 | +properties: |
| 105 | + name: |
| 106 | + type: string |
| 107 | + description: User's first name |
| 108 | + middleName: |
| 109 | + type: string |
| 110 | + nullable: true |
| 111 | + description: User's middle name (optional)`), |
| 112 | + jsonSchema: []byte(`{"properties":{"name":{"type":"string","description":"User's first name"},"middleName":{"type":"string","nullable":true,"description":"User's middle name (optional)"}},"type":"object"}`), |
| 113 | + version: 3.1, |
| 114 | + assertValidResponseSchema: assert.False, |
| 115 | + expectedErrorsCount: 1, |
| 116 | + }, |
| 117 | + } { |
| 118 | + t.Run(name, func(t *testing.T) { |
| 119 | + t.Parallel() |
| 120 | + |
| 121 | + valid, errors := ValidateResponseSchema(tc.request, tc.response, tc.schema, tc.renderedSchema, tc.jsonSchema, tc.version) |
| 122 | + |
| 123 | + tc.assertValidResponseSchema(t, valid) |
| 124 | + assert.Len(t, errors, tc.expectedErrorsCount) |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func postRequest() *http.Request { |
| 130 | + req, _ := http.NewRequest(http.MethodPost, "/test", io.NopCloser(strings.NewReader(""))) |
| 131 | + return req |
| 132 | +} |
| 133 | + |
| 134 | +func responseWithBody(payload string) *http.Response { |
| 135 | + return &http.Response{ |
| 136 | + StatusCode: http.StatusOK, |
| 137 | + Body: io.NopCloser(bytes.NewReader([]byte(payload))), |
| 138 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestInvalidMin(t *testing.T) { |
| 143 | + renderedSchema := []byte(`type: object |
| 144 | +properties: |
| 145 | + exclusiveNumber: |
| 146 | + type: number |
| 147 | + description: This number starts its journey where most numbers are too scared to begin! |
| 148 | + exclusiveMinimum: true |
| 149 | + minimum: !!float 10`) |
| 150 | + |
| 151 | + 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"}`) |
| 152 | + |
| 153 | + valid, errors := ValidateResponseSchema( |
| 154 | + postRequest(), |
| 155 | + responseWithBody(`{"exclusiveNumber": 13}`), |
| 156 | + &base.Schema{ |
| 157 | + Type: []string{"object"}, |
| 158 | + }, |
| 159 | + renderedSchema, |
| 160 | + jsonSchema, |
| 161 | + 3.1, |
| 162 | + ) |
| 163 | + |
| 164 | + assert.False(t, valid) |
| 165 | + assert.Len(t, errors, 1) |
| 166 | +} |
0 commit comments