@@ -158,6 +158,12 @@ func ValidateResponseSchema(input *ValidateResponseSchemaInput) (bool, []*errors
158158 schema := input .Schema
159159
160160 if response == nil || response .Body == http .NoBody {
161+
162+ // skip response body validation for head request after processing schema
163+ if request != nil && request .Method == http .MethodHead {
164+ return true , validationErrors
165+ }
166+
161167 // cannot decode the response body, so it's not valid
162168 violation := & errors.SchemaValidationFailure {
163169 Reason : "response is empty" ,
@@ -210,6 +216,28 @@ func ValidateResponseSchema(input *ValidateResponseSchemaInput) (bool, []*errors
210216 var decodedObj interface {}
211217
212218 if len (responseBody ) > 0 {
219+ // Per RFC7231, a response to a HEAD request MUST NOT include a message body.
220+ if request != nil && request .Method == http .MethodHead {
221+ violation := & errors.SchemaValidationFailure {
222+ Reason : "HEAD responses must not include a message body" ,
223+ Location : "response body" ,
224+ ReferenceObject : string (responseBody ),
225+ ReferenceSchema : referenceSchema ,
226+ }
227+ validationErrors = append (validationErrors , & errors.ValidationError {
228+ ValidationType : helpers .ResponseBodyValidation ,
229+ ValidationSubType : helpers .Schema ,
230+ Message : fmt .Sprintf ("%s response for '%s' must not include a body" ,
231+ request .Method , request .URL .Path ),
232+ Reason : "The response to a HEAD request must not contain a body" ,
233+ SpecLine : 1 ,
234+ SpecCol : 0 ,
235+ SchemaValidationErrors : []* errors.SchemaValidationFailure {violation },
236+ HowToFix : "ensure no response body is present for HEAD requests" ,
237+ Context : referenceSchema ,
238+ })
239+ return false , validationErrors
240+ }
213241 err := json .Unmarshal (responseBody , & decodedObj )
214242 if err != nil {
215243 // cannot decode the response body, so it's not valid
0 commit comments