Class: OpenapiRuby::Testing::ResponseValidator
- Inherits:
-
Object
- Object
- OpenapiRuby::Testing::ResponseValidator
- Defined in:
- lib/openapi_ruby/testing/response_validator.rb
Instance Method Summary collapse
-
#initialize(document_hash = nil) ⇒ ResponseValidator
constructor
A new instance of ResponseValidator.
- #validate(response_body:, status_code:, response_context:, content_type: "application/json") ⇒ Object
- #validate_against_document(response_body:, status_code:, path:, method:, content_type: "application/json") ⇒ Object
Constructor Details
#initialize(document_hash = nil) ⇒ ResponseValidator
Returns a new instance of ResponseValidator.
6 7 8 9 |
# File 'lib/openapi_ruby/testing/response_validator.rb', line 6 def initialize(document_hash = nil) @document_hash = document_hash @schemer = nil end |
Instance Method Details
#validate(response_body:, status_code:, response_context:, content_type: "application/json") ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/openapi_ruby/testing/response_validator.rb', line 11 def validate(response_body:, status_code:, response_context:, content_type: "application/json") errors = [] # Validate status code expected = response_context.status_code.to_s actual = status_code.to_s errors << "Expected status #{expected}, got #{actual}" if actual != expected # Validate response body against schema if response_context.schema_definition && response_body schema_errors = validate_schema(response_body, response_context.schema_definition, content_type) errors.concat(schema_errors) end # Validate headers errors end |
#validate_against_document(response_body:, status_code:, path:, method:, content_type: "application/json") ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/openapi_ruby/testing/response_validator.rb', line 29 def validate_against_document(response_body:, status_code:, path:, method:, content_type: "application/json") return [] unless @document_hash errors = [] operation = @document_hash.dig("paths", path, method.to_s.downcase) return ["Operation not found: #{method.upcase} #{path}"] unless operation response_spec = operation.dig("responses", status_code.to_s) return ["Response #{status_code} not documented for #{method.upcase} #{path}"] unless response_spec schema = response_spec.dig("content", content_type, "schema") if schema && response_body schema_errors = validate_schema(response_body, schema, content_type) errors.concat(schema_errors) end errors end |