Skip to content

Commit ae8fb64

Browse files
committed
addressed coverage
1 parent d60690e commit ae8fb64

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

parameters/validate_parameter_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package parameters
22

33
import (
4+
"github.com/pb33f/libopenapi-validator/helpers"
5+
lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3"
46
"net/http"
57
"testing"
68

@@ -192,3 +194,86 @@ func TestHeaderSchemaNoType_AllPoly(t *testing.T) {
192194
assert.Equal(t, "schema 'apiKey' is defined as an boolean and a integer, however it failed to pass a schema validation", valErrs[0].Reason)
193195
assert.Len(t, valErrs[0].SchemaValidationErrors, 3)
194196
}
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+
}

responses/validate_headers_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,50 @@ paths:
8383
assert.Equal(t, errors[0].Message, "header 'chicken-nuggets' failed to validate")
8484
assert.Equal(t, errors[0].Reason, "response header 'chicken-nuggets' is defined as an integer, however it failed to pass a schema validation")
8585
}
86+
87+
func TestValidateResponseHeaders_Valid(t *testing.T) {
88+
spec := `openapi: "3.0.0"
89+
info:
90+
title: Healthcheck
91+
version: '0.1.0'
92+
paths:
93+
/health:
94+
get:
95+
responses:
96+
'200':
97+
headers:
98+
chicken-nuggets:
99+
description: chicken nuggets response
100+
required: false
101+
schema:
102+
type: integer
103+
description: pet response`
104+
105+
doc, _ := libopenapi.NewDocument([]byte(spec))
106+
107+
m, _ := doc.BuildV3Model()
108+
109+
// build a request
110+
request, _ := http.NewRequest(http.MethodGet, "https://things.com/health", nil)
111+
112+
// simulate a request/response
113+
res := httptest.NewRecorder()
114+
handler := func(w http.ResponseWriter, r *http.Request) {
115+
w.Header().Set("Chicken-Cakes", "I should fail")
116+
w.WriteHeader(http.StatusOK)
117+
_, _ = w.Write(nil)
118+
}
119+
120+
// fire the request
121+
handler(res, request)
122+
123+
response := res.Result()
124+
125+
headers := m.Model.Paths.PathItems.GetOrZero("/health").Get.Responses.Codes.GetOrZero("200").Headers
126+
127+
// validate!
128+
valid, errors := ValidateResponseHeaders(request, response, headers)
129+
130+
assert.True(t, valid)
131+
assert.Len(t, errors, 0)
132+
}

0 commit comments

Comments
 (0)