Class: ApiMiniTester::TestStep
- Inherits:
-
Object
- Object
- ApiMiniTester::TestStep
- Includes:
- HTTParty
- Defined in:
- lib/api_mini_tester/test_step.rb
Constant Summary collapse
- SUPPORTED_METHODS =
%i[ get post put delete ].freeze
- SUPPORTED_RANDOM_DISTRIBUTION =
%w[ static norm uniform ].freeze
Instance Attribute Summary collapse
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#input ⇒ Object
Returns the value of attribute input.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#name ⇒ Object
Returns the value of attribute name.
-
#output ⇒ Object
Returns the value of attribute output.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#sleeps ⇒ Object
readonly
Returns the value of attribute sleeps.
-
#timing ⇒ Object
Returns the value of attribute timing.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #add_result(section, result) ⇒ Object
- #array_diff(a, b, path = nil, section = :body) ⇒ Object
- #assert_body(response, output) ⇒ Object
- #assert_headers(response, output) ⇒ Object
- #assert_status(response, output) ⇒ Object
- #assert_timing(runtime, limit = nil) ⇒ Object
- #body ⇒ Object
- #body_to_form_data ⇒ Object
- #content_type ⇒ Object
- #hash_diff(a, b, path = nil, section = :body) ⇒ Object
- #headers ⇒ Object
-
#initialize(base_uri, step, context = nil, data = nil, defaults = nil, debug = false) ⇒ TestStep
constructor
A new instance of TestStep.
- #log_debug(request, response, expectations) ⇒ Object
- #print_results ⇒ Object
- #raw_body ⇒ Object
- #run_asserts(response) ⇒ Object
- #run_step ⇒ Object
- #step_sleep(params) ⇒ Object
- #test_body ⇒ Object
- #test_headers ⇒ Object
- #test_status ⇒ Object
- #test_timing ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(base_uri, step, context = nil, data = nil, defaults = nil, debug = false) ⇒ TestStep
Returns a new instance of TestStep.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/api_mini_tester/test_step.rb', line 20 def initialize(base_uri, step, context = nil, data = nil, defaults = nil, debug = false) @debug = debug if debug @debug_output = File.open(ApiMiniTester::TestSuite::DEBUG_FILE, 'a') end step = step.deep_merge!(defaults) do |key, this_val, other_val| if this_val.nil? other_val elsif this_val.is_a?(Array) (this_val + other_val) else this_val end end if defaults Liquid::Template.register_filter(::TestFakerFilter) @context = context uri_template = Liquid::Template.parse([base_uri, step['uri']].join("/"), error_mode: :strict) @name = step['name'] @sleeps = step['sleep'] @uri = uri_template.render( {'context' => context, 'data' => data}, { strict_variables: true }) @method = step['method'].downcase.to_sym input_template = Liquid::Template.parse(step['input'].to_yaml.to_s, error_mode: :strict) @input = YAML.load( input_template.render({'context' => context, 'data' => data}, { strict_variables: true })) output_template = Liquid::Template.parse(step['output'].to_yaml.to_s, error_mode: :strict) @output = YAML.load( output_template.render({'context' => context, 'data' => data}, { strict_variables: true })) @results = { name: step['name'], desc: step['desc'], status: [], headers: [], body: [], url: [], method: [], timing: [] } end |
Instance Attribute Details
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
18 19 20 |
# File 'lib/api_mini_tester/test_step.rb', line 18 def debug @debug end |
#input ⇒ Object
Returns the value of attribute input.
17 18 19 |
# File 'lib/api_mini_tester/test_step.rb', line 17 def input @input end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
18 19 20 |
# File 'lib/api_mini_tester/test_step.rb', line 18 def method @method end |
#name ⇒ Object
Returns the value of attribute name.
17 18 19 |
# File 'lib/api_mini_tester/test_step.rb', line 17 def name @name end |
#output ⇒ Object
Returns the value of attribute output.
17 18 19 |
# File 'lib/api_mini_tester/test_step.rb', line 17 def output @output end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
18 19 20 |
# File 'lib/api_mini_tester/test_step.rb', line 18 def results @results end |
#sleeps ⇒ Object (readonly)
Returns the value of attribute sleeps.
18 19 20 |
# File 'lib/api_mini_tester/test_step.rb', line 18 def sleeps @sleeps end |
#timing ⇒ Object
Returns the value of attribute timing.
17 18 19 |
# File 'lib/api_mini_tester/test_step.rb', line 17 def timing @timing end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
18 19 20 |
# File 'lib/api_mini_tester/test_step.rb', line 18 def uri @uri end |
Instance Method Details
#add_result(section, result) ⇒ Object
173 174 175 |
# File 'lib/api_mini_tester/test_step.rb', line 173 def add_result(section, result) @results[section] << result end |
#array_diff(a, b, path = nil, section = :body) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/api_mini_tester/test_step.rb', line 228 def array_diff(a, b, path = nil, section = :body) a.each do |a_item| if b.nil? add_result section, { result: false, name: "Response boby value: #{[path].join(".")}", desc: "Assert #{[path].join(".")} is empty" } elsif a_item.instance_of?(Hash) found = false b.each do |b_item| matching = true a_item.each_key do |k, v| matching = (b_item[k] == a_item[k]) if matching end found = true if matching end add_result section, { result: found, name: "Response body value: #{[path].join(".")}", desc: "Assert #{[path].join(".")} #{found ? 'contains' : 'does not contains'} #{a_item}" } elsif a_item.instance_of?(Array) # TODO: Add support for array of array it isn't so needed to compate so deep structures else add_result section, { result: b.include?(a_item), name: "Response boby value: #{[path].join(".")}", desc: "Assert #{[path].join(".")} #{b.include?(a_item) ? 'contains' : 'does not contains'} #{a_item}" } end end end |
#assert_body(response, output) ⇒ Object
220 221 222 223 224 225 226 |
# File 'lib/api_mini_tester/test_step.rb', line 220 def assert_body(response, output) if output.instance_of?(Hash) hash_diff(output, response) elsif output.instance_of?(Array) array_diff(output, response) end end |
#assert_headers(response, output) ⇒ Object
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/api_mini_tester/test_step.rb', line 209 def assert_headers(response, output) return if output.nil? output.each do |k, v| add_result :headers, { result: (v == response[k]), name: "Header value: #{k} == #{v}", desc: "Header #{k} expected: #{v}, got #{response[k]}", exp: v, real: response[k] } end end |
#assert_status(response, output) ⇒ Object
202 203 204 205 206 207 |
# File 'lib/api_mini_tester/test_step.rb', line 202 def assert_status(response, output) add_result :status, { result: (response == output), name: "Response code == #{output}", desc: "Expected response #{output}, got response #{response}", exp: output, real: response } end |
#assert_timing(runtime, limit = nil) ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/api_mini_tester/test_step.rb', line 194 def assert_timing(runtime, limit = nil) limit ||= Float::INFINITY add_result :timing, { result: (runtime < limit), name: "Request time < #{limit}", desc: "Expected request time #{limit}, real time #{runtime}", exp: limit, real: runtime } end |
#body ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/api_mini_tester/test_step.rb', line 77 def body case content_type when 'application/x-www-form-urlencoded' body_to_form_data when 'multipart/form-data' body_to_form_data else @input["body"].to_json end end |
#body_to_form_data ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/api_mini_tester/test_step.rb', line 92 def body_to_form_data body = {} @input["body"].each do |item| body[item['name']] = item['value'] if item['type'] == 'input' body[item['name']] = File.open(item['value'], 'r') if item['type'] == 'file' end body end |
#content_type ⇒ Object
67 68 69 |
# File 'lib/api_mini_tester/test_step.rb', line 67 def content_type @input['content_type'] || 'application/json' end |
#hash_diff(a, b, path = nil, section = :body) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/api_mini_tester/test_step.rb', line 256 def hash_diff(a, b, path = nil, section = :body) return nil if a.nil? || b.nil? a.each_key do |k, v| current_path = [path, k].join('.') if b[k].nil? add_result section, { result: false, name: "Reponse value: #{[path, k].join(".")}", desc: "Missing #{current_path}" } elsif v.instance_of?(Hash) hash_diff(a[k], b[k], current_path, section) elsif v.instance_of?(Array) array_diff(a[k], b[k], current_path, section) else add_result section, { result: (a[k] == b[k]), name: "Reponse body value: #{[path, k].join(".")}", desc: "Assert #{[path, k].join(".")}: #{a[k]} #{a[k] == b[k] ? '==' : '!='} #{b[k]}" } end end end |
#headers ⇒ Object
71 72 73 74 75 |
# File 'lib/api_mini_tester/test_step.rb', line 71 def headers @input['header'] = {} unless @input['header'] @input['header']['Content-Type'] = content_type if content_type == 'application/json' @input['header'] end |
#log_debug(request, response, expectations) ⇒ Object
162 163 164 165 |
# File 'lib/api_mini_tester/test_step.rb', line 162 def log_debug(request, response, expectations) log = { uri: uri, method: method, request: request, response: response, expectations: expectations } @debug_output.puts log.to_json end |
#print_results ⇒ Object
167 168 169 170 171 |
# File 'lib/api_mini_tester/test_step.rb', line 167 def print_results @results.each do |line| puts line end end |
#raw_body ⇒ Object
88 89 90 |
# File 'lib/api_mini_tester/test_step.rb', line 88 def raw_body @input["body"].to_hash end |
#run_asserts(response) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/api_mini_tester/test_step.rb', line 155 def run_asserts(response) assert_status(response.code, test_status) assert_headers(response.headers, test_headers) assert_body(response.parsed_response, test_body) assert_timing(timing, test_timing) end |
#run_step ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/api_mini_tester/test_step.rb', line 117 def run_step if sleeps step_sleep(sleeps['before']) if sleeps['before'] end @timing = Time.now case method when :get response = HTTParty.get(uri, headers: headers) when :post response = HTTParty.post(uri, headers: headers, body: body) when :put response = HTTParty.put(uri, headers: headers, body: body) when :delete response = HTTParty.delete(uri, headers: headers) when :patch response = HTTParty.patch(uri, headers: headers, body: body) else raise "Unknown HTTP method: #{method}" end @timing = Time.now - @timing if sleeps step_sleep(sleeps['after']) if sleeps['after'] end add_result :url, { result: true, desc: "Url: #{uri}" } add_result :method, { result: true, desc: "Method: #{method}" } log_debug({ headers: headers, body: raw_body }, { headers: response.headers, body: response.parsed_response, code: response.code }, output) if debug run_asserts(response) [ results, response ] end |
#step_sleep(params) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/api_mini_tester/test_step.rb', line 177 def step_sleep(params) params['distribution'] = 'static' unless SUPPORTED_RANDOM_DISTRIBUTION.include?(params['distribution']) t = begin case params['distribution'] when 'static' params['value'] when 'norm' Distribution::Normal.rng(params['mean'], params['sigma'], srand).call when 'uniform' Random.new.rand(params['max'] - params['min']) + params['min'] end end puts "Sleeping for #{t} seconds" sleep(t.to_f.abs) if t end |
#test_body ⇒ Object
105 106 107 |
# File 'lib/api_mini_tester/test_step.rb', line 105 def test_body @output['body'] end |
#test_headers ⇒ Object
101 102 103 |
# File 'lib/api_mini_tester/test_step.rb', line 101 def test_headers @output['header'] end |
#test_status ⇒ Object
109 110 111 |
# File 'lib/api_mini_tester/test_step.rb', line 109 def test_status @output['status'] end |
#test_timing ⇒ Object
113 114 115 |
# File 'lib/api_mini_tester/test_step.rb', line 113 def test_timing @output['timing'] end |
#valid? ⇒ Boolean
57 58 59 60 61 62 63 64 65 |
# File 'lib/api_mini_tester/test_step.rb', line 57 def valid? return false if uri.nil? || uri.empty? return false unless URI.parse(uri) rescue false return false unless SUPPORTED_METHODS.include? method return false if @name.nil? || @name.empty? true end |