class RSpecApib::Element::HttpMessagePayload

Represents a http message payload in api-elements (api-elements.readthedocs.io/en/latest/)

Public Instance Methods

content_type() click to toggle source

The content type if defined else nil @return [String | NilClass] The content type header or nil

# File lib/rspec_apib/elements/http_message_payload.rb, line 10
def content_type
  attributes["headers"] && attributes["headers"]["Content-Type"]
end
validate_schema(request_or_response, allow_no_schema: false) click to toggle source
# File lib/rspec_apib/elements/http_message_payload.rb, line 14
def validate_schema(request_or_response, allow_no_schema: false)
  return [] unless request_or_response.validate_body_with_json_schema?
  schema = body_schema_asset
  return [] if schema.nil? && allow_no_schema
  if schema.nil?
    failure_reason = {
      success: false,
      reason: "Missing a body schema",
      details: []
    }
    return [failure_reason]
  end
  validate_json_schema(schema, request_or_response)
end

Private Instance Methods

body_schema_asset() click to toggle source
# File lib/rspec_apib/elements/http_message_payload.rb, line 44
def body_schema_asset
  content.find { |node| node.is_a?(Asset) && node.meta && node.meta["classes"] && node.meta["classes"].include?("messageBodySchema")}
end
validate_json_schema(schema, request_or_response) click to toggle source
# File lib/rspec_apib/elements/http_message_payload.rb, line 31
def validate_json_schema(schema, request_or_response)
  schema = JSON.parse schema.content
  errors = JSON::Validator.fully_validate(schema, request_or_response.body)
  return [] if errors.length.zero?

  failure_reason = {
    success: false,
    reason: "Schema validation failure",
    details: errors
  }
  [failure_reason]
end