Skip to content

Commit

Permalink
Support regex header matching via expect_header_matches
Browse files Browse the repository at this point in the history
  • Loading branch information
rfletcher committed Nov 5, 2015
1 parent fedc467 commit a7baa08
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/airborne/request_expectations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/airborne/expectations/expect_header_matches_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a7baa08

Please sign in to comment.