|
| 1 | +#---------------------------------------------------------------------------------------------- |
| 2 | +# response_validation_helper.rb: |
| 3 | +# |
| 4 | +# this is an rspec utility to allow validation of responses against the swagger schema generated |
| 5 | +# from the Apipie 'returns' definition for the call. |
| 6 | +# |
| 7 | +# |
| 8 | +# to use this file in a controller rspec you should |
| 9 | +# require 'apipie/rspec/response_validation_helper' in the spec file |
| 10 | +# |
| 11 | +# |
| 12 | +# this utility provides two mechanisms: matcher-based validation and auto-validation |
| 13 | +# |
| 14 | +# matcher-based: an rspec matcher allowing 'expect(response).to match_declared_responses' |
| 15 | +# auto-validation: all responses returned from 'get', 'post', etc. are automatically tested |
| 16 | +# |
| 17 | +# =================================== |
| 18 | +# Matcher-based validation - example |
| 19 | +# =================================== |
| 20 | +# Assume the file 'my_controller_spec.rb': |
| 21 | +# |
| 22 | +# require 'apipie/rspec/response_validation_helper' |
| 23 | +# |
| 24 | +# RSpec.describe MyController, :type => :controller, :show_in_doc => true do |
| 25 | +# |
| 26 | +# describe "GET stuff with response validation" do |
| 27 | +# render_views # this makes sure the 'get' operation will actually |
| 28 | +# # return the rendered view even though this is a Controller spec |
| 29 | +# |
| 30 | +# it "does something" do |
| 31 | +# response = get :index, {format: :json} |
| 32 | +# |
| 33 | +# # the following expectation will fail if the returned object |
| 34 | +# # does not match the 'returns' declaration in the Controller, |
| 35 | +# # or if there is no 'returns' declaration for the returned |
| 36 | +# # HTTP status code |
| 37 | +# expect(response).to match_declared_responses |
| 38 | +# end |
| 39 | +# end |
| 40 | +# |
| 41 | +# |
| 42 | +# =================================== |
| 43 | +# Auto-validation |
| 44 | +# =================================== |
| 45 | +# To use auto-validation, at the beginning of the block in which you want to turn on validation: |
| 46 | +# -) turn on view rendering (by stating 'render_views') |
| 47 | +# -) turn on response validation by stating 'auto_validate_rendered_views' |
| 48 | +# |
| 49 | +# For example, assume the file 'my_controller_spec.rb': |
| 50 | +# |
| 51 | +# require 'apipie/rspec/response_validation_helper' |
| 52 | +# |
| 53 | +# RSpec.describe MyController, :type => :controller, :show_in_doc => true do |
| 54 | +# |
| 55 | +# describe "GET stuff with response validation" do |
| 56 | +# render_views |
| 57 | +# auto_validate_rendered_views |
| 58 | +# |
| 59 | +# it "does something" do |
| 60 | +# get :index, {format: :json} |
| 61 | +# end |
| 62 | +# it "does something else" do |
| 63 | +# get :another_index, {format: :json} |
| 64 | +# end |
| 65 | +# end |
| 66 | +# |
| 67 | +# describe "GET stuff without response validation" do |
| 68 | +# it "does something" do |
| 69 | +# get :index, {format: :json} |
| 70 | +# end |
| 71 | +# it "does something else" do |
| 72 | +# get :another_index, {format: :json} |
| 73 | +# end |
| 74 | +# end |
| 75 | +# |
| 76 | +# |
| 77 | +# Once this is done, responses from http operations ('get', 'post', 'delete', etc.) |
| 78 | +# will fail the test if the response structure does not match the 'returns' declaration |
| 79 | +# on the method (for the actual HTTP status code), or if there is no 'returns' declaration |
| 80 | +# for the HTTP status code. |
| 81 | +#---------------------------------------------------------------------------------------------- |
| 82 | + |
| 83 | + |
| 84 | +#---------------------------------------------------------------------------------------------- |
| 85 | +# Response validation: core logic (used by auto-validation and manual-validation mechanisms) |
| 86 | +#---------------------------------------------------------------------------------------------- |
| 87 | +class ActionController::Base |
| 88 | + module Apipie::ControllerValidationHelpers |
| 89 | + # this method is injected into ActionController::Base in order to |
| 90 | + # get access to the names of the current controller, current action, as well as to the response |
| 91 | + def schema_validation_errors_for_response |
| 92 | + unprocessed_schema = Apipie::json_schema_for_method_response(controller_name, action_name, response.code, true) |
| 93 | + |
| 94 | + if unprocessed_schema.nil? |
| 95 | + err = "no schema defined for #{controller_name}##{action_name}[#{response.code}]" |
| 96 | + return [nil, [err], RuntimeError.new(err)] |
| 97 | + end |
| 98 | + |
| 99 | + schema = JSON.parse(JSON(unprocessed_schema)) |
| 100 | + |
| 101 | + error_list = JSON::Validator.fully_validate(schema, response.body, :strict => false, :version => :draft4, :json => true) |
| 102 | + |
| 103 | + error_object = Apipie::ResponseDoesNotMatchSwaggerSchema.new(controller_name, action_name, response.code, error_list, schema, response.body) |
| 104 | + |
| 105 | + [schema, error_list, error_object] |
| 106 | + end |
| 107 | + |
| 108 | + end |
| 109 | + |
| 110 | + include Apipie::ControllerValidationHelpers |
| 111 | +end |
| 112 | + |
| 113 | +module Apipie |
| 114 | + def self.print_validation_errors(validation_errors, schema, response, error_object=nil) |
| 115 | + Rails.logger.warn(validation_errors.to_s) |
| 116 | + if Rails.env.test? |
| 117 | + puts "schema validation errors:" |
| 118 | + validation_errors.each { |e| puts "--> #{e.to_s}" } |
| 119 | + puts "schema: #{schema.nil? ? '<none>' : JSON(schema)}" |
| 120 | + puts "response: #{response.body}" |
| 121 | + raise error_object if error_object |
| 122 | + end |
| 123 | + end |
| 124 | +end |
| 125 | + |
| 126 | +#--------------------------------- |
| 127 | +# Manual-validation (RSpec matcher) |
| 128 | +#--------------------------------- |
| 129 | +RSpec::Matchers.define :match_declared_responses do |
| 130 | + match do |actual| |
| 131 | + (schema, validation_errors) = subject.send(:schema_validation_errors_for_response) |
| 132 | + valid = (validation_errors == []) |
| 133 | + Apipie::print_validation_errors(validation_errors, schema, response) unless valid |
| 134 | + |
| 135 | + valid |
| 136 | + end |
| 137 | +end |
| 138 | + |
| 139 | + |
| 140 | +#--------------------------------- |
| 141 | +# Auto-validation logic |
| 142 | +#--------------------------------- |
| 143 | +module RSpec::Rails::ViewRendering |
| 144 | + # Augment the RSpec DSL |
| 145 | + module ClassMethods |
| 146 | + def auto_validate_rendered_views |
| 147 | + before do |
| 148 | + @is_response_validation_on = true |
| 149 | + end |
| 150 | + |
| 151 | + after do |
| 152 | + @is_response_validation_on = false |
| 153 | + end |
| 154 | + end |
| 155 | + end |
| 156 | +end |
| 157 | + |
| 158 | + |
| 159 | +ActionController::TestCase::Behavior.instance_eval do |
| 160 | + # instrument the 'process' method in ActionController::TestCase to enable response validation |
| 161 | + module Apipie::ResponseValidationHelpers |
| 162 | + @is_response_validation_on = false |
| 163 | + def process(*args) |
| 164 | + result = super(*args) |
| 165 | + validate_response if @is_response_validation_on |
| 166 | + |
| 167 | + result |
| 168 | + end |
| 169 | + |
| 170 | + def validate_response |
| 171 | + controller.send(:validate_response_and_abort_with_info_if_errors) |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + prepend Apipie::ResponseValidationHelpers |
| 176 | +end |
| 177 | + |
| 178 | + |
| 179 | +class ActionController::Base |
| 180 | + module Apipie::ControllerValidationHelpers |
| 181 | + def validate_response_and_abort_with_info_if_errors |
| 182 | + |
| 183 | + (schema, validation_errors, error_object) = schema_validation_errors_for_response |
| 184 | + |
| 185 | + valid = (validation_errors == []) |
| 186 | + if !valid |
| 187 | + Apipie::print_validation_errors(validation_errors, schema, response, error_object) |
| 188 | + end |
| 189 | + end |
| 190 | + end |
| 191 | +end |
| 192 | + |
| 193 | + |
0 commit comments