|
1 | 1 | package parameters
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "github.com/pb33f/libopenapi-validator/helpers" |
| 5 | + lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3" |
4 | 6 | "net/http"
|
5 | 7 | "testing"
|
6 | 8 |
|
@@ -192,3 +194,86 @@ func TestHeaderSchemaNoType_AllPoly(t *testing.T) {
|
192 | 194 | assert.Equal(t, "schema 'apiKey' is defined as an boolean and a integer, however it failed to pass a schema validation", valErrs[0].Reason)
|
193 | 195 | assert.Len(t, valErrs[0].SchemaValidationErrors, 3)
|
194 | 196 | }
|
| 197 | + |
| 198 | +func TestHeaderSchemaStringNoJSON(t *testing.T) { |
| 199 | + bytes := []byte(`{ |
| 200 | + "openapi": "3.0.0", |
| 201 | + "info": { |
| 202 | + "title": "API Spec With Mandatory Header", |
| 203 | + "version": "1.0.0" |
| 204 | + }, |
| 205 | + "paths": { |
| 206 | + "/api-endpoint": { |
| 207 | + "get": { |
| 208 | + "summary": "Restricted API Endpoint", |
| 209 | + |
| 210 | + "responses": { |
| 211 | + "200": { |
| 212 | + "description": "Successful response", |
| 213 | + "headers": { |
| 214 | + "chicken-nuggets": { |
| 215 | + "required": true, |
| 216 | + "schema": { |
| 217 | + "oneOf": [ |
| 218 | + { |
| 219 | + "type": "boolean" |
| 220 | + }, |
| 221 | + { |
| 222 | + "type": "integer" |
| 223 | + } |
| 224 | + ], |
| 225 | + } |
| 226 | + } |
| 227 | + }, |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + }, |
| 233 | + "components": { |
| 234 | + "securitySchemes": { |
| 235 | + "ApiKeyHeader": { |
| 236 | + "type": "apiKey", |
| 237 | + "name": "apiKey", |
| 238 | + "in": "header" |
| 239 | + } |
| 240 | + } |
| 241 | + }, |
| 242 | + "security": [ |
| 243 | + { |
| 244 | + "ApiKeyHeader": [] |
| 245 | + } |
| 246 | + ] |
| 247 | +}`) |
| 248 | + |
| 249 | + doc, err := libopenapi.NewDocument(bytes) |
| 250 | + if err != nil { |
| 251 | + t.Fatalf("error while creating open api spec document: %v", err) |
| 252 | + } |
| 253 | + |
| 254 | + req, err := http.NewRequest("GET", "/api-endpoint", nil) |
| 255 | + if err != nil { |
| 256 | + t.Fatalf("error while creating request: %v", err) |
| 257 | + } |
| 258 | + |
| 259 | + req.Header.Set("Content-Type", "application/json") |
| 260 | + req.Header.Set("apiKey", "headerValue") |
| 261 | + |
| 262 | + v3Model, errs := doc.BuildV3Model() |
| 263 | + if len(errs) > 0 { |
| 264 | + t.Fatalf("error while building v3 model: %v", errs) |
| 265 | + } |
| 266 | + |
| 267 | + v3Model.Model.Servers = nil |
| 268 | + // render the document back to bytes and reload the model. |
| 269 | + _, _, v3Model, _ = doc.RenderAndReload() |
| 270 | + |
| 271 | + headers := v3Model.Model.Paths.PathItems.GetOrZero("/api-endpoint").Get.Responses.Codes.GetOrZero("200").Headers |
| 272 | + headerSchema := headers.GetOrZero("chicken-nuggets").Schema.Schema() |
| 273 | + |
| 274 | + headerErrors := ValidateParameterSchema(headerSchema, nil, "bubbles", "header", |
| 275 | + "response header", "chicken-nuggets", helpers.ResponseBodyValidation, lowv3.HeadersLabel, nil) |
| 276 | + |
| 277 | + assert.Len(t, headerErrors, 1) |
| 278 | + assert.Equal(t, "response header 'chicken-nuggets' is defined as an boolean or integer, however it failed to pass a schema validation", headerErrors[0].Reason) |
| 279 | +} |
0 commit comments