@@ -261,3 +261,123 @@ func ExampleNewValidator_validateHttpResponse() {
261261 // Output: Type: response, Failure: 200 response body for '/pet/findByStatus' failed to validate schema
262262 // Schema Error: got string, want integer, Line: 19, Col: 27
263263}
264+
265+ func ExampleNewValidator_testResponseHeaders () {
266+ // 1. Load the OpenAPI 3+ spec into a byte array
267+ petstore := []byte (`openapi: "3.0.0"
268+ info:
269+ title: Healthcheck
270+ version: '0.1.0'
271+ paths:
272+ /health:
273+ get:
274+ responses:
275+ '200':
276+ headers:
277+ chicken-nuggets:
278+ description: chicken nuggets response
279+ required: true
280+ schema:
281+ type: integer
282+ description: pet response` )
283+
284+ // 2. Create a new OpenAPI document using libopenapi
285+ document , docErrs := libopenapi .NewDocument (petstore )
286+
287+ if docErrs != nil {
288+ panic (docErrs )
289+ }
290+
291+ // 3. Create a new validator
292+ docValidator , validatorErrs := NewValidator (document )
293+
294+ if validatorErrs != nil {
295+ panic (validatorErrs )
296+ }
297+
298+ // 6. Create a new *http.Request (normally, this would be where the host application will pass in the request)
299+ request , _ := http .NewRequest (http .MethodGet , "/health" , nil )
300+
301+ // 7. Simulate a request/response, in this case the contract returns a 200 with an array of pets.
302+ // Normally, this would be where the host application would pass in the response.
303+ recorder := httptest .NewRecorder ()
304+ handler := func (w http.ResponseWriter , r * http.Request ) {
305+ // set return content type.
306+ w .Header ().Set ("Chicken-Nuggets" , "I am a chicken nugget, and not an integer" )
307+ w .WriteHeader (http .StatusOK )
308+ _ , _ = w .Write (nil )
309+ }
310+
311+ // simulate request/response
312+ handler (recorder , request )
313+
314+ // 7. Validate the response only
315+ valid , validationErrs := docValidator .ValidateHttpResponse (request , recorder .Result ())
316+
317+ if ! valid {
318+ for _ , e := range validationErrs {
319+ // 5. Handle the error
320+ fmt .Printf ("Type: %s, Failure: %s\n " , e .ValidationType , e .Message )
321+ }
322+ }
323+ // Output: Type: response, Failure: header 'chicken-nuggets' failed to validate
324+ }
325+
326+ func ExampleNewValidator_responseHeaderNotRequired () {
327+ // 1. Load the OpenAPI 3+ spec into a byte array
328+ petstore := []byte (`openapi: "3.0.0"
329+ info:
330+ title: Healthcheck
331+ version: '0.1.0'
332+ paths:
333+ /health:
334+ get:
335+ responses:
336+ '200':
337+ headers:
338+ chicken-nuggets:
339+ description: chicken nuggets response
340+ required: false
341+ schema:
342+ type: integer
343+ description: pet response` )
344+
345+ // 2. Create a new OpenAPI document using libopenapi
346+ document , docErrs := libopenapi .NewDocument (petstore )
347+
348+ if docErrs != nil {
349+ panic (docErrs )
350+ }
351+
352+ // 3. Create a new validator
353+ docValidator , validatorErrs := NewValidator (document )
354+
355+ if validatorErrs != nil {
356+ panic (validatorErrs )
357+ }
358+
359+ // 6. Create a new *http.Request (normally, this would be where the host application will pass in the request)
360+ request , _ := http .NewRequest (http .MethodGet , "/health" , nil )
361+
362+ // 7. Simulate a request/response, in this case the contract returns a 200 with an array of pets.
363+ // Normally, this would be where the host application would pass in the response.
364+ recorder := httptest .NewRecorder ()
365+ handler := func (w http.ResponseWriter , r * http.Request ) {
366+ // set return content type.
367+ w .Header ().Set ("Chicken-Nuggets" , "I am a chicken nugget, and not an integer" )
368+ w .WriteHeader (http .StatusOK )
369+ _ , _ = w .Write (nil )
370+ }
371+
372+ // simulate request/response
373+ handler (recorder , request )
374+
375+ // 7. Validate the response only
376+ valid , _ := docValidator .ValidateHttpResponse (request , recorder .Result ())
377+
378+ if ! valid {
379+ panic ("the header is not required, it should not fail" )
380+ }
381+ fmt .Println ("Header is not required, validation passed" )
382+ // Output: Header is not required, validation passed
383+ }
0 commit comments