Best Airborne code snippet using Airborne.body
spaceborne.rb
Source:spaceborne.rb
...12 def add_time13 "TIME: #{Time.now.strftime('%d/%m/%Y %H:%M')}\n"14 end15 def add_request16 if @request_body17 "REQUEST: #{response.request.method.upcase} #{response.request.url}\n"\18 " HEADERS:\n#{JSON.pretty_generate(response.request.headers)}\n"\19 " PAYLOAD:\n#{@request_body}\n"20 else21 ''22 end23 end24 def add_response25 "\nRESPONSE: #{response.code}\n"\26 " HEADERS:\n#{JSON.pretty_generate(response.headers)}\n"\27 << response_body28 end29 def response_body30 return '' if response.request.method.casecmp('head').zero?31 if json?(response.headers)32 " JSON_BODY\n#{JSON.pretty_generate(json_body)}\n"33 else34 " BODY\n#{response.body}\n"35 end36 end37 def request_info(str = "\n")38 str << add_time << add_request << add_response39 str40 end41 def wrap_request42 yield43 rescue Exception => e44 raise e unless response45 raise RSpec::Expectations::ExpectationNotMetError, e.message + request_info46 end47end48# monkeypatch Airborne49module Airborne50 def decode_body51 if response.headers[:content_encoding]&.include?('gzip')52 Zlib::Inflate.new(32 + Zlib::MAX_WBITS).inflate(response.body)53 else54 response.body55 end56 end57 def json_body58 @json_body ||= JSON.parse(decode_body, symbolize_names: true)59 rescue StandardError60 raise InvalidJsonError, 'Api request returned invalid json'61 end62 # spaceborne enhancements63 module RestClientRequester64 def body?(method)65 case method66 when :post, :patch, :put67 true68 else69 false70 end71 end72 def split_options(options)73 local = {}74 local[:nonjson_data] = options.dig(:headers, :nonjson_data)75 options[:headers].delete(:nonjson_data) if local[:nonjson_data]76 local[:is_hash] = options[:body].is_a?(Hash)77 local[:proxy] = options.dig(:headers, :use_proxy)78 local79 end80 def calc_headers(options, local)81 headers = base_headers.merge(options[:headers] || {})82 headers[:no_restclient_headers] = true83 return headers unless local[:is_hash]84 headers.delete('Content-Type') if options[:nonjson_data]85 headers86 end87 def handle_proxy(_options, local)88 return unless local[:proxy]89 RestClient.proxy = local[:proxy]90 end91 def calc_body(options, local)92 return '' unless options[:body]93 if local[:nonjson_data] || !local[:is_hash]94 options[:body]95 else96 options[:body].to_json97 end98 end99 def send_restclient(method, url, body, headers)100 if body?(method)101 RestClient.send(method, url, body, headers)102 else103 RestClient.send(method, url, headers)104 end105 end106 def pre_request(options)107 @json_body = nil108 local_options = split_options(options)109 handle_proxy(options, local_options)110 hdrs = calc_headers(options, local_options)111 @request_body = calc_body(options, local_options)112 [hdrs, @request_body]113 end114 def make_request(method, url, options = {})115 hdrs, body = pre_request(options)116 send_restclient(method, get_url(url), body, hdrs)117 rescue RestClient::ServerBrokeConnection => e118 raise e119 rescue RestClient::Exception => e120 if [301, 302].include?(e.response&.code)121 e.response.&follow_redirection122 else123 e.response124 end125 end126 private127 def base_headers128 { 'Content-Type' => 'application/json' }129 .merge(Airborne.configuration.headers || {})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)...
base.rb
Source:base.rb
...3require 'active_support/core_ext/hash/indifferent_access'4module Airborne5 class InvalidJsonError < StandardError; end6 include RequestExpectations7 attr_reader :response, :headers, :body8 def self.configure9 RSpec.configure do |config|10 yield config11 end12 end13 def self.included(base)14 if !Airborne.configuration.requester_module.nil?15 base.send(:include, Airborne.configuration.requester_module)16 elsif !Airborne.configuration.rack_app.nil?17 base.send(:include, RackTestRequester)18 else19 base.send(:include, RestClientRequester)20 end21 end22 def self.configuration23 RSpec.configuration24 end25 def get(url, headers = nil)26 @response = make_request(:get, url, headers: headers)27 end28 def post(url, post_body = nil, headers = nil)29 @response = make_request(:post, url, body: post_body, headers: headers)30 end31 def patch(url, patch_body = nil, headers = nil)32 @response = make_request(:patch, url, body: patch_body, headers: headers)33 end34 def put(url, put_body = nil, headers = nil)35 @response = make_request(:put, url, body: put_body, headers: headers)36 end37 def delete(url, delete_body = nil, headers = nil)38 @response = make_request(:delete, url, body: delete_body, headers: headers)39 end40 def head(url, headers = nil)41 @response = make_request(:head, url, headers: headers)42 end43 def options(url, headers = nil)44 @response = make_request(:options, url, headers: headers)45 end46 def response47 @response48 end49 def headers50 HashWithIndifferentAccess.new(response.headers)51 end52 def body53 response.body54 end55 def json_body56 JSON.parse(response.body, symbolize_names: true) rescue fail InvalidJsonError, 'Api request returned invalid json'57 end58 private59 def get_url(url)60 base = Airborne.configuration.base_url || ''61 base + url62 end63end...
functional-tests.rb
Source:functional-tests.rb
...22 it "#{url} should return exact title/subtitle match" do23 search_title = "The card catalog: books, cards, and literary treasures"24 post search_endpoint, { :query => "keyword:{\"#{search_title}\"}", :pagination => { :start => 0, :rows => 1 }}25 expect_status( 200 )26 expect( json_body[:group_list].count ).to be > 027 first_titles = Helpers.pool_results_first_title( json_body )28 first_subtitle = Helpers.pool_results_first_subtitle( json_body )29 search_result = first_titles + ": "+ first_subtitle30 expect(search_result).to include(search_title)31 end32 it "#{url} should return at least one result for a * search" do33 post search_endpoint, { :query => "keyword:{*}", :pagination => { :start => 0, :rows => 1 }}34 expect_status( 200 )35 expect( json_body[:group_list].count ).to be > 036 expect(Helpers.pool_results_first_title( json_body )).not_to be nil?37 end38end...
body
Using AI Code Generation
1 expect_json('foo', 'bar')2 expect_json_types('foo', :string)3 expect_json('foo', 'bar')4 expect_json_types('foo', :string)5 expect_json('foo', 'bar')6 expect_json_types('foo', :string)7 expect_json('foo', 'bar')8 expect_json_types('foo', :string)9 expect_json('foo', 'bar')10 expect_json_types('foo', :string)
body
Using AI Code Generation
1 expect_json_types('*', name: :string, email: :string)2 expect_json('?', name: 'John')3 expect_json('?', email: '
body
Using AI Code Generation
1 expect_json_types('data.*', {id: :int})2 expect_json('data.*', {id: 1})3 expect_json('data.*', {id: 2})4 expect_json('data.*', {id: 3})5 expect_json('data.*', {id: 4})6 expect_json('data.*', {id: 5})7 expect_json('data.*', {id: 6})8 expect_json('data.*', {id: 7})9 expect_json('data.*', {id: 8})10 expect_json('data.*', {id: 9})11 expect_json('data.*', {id: 10})
body
Using AI Code Generation
1 expect_json_types(login: :string, id: :int, name: :string, company: :string)2 expect_json(login: 'sammyshj', name: 'Sammy Shaji')3 expect(body['login']).to eq('sammyshj')4 expect_json_types(login: :string, id: :int, name: :string, company: :string)5 expect_json(login: 'sammyshj', name: 'Sammy Shaji')6 expect(json_body['login']).to eq('sammyshj')7 expect_json_types(login: :string, id: :int, name: :string, company: :string)8 expect_json(login: 'sammyshj', name: 'Sammy Shaji')9 expect(json['login']).to eq('sammyshj')
body
Using AI Code Generation
1 expect_json_types(:object)2 expect_json_types('name', :string)3 expect_json_types('name', :string)4 expect_json_types('age', :int)5 expect_json_types('name', :string)6 expect_json_types('age', :int)7 expect_json_types('address', :string)8 expect_json_types('name', :string)
body
Using AI Code Generation
1 expect(body).to include 'Hello World'2Finished in 0.063 seconds (files took 0.0704 seconds to load)3 expect(body).to include 'Hello World'4 Failure/Error: expect(body).to include 'Hello World'5Finished in 0.063 seconds (files took 0.0752 seconds to load)6 expect(body).to include 'Hello World'7 Failure/Error: expect(body).to include 'Hello World'8Finished in 0.064 seconds (files took 0.074 seconds to load)
body
Using AI Code Generation
1puts get("http://localhost:3000/api/v1/users/1").body["id"]2puts get("http://localhost:3000/api/v1/users/1").parsed_body["id"]3puts get("http://localhost:3000/api/v1/users/1").parsed_body["id"]4puts get("http://localhost:3000/api/v1/users/1").parsed_body["id"]5puts get("http://localhost:3000/api/v1/users/1").parsed_body["id"]6puts get("http://localhost:3000/api/v1/users/1").parsed_body["id"]
body
Using AI Code Generation
1 expect(body).to include 'Hello World'2Finished in 0.063 seconds (files took 0.0704 seconds to load)3 expect(body).to include 'Hello World'4 Failure/Error: expect(body).to include 'Hello World'5Finished in 0.063 seconds (files took 0.0752 seconds to load)6 expect(body).to include 'Hello World'7 Failure/Error: expect(body).to include 'Hello World'8Finished in 0.064 seconds (files took 0.074 seconds to load)
body
Using AI Code Generation
1puts get("http://localhost:3000/api/v1/users/1").body["id"]2puts get("http://localhost:3000/api/v1/users/1").parsed_body["id"]3puts get("http://localhost:3000/api/v1/users/1").parsed_body["id"]4puts get("http://localhost:3000/api/v1/users/1").parsed_body["id"]5puts get("http://localhost:3000/api/v1/users/1").parsed_body["id"]6puts get("http://localhost:3000/api/v1/users/1").parsed_body["id"]
body
Using AI Code Generation
1 expect(body).to include 'Hello World'2Finished in 0.063 seconds (files took 0.0704 seconds to load)3 expect(body).to include 'Hello World'4 Failure/Error: expect(body).to include 'Hello World'5Finished in 0.063 seconds (files took 0.0752 seconds to load)6 expect(body).to include 'Hello World'7 Failure/Error: expect(body).to include 'Hello World'8Finished in 0.064 seconds (files took 0.074 seconds to load)
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!!