Module: MiniApivore::Validation
- Defined in:
- lib/mini_apivore/validation.rb
Overview
former Validator
Defined Under Namespace
Classes: IndHash
Instance Method Summary collapse
- #action_dispatch_request_args(path, params: {}, headers: {}) ⇒ Object
- #apivore_build_path(path, data) ⇒ Object
- #check_request_path ⇒ Object
- #check_response_is_valid ⇒ Object
- #check_route(verb, path, expected_response_code, params = {}) ⇒ Object
- #check_status_code ⇒ Object
- #failure_message ⇒ Object
- #full_path ⇒ Object
- #has_errors? ⇒ Boolean
- #match? ⇒ Boolean
- #prepare_action_env(verb, path, expected_response_code, params = {}) ⇒ Object
- #prepare_error_backtrace ⇒ Object
- #response_body ⇒ Object
- #swagger_checker ⇒ Object
Instance Method Details
#action_dispatch_request_args(path, params: {}, headers: {}) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/mini_apivore/validation.rb', line 136 def action_dispatch_request_args(path, params: {}, headers: {}) if defined?(ActionPack) && ActionPack::VERSION::MAJOR >= 5 [path, params: params, headers: headers] else [path, params, headers] end end |
#apivore_build_path(path, data) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mini_apivore/validation.rb', line 92 def apivore_build_path(path, data) path.scan(/\{([^\}]*)\}/).each do |param| key = param.first dkey = data && ( data[key] || data[key.to_sym] ) if dkey path = path.gsub "{#{key}}", dkey.to_param.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'].to_param}" : '') end |
#check_request_path ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mini_apivore/validation.rb', line 69 def check_request_path 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, @verb) @errors << "Swagger doc: #{swagger_checker.swagger_path} does not have"\ " a documented @path for #{@verb} #{@path}" elsif !swagger_checker.has_response_code_for_path?(@path, @verb, @expected_response_code) @errors << "Swagger doc: #{swagger_checker.swagger_path} does not have"\ " a documented response code of #{@expected_response_code} at @path"\ " #{@verb} #{@path}. "\ "\n Available response codes: #{swagger_checker.response_codes_for_path(@path, @verb)}" elsif @verb == "get" && swagger_checker.fragment(@path, @verb, @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 ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/mini_apivore/validation.rb', line 117 def check_response_is_valid swagger_errors = swagger_checker.has_matching_document_for( @path, @verb, response.status, response_body ) unless swagger_errors.empty? @errors.concat( swagger_errors.map do |e| e.sub("'#", "'#{full_path}#").gsub( /^The property|in schema.*$/,'' ) end ) end end |
#check_route(verb, path, expected_response_code, params = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/mini_apivore/validation.rb', line 21 def check_route( verb, path, expected_response_code, params = {} ) prepare_action_env( verb, path, expected_response_code, params ) assert( match?, "Failed at: \#{prepare_error_backtrace}\\n\nFailure message: \#{failure_message},\\n \nfullpath: \#{full_path}, \\n \nparams causing failure:\#{params}\n" ) end |
#check_status_code ⇒ Object
110 111 112 113 114 115 |
# File 'lib/mini_apivore/validation.rb', line 110 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 |
#failure_message ⇒ Object
108 |
# File 'lib/mini_apivore/validation.rb', line 108 def ; @errors.join(" ") end |
#full_path ⇒ Object
88 89 90 |
# File 'lib/mini_apivore/validation.rb', line 88 def full_path apivore_build_path(swagger_checker.base_path + @path, @params) end |
#has_errors? ⇒ Boolean
106 |
# File 'lib/mini_apivore/validation.rb', line 106 def has_errors?; !@errors.empty? end |
#match? ⇒ Boolean
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mini_apivore/validation.rb', line 38 def match? #pre_checks check_request_path # request unless has_errors? send( @verb, *action_dispatch_request_args( full_path, params: @params['_data'] || {}, headers: @params['_headers'] || {} ) ) #post_checks check_status_code check_response_is_valid unless has_errors? 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, @verb, @expected_response_code ) end !has_errors? end |
#prepare_action_env(verb, path, expected_response_code, params = {}) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/mini_apivore/validation.rb', line 11 def prepare_action_env(verb, path, expected_response_code, params = {}) @errors = [] @verb = verb.to_s @path = path.to_s @params = IndHash.new(params) @expected_response_code = expected_response_code.to_i end |
#prepare_error_backtrace ⇒ Object
31 32 33 34 35 36 |
# File 'lib/mini_apivore/validation.rb', line 31 def prepare_error_backtrace # it will deliver something like this: #"/app/test/helpers/base_routes_helpers.rb:57:in `__create_card'", #"/app/test/integration/cards_api_test.rb:71:in `block (2 levels) in <class:CommentsApiTest>'", Thread.current.backtrace[2..-1].slice_after{|trc| trc[/check_route/] }.to_a.last[0..1] end |
#response_body ⇒ Object
132 133 134 |
# File 'lib/mini_apivore/validation.rb', line 132 def response_body JSON.parse(response.body) if response.body && !response.body.empty? end |
#swagger_checker ⇒ Object
19 |
# File 'lib/mini_apivore/validation.rb', line 19 def swagger_checker; self.class.swagger_checker end |