spec/http/headers_spec.rb in http-0.6.4 vs spec/http/headers_spec.rb in http-0.7.0
- old
+ new
@@ -1,8 +1,8 @@
require 'spec_helper'
-describe HTTP::Headers do
+RSpec.describe HTTP::Headers do
subject(:headers) { described_class.new }
it 'is Enumerable' do
expect(headers).to be_an Enumerable
end
@@ -24,12 +24,12 @@
expect(headers['Set-Cookie']).to eq 'woo=hoo'
end
it 'allows set multiple values' do
headers.set :set_cookie, 'hoo=ray'
- headers.set :set_cookie, %w[hoo=ray woo=hoo]
- expect(headers['Set-Cookie']).to eq %w[hoo=ray woo=hoo]
+ headers.set :set_cookie, %w(hoo=ray woo=hoo)
+ expect(headers['Set-Cookie']).to eq %w(hoo=ray woo=hoo)
end
end
describe '#[]=' do
it 'sets header value' do
@@ -48,12 +48,12 @@
expect(headers['Set-Cookie']).to eq 'woo=hoo'
end
it 'allows set multiple values' do
headers[:set_cookie] = 'hoo=ray'
- headers[:set_cookie] = %w[hoo=ray woo=hoo]
- expect(headers['Set-Cookie']).to eq %w[hoo=ray woo=hoo]
+ headers[:set_cookie] = %w(hoo=ray woo=hoo)
+ expect(headers['Set-Cookie']).to eq %w(hoo=ray woo=hoo)
end
end
describe '#delete' do
before { headers.set 'Content-Type', 'application/json' }
@@ -81,29 +81,29 @@
end
it 'appends new value if header exists' do
headers.add :set_cookie, 'hoo=ray'
headers.add :set_cookie, 'woo=hoo'
- expect(headers['Set-Cookie']).to eq %w[hoo=ray woo=hoo]
+ expect(headers['Set-Cookie']).to eq %w(hoo=ray woo=hoo)
end
it 'allows append multiple values' do
headers.add :set_cookie, 'hoo=ray'
- headers.add :set_cookie, %w[woo=hoo yup=pie]
- expect(headers['Set-Cookie']).to eq %w[hoo=ray woo=hoo yup=pie]
+ headers.add :set_cookie, %w(woo=hoo yup=pie)
+ expect(headers['Set-Cookie']).to eq %w(hoo=ray woo=hoo yup=pie)
end
end
describe '#get' do
before { headers.set 'Content-Type', 'application/json' }
it 'returns array of associated values' do
- expect(headers.get 'Content-Type').to eq %w[application/json]
+ expect(headers.get 'Content-Type').to eq %w(application/json)
end
it 'normalizes header name' do
- expect(headers.get :content_type).to eq %w[application/json]
+ expect(headers.get :content_type).to eq %w(application/json)
end
context 'when header does not exists' do
it 'returns empty array' do
expect(headers.get :accept).to eq []
@@ -139,11 +139,11 @@
it 'normalizes header name' do
expect(headers[:set_cookie]).to_not be_nil
end
it 'returns array of associated values' do
- expect(headers[:set_cookie]).to eq %w[hoo=ray woo=hoo]
+ expect(headers[:set_cookie]).to eq %w(hoo=ray woo=hoo)
end
end
end
describe '#to_h' do
@@ -152,26 +152,26 @@
headers.add :set_cookie, 'hoo=ray'
headers.add :set_cookie, 'woo=hoo'
end
it 'returns a Hash' do
- expect(headers.to_h).to be_a Hash
+ expect(headers.to_h).to be_a ::Hash
end
it 'returns Hash with normalized keys' do
- expect(headers.to_h.keys).to match_array %w[Content-Type Set-Cookie]
+ expect(headers.to_h.keys).to match_array %w(Content-Type Set-Cookie)
end
context 'for a header with single value' do
it 'provides a value as is' do
expect(headers.to_h['Content-Type']).to eq 'application/json'
end
end
context 'for a header with multiple values' do
it 'provides an array of values' do
- expect(headers.to_h['Set-Cookie']).to eq %w[hoo=ray woo=hoo]
+ expect(headers.to_h['Set-Cookie']).to eq %w(hoo=ray woo=hoo)
end
end
end
describe '#to_a' do
@@ -185,33 +185,33 @@
expect(headers.to_a).to be_a Array
end
it 'returns Array of key/value pairs with normalized keys' do
expect(headers.to_a).to eq [
- %w[Content-Type application/json],
- %w[Set-Cookie hoo=ray],
- %w[Set-Cookie woo=hoo]
+ %w(Content-Type application/json),
+ %w(Set-Cookie hoo=ray),
+ %w(Set-Cookie woo=hoo)
]
end
end
describe '#inspect' do
- before { headers.set :set_cookie, %w[hoo=ray woo=hoo] }
+ before { headers.set :set_cookie, %w(hoo=ray woo=hoo) }
subject { headers.inspect }
- it { should eq '#<HTTP::Headers {"Set-Cookie"=>["hoo=ray", "woo=hoo"]}>' }
+ it { is_expected.to eq '#<HTTP::Headers {"Set-Cookie"=>["hoo=ray", "woo=hoo"]}>' }
end
describe '#keys' do
before do
headers.add :content_type, 'application/json'
headers.add :set_cookie, 'hoo=ray'
headers.add :set_cookie, 'woo=hoo'
end
it 'returns uniq keys only' do
- expect(headers.keys).to have_exactly(2).items
+ expect(headers.keys.size).to eq 2
end
it 'normalizes keys' do
expect(headers.keys).to include('Content-Type', 'Set-Cookie')
end
@@ -228,36 +228,36 @@
expect { |b| headers.each(&b) }.to yield_control.exactly(3).times
end
it 'yields headers in the same order they were added' do
expect { |b| headers.each(&b) }.to yield_successive_args(
- %w[Set-Cookie hoo=ray],
- %w[Content-Type application/json],
- %w[Set-Cookie woo=hoo]
+ %w(Set-Cookie hoo=ray),
+ %w(Content-Type application/json),
+ %w(Set-Cookie woo=hoo)
)
end
end
describe '.empty?' do
subject { headers.empty? }
context 'initially' do
- it { should be_true }
+ it { is_expected.to be true }
end
context 'when header exists' do
before { headers.add :accept, 'text/plain' }
- it { should be_false }
+ it { is_expected.to be false }
end
context 'when last header was removed' do
before do
headers.add :accept, 'text/plain'
headers.delete :accept
end
- it { should be_true }
+ it { is_expected.to be true }
end
end
describe '#hash' do
let(:left) { described_class.new }
@@ -282,11 +282,11 @@
expect(left).to eq right
end
it 'allows comparison with Array of key/value pairs' do
left.add :accept, 'text/plain'
- expect(left).to eq [%w[Accept text/plain]]
+ expect(left).to eq [%w(Accept text/plain)]
end
it 'sensitive to headers order' do
left.add :accept, 'text/plain'
left.add :cookie, 'woo=hoo'
@@ -309,12 +309,12 @@
describe '#dup' do
before { headers.set :content_type, 'application/json' }
subject(:dupped) { headers.dup }
- it { should be_a described_class }
- it { should_not be headers }
+ it { is_expected.to be_a described_class }
+ it { is_expected.not_to be headers }
it 'has headers copied' do
expect(dupped[:content_type]).to eq 'application/json'
end
@@ -333,11 +333,11 @@
describe '#merge!' do
before do
headers.set :host, 'example.com'
headers.set :accept, 'application/json'
- headers.merge! :accept => 'plain/text', :cookie => %w[hoo=ray woo=hoo]
+ headers.merge! :accept => 'plain/text', :cookie => %w(hoo=ray woo=hoo)
end
it 'leaves headers not presented in other as is' do
expect(headers[:host]).to eq 'example.com'
end
@@ -345,26 +345,26 @@
it 'overwrites existing values' do
expect(headers[:accept]).to eq 'plain/text'
end
it 'appends other headers, not presented in base' do
- expect(headers[:cookie]).to eq %w[hoo=ray woo=hoo]
+ expect(headers[:cookie]).to eq %w(hoo=ray woo=hoo)
end
end
describe '#merge' do
before do
headers.set :host, 'example.com'
headers.set :accept, 'application/json'
end
subject(:merged) do
- headers.merge :accept => 'plain/text', :cookie => %w[hoo=ray woo=hoo]
+ headers.merge :accept => 'plain/text', :cookie => %w(hoo=ray woo=hoo)
end
- it { should be_a described_class }
- it { should_not be headers }
+ it { is_expected.to be_a described_class }
+ it { is_expected.not_to be headers }
it 'does not affects original headers' do
expect(merged.to_h).to_not eq headers.to_h
end
@@ -375,11 +375,11 @@
it 'overwrites existing values' do
expect(merged[:accept]).to eq 'plain/text'
end
it 'appends other headers, not presented in base' do
- expect(merged[:cookie]).to eq %w[hoo=ray woo=hoo]
+ expect(merged[:cookie]).to eq %w(hoo=ray woo=hoo)
end
end
describe '.coerce' do
let(:dummyClass) { Class.new { def respond_to?(*); end } }
@@ -393,11 +393,11 @@
hashie = double :to_h => {'accept' => 'json'}
expect(described_class.coerce(hashie)['accept']).to eq 'json'
end
it 'accepts any object that respond to #to_a' do
- hashie = double :to_a => [%w[accept json]]
+ hashie = double :to_a => [%w(accept json)]
expect(described_class.coerce(hashie)['accept']).to eq 'json'
end
it 'fails if given object cannot be coerced' do
expect { described_class.coerce dummyClass.new }.to raise_error HTTP::Error
@@ -406,12 +406,16 @@
context 'with duplicate header keys (mixed case)' do
let(:headers) { {'Set-Cookie' => 'hoo=ray', 'set-cookie' => 'woo=hoo'} }
it 'adds all headers' do
expect(described_class.coerce(headers).to_a).to match_array([
- %w[Set-Cookie hoo=ray],
- %w[Set-Cookie woo=hoo]
+ %w(Set-Cookie hoo=ray),
+ %w(Set-Cookie woo=hoo)
])
end
+ end
+
+ it 'is aliased as .[]' do
+ expect(described_class.method :coerce).to eq described_class.method(:[])
end
end
end