Validate response headers based on specified header

Add validate_headers step in response validator.
Using JSON::Validator with validate header value with swagger header 
object.
This commit is contained in:
vinhbachsy
2016-10-18 21:45:55 +08:00
parent 10dd37896f
commit 5cf376891a
5 changed files with 79 additions and 1 deletions

View File

@@ -13,6 +13,7 @@ module Rswag
def validate!(response)
validate_code!(response.code)
validate_headers!(response.headers)
validate_body!(response.body)
end
@@ -24,6 +25,20 @@ module Rswag
end
end
def validate_headers!(headers)
header_schema = @api_metadata[:response][:headers]
return if header_schema.nil?
header_schema.each do |header_name, schema|
validate_header!(schema, header_name, headers[header_name.to_s])
end
end
def validate_header!(schema, header_name, header_value)
JSON::Validator.validate!(schema.merge(@global_metadata), header_value.to_json)
rescue JSON::Schema::ValidationError => ex
raise UnexpectedResponse, "Expected response headers #{header_name} to match schema: #{ex.message}"
end
def validate_body!(body)
response_schema = @api_metadata[:response][:schema]
return if response_schema.nil?
@@ -34,7 +49,7 @@ module Rswag
.merge(@global_metadata.slice(:definitions))
JSON::Validator.validate!(validation_schema, body)
rescue JSON::Schema::ValidationError => ex
raise UnexpectedResponse, "Expected response body to match schema: #{ex.message}"
raise UnexpectedResponse, "Expected response body to match schema: #{ex.message}"
end
end
end