How to use optional method of RequestExpectations Package

Best Airborne code snippet using RequestExpectations.optional

spaceborne.rb

Source:spaceborne.rb Github

copy

Full Screen

...130 end131 end132 # Extend airborne's expectations133 module RequestExpectations134 # class used for holding an optional path135 class OptionalPathExpectations136 def initialize(string)137 @string = string138 end139 def to_s140 @string.to_s141 end142 end143 def do_process_json(part, json)144 json = process_json(part, json)145 rescue StandardError146 raise PathError,147 "Expected #{json.class}\nto be an object with property #{part}"148 end149 def call_with_relative_path(data, args)150 if args.length == 2151 get_by_path(args[0], data) do |json_chunk|152 yield(args[1], json_chunk)153 end154 else155 yield(args[0], data)156 end157 end158 def exception_path_adder(args, body)159 yield160 rescue RSpec::Expectations::ExpectationNotMetError,161 ExpectationError,162 Airborne::ExpectationError => e163 e.message << "\nexpect arguments: #{args}\ndata element: #{body}"164 raise e165 end166 def expect_json_types(*args)167 call_with_relative_path(json_body, args) do |param, body|168 exception_path_adder(args, body) do169 expect_json_types_impl(param, body)170 end171 end172 end173 def expect_json(*args)174 call_with_relative_path(json_body, args) do |param, body|175 exception_path_adder(args, body) do176 expect_json_impl(param, body)177 end178 end179 end180 def expect_header_types(*args)181 call_with_relative_path(response.headers, args) do |param, body|182 exception_path_adder(args, body) do183 expect_json_types_impl(param, body)184 end185 end186 end187 def expect_header(*args)188 call_with_relative_path(response.headers, args) do |param, body|189 exception_path_adder(args, body) do190 expect_json_impl(param, body)191 end192 end193 end194 def optional(data)195 if data.is_a?(Hash)196 OptionalHashTypeExpectations.new(data)197 else198 Airborne::OptionalPathExpectations.new(data)199 end200 end201 end202 # extension to handle hash value checking203 module PathMatcher204 def handle_container(path, json, &block)205 case json.class.name206 when 'Array'207 expect_all(json, &block)208 when 'Hash'209 json.each { |k, _v| yield json[k] }210 else211 raise ExpectationError, "expected array or hash at #{path}, got #{json.class.name}"212 end213 end214 def handle_type(type, path, json, &block)215 case type.to_s216 when '*'217 handle_container(path, json, &block)218 when '?'219 expect_one(path, json, &block)220 else221 yield json222 end223 end224 def make_sub_path_optional(path, sub_path)225 if path.is_a?(Airborne::OptionalPathExpectations)226 Airborne::OptionalPathExpectations.new(sub_path)227 else228 sub_path229 end230 end231 def iterate_path(path)232 raise PathError, "Invalid Path, contains '..'" if /\.\./ =~ path.to_s233 parts = path.to_s.split('.')234 parts.each_with_index do |part, index|235 use_part = make_sub_path_optional(path, part)236 yield(parts, use_part, index)237 end238 end239 def shortcut_validation(path, json)240 json.nil? && path.is_a?(Airborne::OptionalPathExpectations)241 end242 def get_by_path(path, json, type: false, &block)243 iterate_path(path) do |parts, part, index|244 return if shortcut_validation(path, json)245 if %w[* ?].include?(part.to_s)246 type = part247 walk_with_path(type.to_s, index, path, parts, json, &block) && return if index < parts.length.pred248 next249 end250 json = do_process_json(part.to_s, json)251 end252 handle_type(type, path, json, &block)253 end254 def ensure_array_or_hash(path, json)255 return if json.instance_of?(Array) || json.instance_of?(Hash)256 raise RSpec::Expectations::ExpectationNotMetError,257 "Expected #{path} to be array or hash, got #{json.class}"\258 ' from JSON response'259 end260 def handle_missing_errors(type, path, sub_path, element, &block)261 exception_path_adder({ type: type, path: sub_path }, element) do262 get_by_path(make_sub_path_optional(path, sub_path), element, &block)263 end264 end265 def walk_with_path(type, index, path, parts, json, &block)266 last_error = nil267 item_count = json.length268 error_count = 0269 json.each do |element|270 sub_path = parts[(index.next)...(parts.length)].join('.')271 begin272 handle_missing_errors(type, path, sub_path, element, &block)273 rescue Exception => e274 last_error = e275 error_count += 1276 end...

Full Screen

Full Screen

request_expectations.rb

Source:request_expectations.rb Github

copy

Full Screen

...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)...

Full Screen

Full Screen

optional

Using AI Code Generation

copy

Full Screen

1s = HTTPServer.new(:Port => 2000)2s.mount_proc('/') {|req, res|3}4trap("INT"){ s.shutdown }5s = HTTPServer.new(:Port => 2000)6s.mount_proc('/') {|req, res|7}8trap("INT"){ s.shutdown }9s = HTTPServer.new(:Port => 2000)10s.mount_proc('/') {|req, res|11}12trap("INT"){ s.shutdown }13s = HTTPServer.new(:Port => 2000)14s.mount_proc('/') {|req, res|15}16trap("INT"){ s.shutdown }17s = HTTPServer.new(:Port => 2000)18s.mount_proc('/') {|req, res|19}20trap("INT"){ s.shutdown }21s = HTTPServer.new(:Port => 2000)22s.mount_proc('/') {|req, res|23}24trap("INT"){ s.shutdown }

Full Screen

Full Screen

optional

Using AI Code Generation

copy

Full Screen

1 stub_request(:get, "http://localhost:3000/").to_return(:body => "Hello World")2 response = HTTParty.get("http://localhost:3000/")3 expect(response.code).to eq(200)4 stub_request(:get, "http://localhost:3000/").to_return(:body => "Hello World")5 response = HTTParty.get("http://localhost:3000/")6 response.code.should eq(200)7 stub_request(:get, "http://localhost:3000/").to_return(:body => "Hello World")8 response = HTTParty.get("http://localhost:3000/")9 stub_request(:get, "http://localhost:3000/").to_return(:body => "Hello World")10 response = HTTParty.get("http://localhost:3000/")11 response.code.should eql(200)12 stub_request(:get,

Full Screen

Full Screen

optional

Using AI Code Generation

copy

Full Screen

1def get_request(url)2 Net::HTTP.get_response(URI.parse(url))3 request_expectations.optional_headers = { 'Accept' => 'text/html' }4 request_expectations.optional_headers = { 'Accept' => 'text/html' }5 request_expectations.optional_headers = { 'Accept' => 'text/html' }6 request_expectations.optional_headers = { 'Accept' => 'text/html' }7 request_expectations.optional_headers = { 'Accept' => 'text/html' }

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful