From 6af8b1b274094820ebe674eb91901213d4f4eb6c Mon Sep 17 00:00:00 2001 From: Rick Fletcher Date: Thu, 5 Nov 2015 15:57:02 -0500 Subject: [PATCH] Support multi-value headers in expect_header, expect_header_contains --- lib/airborne/request_expectations.rb | 20 +++++++++++++++---- .../expect_header_contains_spec.rb | 14 +++++++++++++ .../expectations/expect_header_spec.rb | 14 +++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/airborne/request_expectations.rb b/lib/airborne/request_expectations.rb index 8686b20..20db774 100644 --- a/lib/airborne/request_expectations.rb +++ b/lib/airborne/request_expectations.rb @@ -59,12 +59,24 @@ def date private def expect_header_impl(key, content, contains = nil) - header = headers[key] - if header + content = content.downcase + header = headers[key] + + if header.is_a?(Array) + header = header.map(&:downcase) + + if contains + expect(header).to include(a_string_matching(content)) + else + expect(header).to include(content) + end + elsif header + header = header.downcase + if contains - expect(header.downcase).to include(content.downcase) + expect(header).to include(content) else - expect(header.downcase).to eq(content.downcase) + expect(header).to eq(content) end else fail RSpec::Expectations::ExpectationNotMetError, "Header #{key} not present in HTTP response" diff --git a/spec/airborne/expectations/expect_header_contains_spec.rb b/spec/airborne/expectations/expect_header_contains_spec.rb index bc23e8b..5255c04 100644 --- a/spec/airborne/expectations/expect_header_contains_spec.rb +++ b/spec/airborne/expectations/expect_header_contains_spec.rb @@ -18,4 +18,18 @@ get '/simple_get' expect { expect_header_contains(:content_type, 'bar') }.to raise_error end + + describe 'with multi-value headers' do + it 'should ensure partial header match exists' do + mock_get('simple_get', 'Set-Cookie' => ['cookie-name=myvalue; Path=/', 'another=value;']) + get '/simple_get' + expect_header_contains(:set_cookie, 'another=') + end + + it 'should ensure partial header is present' do + mock_get('simple_get', 'Set-Cookie' => ['cookie-name=myvalue; Path=/', 'another=value;']) + get '/simple_get' + expect { expect_header_contains(:set_cookie, 'bar') }.to raise_error + end + end end diff --git a/spec/airborne/expectations/expect_header_spec.rb b/spec/airborne/expectations/expect_header_spec.rb index 3325968..3bbb635 100644 --- a/spec/airborne/expectations/expect_header_spec.rb +++ b/spec/airborne/expectations/expect_header_spec.rb @@ -18,4 +18,18 @@ get '/simple_get' expect { expect_header(:foo, 'bar') }.to raise_error end + + describe 'with multi-value headers' do + it 'should find exact match for header content' do + mock_get('simple_get', 'Set-Cookie' => ['cookie-name=myvalue; Path=/', 'another=value;']) + get '/simple_get' + expect_header(:set_cookie, 'cookie-name=myvalue; Path=/') + end + + it 'should find exact match for header content' do + mock_get('simple_get', 'Set-Cookie' => ['cookie-name=myvalue; Path=/', 'another=value;']) + get '/simple_get' + expect { expect_header(:set_cookie, 'another=') }.to raise_error + end + end end