lib/airborne/request_expectations.rb in airborne-0.0.22 vs lib/airborne/request_expectations.rb in airborne-0.0.23
- old
+ new
@@ -4,47 +4,41 @@
module RequestExpectations
include RSpec
include PathMatcher
def expect_json_types(*args)
- set_response(@response) if @json_body.nil?
call_with_path(args) do |param, body|
expect_json_types_impl(param, body)
end
end
def expect_json(*args)
- set_response(@response) if @json_body.nil?
call_with_path(args) do |param, body|
expect_json_impl(param, body)
end
end
def expect_json_keys(*args)
- set_response(@response) if @json_body.nil?
call_with_path(args) do |param, body|
expect(body.keys).to include(*param)
end
end
def expect_status(code)
- set_response(@response) if @json_body.nil?
expect(response.code).to eq(code)
end
def expect_header(key, content)
- set_response(@response) if @json_body.nil?
header = headers[key]
if header
expect(header.downcase).to eq(content.downcase)
else
raise "Header #{key} not present in HTTP response"
end
end
def expect_header_contains(key, content)
- set_response(@response) if @json_body.nil?
header = headers[key]
if header
expect(header.downcase).to include(content.downcase)
else
raise "Header #{key} not present in HTTP response"
@@ -57,12 +51,24 @@
def regex(reg)
Regexp.new(reg)
end
+ [:expect_json_types, :expect_json, :expect_json_keys, :expect_status, :expect_header, :expect_header_contains].each do |method_name|
+ method = instance_method(method_name)
+ define_method(method_name) do |*args, &block|
+ set_rails_response
+ method.bind(self).(*args, &block)
+ end
+ end
+
private
+ def set_rails_response
+ set_response(@response) if @json_body.nil?
+ end
+
def call_with_path(args)
if args.length == 2
get_by_path(args[0], json_body) do|json_chunk|
yield(args[1], json_chunk)
end
@@ -99,38 +105,41 @@
end
def expect_json_types_impl(expectations, hash)
return if expectations.class == Airborne::OptionalHashTypeExpectations && hash.nil?
@mapper ||= get_mapper
+ return expect(@mapper[expectations].include?(hash.class)).to eq(true) if expectations.class == Symbol
expectations.each do |prop_name, expected_type|
value = hash[prop_name]
if expected_type.class == Hash || expected_type.class == Airborne::OptionalHashTypeExpectations
expect_json_types_impl(expected_type, value)
elsif expected_type.class == Proc
expected_type.call(value)
elsif expected_type.to_s.include?("array_of")
expect(value.class).to eq(Array), "Expected #{prop_name} to be of type #{expected_type}, got #{value.class} instead"
value.each do |val|
- expect(@mapper[expected_type].include?(val.class)).to eq(true), "Expected #{prop_name} to be of type #{expected_type}, got #{val.class} instead"
+ expect(@mapper[expected_type].include?(val.class)).to eq(true), "Expected #{prop_name} to be of type #{expected_type}, got #{val.class} instead"
end
else
expect(@mapper[expected_type].include?(value.class)).to eq(true), "Expected #{prop_name} to be of type #{expected_type}, got #{value.class} instead"
end
end
end
def expect_json_impl(expectations, hash)
+ hash = hash.to_s if expectations.class == Regexp
+ return expect(hash).to match(expectations) if [String, Regexp, Float, Fixnum, Bignum].include?(expectations.class)
expectations.each do |prop_name, expected_value|
actual_value = hash[prop_name]
if expected_value.class == Hash
expect_json_impl(expected_value, actual_value)
elsif expected_value.class == Proc
expected_value.call(actual_value)
elsif expected_value.class == Regexp
- expect(actual_value).to match(expected_value)
+ expect(actual_value.to_s).to match(expected_value)
else
expect(actual_value).to eq(expected_value)
end
end
end
end
-end
\ No newline at end of file
+end