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.
10 11 12 13 14 15 |
# File 'lib/apivore/validator.rb', line 10 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.
8 9 10 |
# File 'lib/apivore/validator.rb', line 8 def expected_response_code @expected_response_code end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
8 9 10 |
# File 'lib/apivore/validator.rb', line 8 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/apivore/validator.rb', line 8 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/apivore/validator.rb', line 8 def path @path end |
Instance Method Details
#apivore_build_path(path, data) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/apivore/validator.rb', line 46 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
130 131 132 |
# File 'lib/apivore/validator.rb', line 130 def app ::Rails.application end |
#check_request_path(swagger_checker) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/apivore/validator.rb', line 68 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/apivore/validator.rb', line 94 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
87 88 89 90 91 92 |
# File 'lib/apivore/validator.rb', line 87 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
125 126 127 |
# File 'lib/apivore/validator.rb', line 125 def description "validate that #{method} #{path} returns #{expected_response_code}" end |
#errors ⇒ Object
121 122 123 |
# File 'lib/apivore/validator.rb', line 121 def errors @errors ||= [] end |
#failure_message ⇒ Object
117 118 119 |
# File 'lib/apivore/validator.rb', line 117 def errors.join(" ") end |
#full_path(swagger_checker) ⇒ Object
42 43 44 |
# File 'lib/apivore/validator.rb', line 42 def full_path(swagger_checker) apivore_build_path(swagger_checker.base_path + path, params) end |
#has_errors? ⇒ Boolean
113 114 115 |
# File 'lib/apivore/validator.rb', line 113 def has_errors? !errors.empty? end |
#matches?(swagger_checker) ⇒ Boolean
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/apivore/validator.rb', line 17 def matches?(swagger_checker) pre_checks(swagger_checker) unless has_errors? send( method, full_path(swagger_checker), params['_data'] || {}, 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
63 64 65 66 |
# File 'lib/apivore/validator.rb', line 63 def post_checks(swagger_checker) check_status_code check_response_is_valid(swagger_checker) unless has_errors? end |
#pre_checks(swagger_checker) ⇒ Object
59 60 61 |
# File 'lib/apivore/validator.rb', line 59 def pre_checks(swagger_checker) check_request_path(swagger_checker) end |
#reset_template_assertion ⇒ Object
Required by rails
135 136 |
# File 'lib/apivore/validator.rb', line 135 def reset_template_assertion end |
#response_body ⇒ Object
109 110 111 |
# File 'lib/apivore/validator.rb', line 109 def response_body JSON.parse(response.body) unless response.body.blank? end |