Class: Apivore::Validator
- Inherits:
-
Object
- Object
- Apivore::Validator
- Includes:
- ActionDispatch::Integration::Runner
- Defined in:
- lib/apivore/validator.rb
Instance Attribute Summary collapse
-
#expected_response_code ⇒ Object
readonly
Returns the value of attribute expected_response_code.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #apivore_build_path(path, data) ⇒ Object
-
#app ⇒ Object
Required by ActionDispatch::Integration::Runner.
- #check_request_path(swagger_checker) ⇒ Object
- #check_response_is_valid(swagger_checker) ⇒ Object
- #check_status_code ⇒ Object
- #description ⇒ Object
- #errors ⇒ Object
- #failure_message ⇒ Object
- #full_path(swagger_checker) ⇒ Object
- #has_errors? ⇒ Boolean
-
#initialize(method, path, expected_response_code, params = {}) ⇒ Validator
constructor
A new instance of Validator.
- #matches?(swagger_checker) ⇒ Boolean
- #post_checks(swagger_checker) ⇒ Object
- #pre_checks(swagger_checker) ⇒ Object
-
#reset_template_assertion ⇒ Object
Required by rails.
- #response_body ⇒ Object
Constructor Details
#initialize(method, path, expected_response_code, params = {}) ⇒ Validator
Returns a new instance of Validator.
11 12 13 14 15 16 |
# File 'lib/apivore/validator.rb', line 11 def initialize(method, path, expected_response_code, params = {}) @method = method.to_s @path = path.to_s @params = params @expected_response_code = expected_response_code.to_i end |
Instance Attribute Details
#expected_response_code ⇒ Object (readonly)
Returns the value of attribute expected_response_code.
9 10 11 |
# File 'lib/apivore/validator.rb', line 9 def expected_response_code @expected_response_code end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
9 10 11 |
# File 'lib/apivore/validator.rb', line 9 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
9 10 11 |
# File 'lib/apivore/validator.rb', line 9 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/apivore/validator.rb', line 9 def path @path end |
Instance Method Details
#apivore_build_path(path, data) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/apivore/validator.rb', line 49 def apivore_build_path(path, data) path.scan(/\{([^\}]*)\}/).each do |param| key = param.first if data && data[key] path = path.gsub "{#{key}}", data[key].to_s else raise URI::InvalidURIError, "No substitution data found for {#{key}}"\ " to test the path #{path}.", caller end end path + (data['_query_string'] ? "?#{data['_query_string']}" : '') end |
#app ⇒ Object
Required by ActionDispatch::Integration::Runner
133 134 135 |
# File 'lib/apivore/validator.rb', line 133 def app ::Rails.application end |
#check_request_path(swagger_checker) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/apivore/validator.rb', line 71 def check_request_path(swagger_checker) if !swagger_checker.has_path?(path) errors << "Swagger doc: #{swagger_checker.swagger_path} does not have"\ " a documented path for #{path}" elsif !swagger_checker.has_method_at_path?(path, method) errors << "Swagger doc: #{swagger_checker.swagger_path} does not have"\ " a documented path for #{method} #{path}" elsif !swagger_checker.has_response_code_for_path?(path, method, expected_response_code) errors << "Swagger doc: #{swagger_checker.swagger_path} does not have"\ " a documented response code of #{expected_response_code} at path"\ " #{method} #{path}. "\ "\n Available response codes: #{swagger_checker.response_codes_for_path(path, method)}" elsif method == "get" && swagger_checker.fragment(path, method, expected_response_code).nil? errors << "Swagger doc: #{swagger_checker.swagger_path} missing"\ " response model for get request with #{path} for code"\ " #{expected_response_code}" end end |
#check_response_is_valid(swagger_checker) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/apivore/validator.rb', line 97 def check_response_is_valid(swagger_checker) swagger_errors = swagger_checker.has_matching_document_for( path, method, response.status, response_body ) unless swagger_errors.empty? errors.concat( swagger_errors.map do |e| e.sub("'#", "'#{full_path(swagger_checker)}#").gsub( /^The property|in schema.*$/,'' ) end ) end end |
#check_status_code ⇒ Object
90 91 92 93 94 95 |
# File 'lib/apivore/validator.rb', line 90 def check_status_code if response.status != expected_response_code errors << "Path #{path} did not respond with expected status code."\ " Expected #{expected_response_code} got #{response.status}"\ end end |
#description ⇒ Object
128 129 130 |
# File 'lib/apivore/validator.rb', line 128 def description "validate that #{method} #{path} returns #{expected_response_code}" end |
#errors ⇒ Object
124 125 126 |
# File 'lib/apivore/validator.rb', line 124 def errors @errors ||= [] end |
#failure_message ⇒ Object
120 121 122 |
# File 'lib/apivore/validator.rb', line 120 def errors.join(" ") end |
#full_path(swagger_checker) ⇒ Object
45 46 47 |
# File 'lib/apivore/validator.rb', line 45 def full_path(swagger_checker) apivore_build_path(swagger_checker.base_path + path, params) end |
#has_errors? ⇒ Boolean
116 117 118 |
# File 'lib/apivore/validator.rb', line 116 def has_errors? !errors.empty? end |
#matches?(swagger_checker) ⇒ Boolean
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/apivore/validator.rb', line 18 def matches?(swagger_checker) pre_checks(swagger_checker) unless has_errors? send( method, *RailsShim.action_dispatch_request_args( full_path(swagger_checker), params: params['_data'] || {}, headers: params['_headers'] || {} ) ) swagger_checker.response = response post_checks(swagger_checker) if has_errors? && response.body.length > 0 errors << "\nResponse body:\n #{JSON.pretty_generate(JSON.parse(response.body))}" end swagger_checker.remove_tested_end_point_response( path, method, expected_response_code ) end !has_errors? end |
#post_checks(swagger_checker) ⇒ Object
66 67 68 69 |
# File 'lib/apivore/validator.rb', line 66 def post_checks(swagger_checker) check_status_code check_response_is_valid(swagger_checker) unless has_errors? end |
#pre_checks(swagger_checker) ⇒ Object
62 63 64 |
# File 'lib/apivore/validator.rb', line 62 def pre_checks(swagger_checker) check_request_path(swagger_checker) end |
#reset_template_assertion ⇒ Object
Required by rails
138 139 |
# File 'lib/apivore/validator.rb', line 138 def reset_template_assertion end |
#response_body ⇒ Object
112 113 114 |
# File 'lib/apivore/validator.rb', line 112 def response_body JSON.parse(response.body) unless response.body.blank? end |