Best Airborne code snippet using RequestExpectations.resolve_status
request_expectations.rb
Source:request_expectations.rb
...25 args.push(convert_expectations_for_json_sizes(args.pop))26 expect_json_types(*args)27 end28 def expect_status(code)29 expect(response.code).to eq(resolve_status(code, response.code))30 end31 def expect_header(key, content)32 expect_header_impl(key, content)33 end34 def expect_header_contains(key, content)35 expect_header_impl(key, content, true)36 end37 def optional(hash)38 OptionalHashTypeExpectations.new(hash)39 end40 def regex(reg)41 Regexp.new(reg)42 end43 def date44 ->(value) { yield DateTime.parse(value) }45 end46 private47 def expect_header_impl(key, content, contains = nil)48 header = headers[key]49 if header50 if contains51 expect(header.downcase).to include(content.downcase)52 else53 expect(header.downcase).to eq(content.downcase)54 end55 else56 fail RSpec::Expectations::ExpectationNotMetError, "Header #{key} not present in the HTTP response"57 end58 end59 def expect_json_impl(expected, actual)60 return if nil_optional_hash?(expected, actual)61 actual = actual.to_s if expected.is_a?(Regexp)62 return expect(actual).to match(expected) if property?(expected)63 keys = []64 keys << expected.keys if match_expected?65 keys << actual.keys if match_actual?66 keys = expected.keys & actual.keys if match_none?67 keys.flatten.uniq.each do |prop|68 expected_value = extract_expected_value(expected, prop)69 actual_value = extract_actual(actual, prop)70 next expect_json_impl(expected_value, actual_value) if hash?(expected_value)71 next expected_value.call(actual_value) if expected_value.is_a?(Proc)72 next expect(actual_value.to_s).to match(expected_value) if expected_value.is_a?(Regexp)73 expect(actual_value).to eq(expected_value)74 end75 end76 def expect_json_types_impl(expected, actual)77 return if nil_optional_hash?(expected, actual)78 @mapper ||= get_mapper79 actual = convert_to_date(actual) if expected == :date80 return expect_type(expected, actual.class) if expected.is_a?(Symbol)81 return expected.call(actual) if expected.is_a?(Proc)82 keys = []83 keys << expected.keys if match_expected?84 keys << actual.keys if match_actual?85 keys = expected.keys & actual.keys if match_none?86 keys.flatten.uniq.each do |prop|87 type = extract_expected_type(expected, prop)88 value = extract_actual(actual, prop)89 value = convert_to_date(value) if type == :date90 next expect_json_types_impl(type, value) if hash?(type)91 next type.call(value) if type.is_a?(Proc)92 val_class = value.class93 type_string = type.to_s94 if type_string.include?('array_of') && !(type_string.include?('or_null') && value.nil?)95 check_array_types(value, val_class, prop, type)96 else97 expect_type(type, val_class, prop)98 end99 end100 end101 def call_with_path(args)102 if args.length == 2103 get_by_path(args[0], json_body) do |json_chunk|104 yield(args[1], json_chunk)105 end106 else107 yield(args[0], json_body)108 end109 end110 def extract_expected_value(expected, prop)111 begin112 raise unless expected.keys.include?(prop)113 expected[prop]114 rescue115 raise ExpectationError, "Expectation is expected to contain property: #{prop}"116 end117 end118 def extract_expected_type(expected, prop)119 begin120 type = expected[prop]121 type.nil? ? raise : type122 rescue123 raise ExpectationError, "Expectation is expected to contain property: #{prop}"124 end125 end126 def extract_actual(actual, prop)127 begin128 value = actual[prop]129 rescue130 raise ExpectationError, "Expected #{actual.class} #{actual}\nto be an object with property #{prop}"131 end132 end133 def expect_type(expected_type, value_class, prop_name = nil)134 fail ExpectationError, "Expected type #{expected_type}\nis an invalid type" if @mapper[expected_type].nil?135 insert = prop_name.nil? ? '' : "#{prop_name} to be of type"136 message = "Expected #{insert} #{expected_type}\n got #{value_class} instead"137 expect(@mapper[expected_type].include?(value_class)).to eq(true), message138 end139 def convert_to_date(value)140 begin141 DateTime.parse(value)142 rescue143 end144 end145 def check_array_types(value, value_class, prop_name, expected_type)146 expect_array(value_class, prop_name, expected_type)147 value.each do |val|148 expect_type(expected_type, val.class, prop_name)149 end150 end151 def nil_optional_hash?(expected, hash)152 expected.is_a?(Airborne::OptionalHashTypeExpectations) && hash.nil?153 end154 def hash?(hash)155 hash.is_a?(Hash) || hash.is_a?(Airborne::OptionalHashTypeExpectations)156 end157 def expect_array(value_class, prop_name, expected_type)158 expect(value_class).to eq(Array), "Expected #{prop_name}\n to be of type #{expected_type}\n got #{value_class} instead"159 end160 def convert_expectations_for_json_sizes(old_expectations)161 unless old_expectations.is_a?(Hash)162 return convert_expectation_for_json_sizes(old_expectations)163 end164 old_expectations.each_with_object({}) do |(prop_name, expected_size), memo|165 new_value = if expected_size.is_a?(Hash)166 convert_expectations_for_json_sizes(expected_size)167 else168 convert_expectation_for_json_sizes(expected_size)169 end170 memo[prop_name] = new_value171 end172 end173 def convert_expectation_for_json_sizes(expected_size)174 ->(data) { expect(data.size).to eq(expected_size) }175 end176 def ensure_hash_contains_prop(prop_name, hash)177 begin178 yield179 rescue180 raise ExpectationError, "Expected #{hash.class} #{hash}\nto be an object with property #{prop_name}"181 end182 end183 def property?(expectations)184 [String, Regexp, Float, Fixnum, Bignum, TrueClass, FalseClass, NilClass, Array].include?(expectations.class)185 end186 def get_mapper187 base_mapper = {188 integer: [Fixnum, Bignum],189 array_of_integers: [Fixnum, Bignum],190 int: [Fixnum, Bignum],191 array_of_ints: [Fixnum, Bignum],192 float: [Float, Fixnum, Bignum],193 array_of_floats: [Float, Fixnum, Bignum],194 string: [String],195 array_of_strings: [String],196 boolean: [TrueClass, FalseClass],197 array_of_booleans: [TrueClass, FalseClass],198 bool: [TrueClass, FalseClass],199 array_of_bools: [TrueClass, FalseClass],200 object: [Hash],201 array_of_objects: [Hash],202 array: [Array],203 array_of_arrays: [Array],204 date: [DateTime],205 null: [NilClass]206 }207 mapper = base_mapper.clone208 base_mapper.each do |key, value|209 mapper[(key.to_s + '_or_null').to_sym] = value + [NilClass]210 end211 mapper212 end213 def resolve_status(candidate, authority)214 candidate = Rack::Utils::SYMBOL_TO_STATUS_CODE[candidate] if candidate.is_a?(Symbol)215 case authority216 when String then candidate.to_s217 when Fixnum then candidate.to_i218 else candidate219 end220 end221 def match_none?222 !match_actual? && !match_expected?223 end224 def match_actual?225 Airborne.configuration.match_actual?226 end227 def match_expected?...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!