Module: Apipie::ControllerValidationHelpers

Included in:
ActionController::Base
Defined in:
lib/apipie/rspec/response_validation_helper.rb,
lib/apipie/rspec/response_validation_helper.rb

Instance Method Summary collapse

Instance Method Details

#schema_validation_errors_for_responseObject

this method is injected into ActionController::Base in order to get access to the names of the current controller, current action, as well as to the response



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/apipie/rspec/response_validation_helper.rb', line 91

def schema_validation_errors_for_response
  unprocessed_schema = Apipie::json_schema_for_method_response(controller_name, action_name, response.code, true)

  if unprocessed_schema.nil?
    err = "no schema defined for #{controller_name}##{action_name}[#{response.code}]"
    return [nil, [err], RuntimeError.new(err)]
  end

  schema = JSON.parse(JSON(unprocessed_schema))

  error_list = JSON::Validator.fully_validate(schema, response.body, :strict => false, :version => :draft4, :json => true)

  error_object = Apipie::ResponseDoesNotMatchSwaggerSchema.new(controller_name, action_name, response.code, error_list, schema, response.body)

  [schema, error_list, error_object]
rescue Apipie::NoDocumentedMethod
  [nil, [], nil]
end

#validate_response_and_abort_with_info_if_errorsObject



182
183
184
185
186
187
188
189
190
# File 'lib/apipie/rspec/response_validation_helper.rb', line 182

def validate_response_and_abort_with_info_if_errors

  (schema, validation_errors, error_object) = schema_validation_errors_for_response

  valid = (validation_errors == [])
  if !valid
    Apipie::print_validation_errors(validation_errors, schema, response, error_object)
  end
end