@@ -1367,6 +1367,56 @@ components:
13671367 assert .Equal (t , "cannot render circular reference: #/components/schemas/Error" , errors [0 ].Reason )
13681368}
13691369
1370+ func TestValidateBody_CheckHeader (t * testing.T ) {
1371+ spec := `openapi: "3.0.0"
1372+ info:
1373+ title: Healthcheck
1374+ version: '0.1.0'
1375+ paths:
1376+ /health:
1377+ get:
1378+ responses:
1379+ '200':
1380+ headers:
1381+ chicken-nuggets:
1382+ description: chicken nuggets response
1383+ required: true
1384+ schema:
1385+ type: integer
1386+ description: pet response`
1387+
1388+ doc , _ := libopenapi .NewDocument ([]byte (spec ))
1389+
1390+ m , _ := doc .BuildV3Model ()
1391+ v := NewResponseBodyValidator (& m .Model )
1392+
1393+ // build a request
1394+ request , _ := http .NewRequest (http .MethodGet , "https://things.com/health" , nil )
1395+
1396+ // simulate a request/response
1397+ res := httptest .NewRecorder ()
1398+ handler := func (w http.ResponseWriter , r * http.Request ) {
1399+ w .Header ().Set (helpers .ContentTypeHeader , helpers .JSONContentType )
1400+ w .WriteHeader (http .StatusOK )
1401+ _ , _ = w .Write (nil )
1402+ }
1403+
1404+ // fire the request
1405+ handler (res , request )
1406+
1407+ // record response
1408+ response := res .Result ()
1409+
1410+ // validate!
1411+ valid , errors := v .ValidateResponseBody (request , response )
1412+
1413+ assert .False (t , valid )
1414+ assert .Len (t , errors , 1 )
1415+ assert .Equal (t , "Missing required header" , errors [0 ].Message )
1416+ assert .Equal (t , "Required header 'chicken-nuggets' was not found in response" , errors [0 ].Reason )
1417+
1418+ }
1419+
13701420type errorReader struct {}
13711421
13721422func (er * errorReader ) Read (p []byte ) (n int , err error ) {
0 commit comments