diff --git a/lib/airborne/request_expectations.rb b/lib/airborne/request_expectations.rb index 8686b20..0f7721e 100644 --- a/lib/airborne/request_expectations.rb +++ b/lib/airborne/request_expectations.rb @@ -44,6 +44,10 @@ def expect_header_contains(key, content) expect_header_impl(key, content, true) end + def expect_header_matches(key, content) + expect_header_impl(key, content, false, true) + end + def optional(hash) OptionalHashTypeExpectations.new(hash) end @@ -58,11 +62,13 @@ def date private - def expect_header_impl(key, content, contains = nil) + def expect_header_impl(key, content, contains = nil, matches = nil) header = headers[key] if header if contains expect(header.downcase).to include(content.downcase) + elsif matches + expect(header.downcase).to match(content) else expect(header.downcase).to eq(content.downcase) end diff --git a/spec/airborne/expectations/expect_header_matches_spec.rb b/spec/airborne/expectations/expect_header_matches_spec.rb new file mode 100644 index 0000000..56d2254 --- /dev/null +++ b/spec/airborne/expectations/expect_header_matches_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe 'expect header matches' do + it 'should ensure partial header match exists' do + mock_get('simple_get', 'Content-Type' => 'application/json') + get '/simple_get' + expect_header_matches(:content_type, /json/) + end + + it 'should ensure header is present' do + mock_get('simple_get', 'Content-Type' => 'application/json') + get '/simple_get' + expect { expect_header_matches(:foo, /json/) }.to raise_error + end + + it 'should ensure partial header is present' do + mock_get('simple_get', 'Content-Type' => 'application/json') + get '/simple_get' + expect { expect_header_matches(:content_type, /bar/) }.to raise_error + end +end