Skip to content

Commit

Permalink
Default HTTP headers: allow to set values multiple times, reject head…
Browse files Browse the repository at this point in the history
…ers with nil values, safely duplicate this setting. Ref #88
  • Loading branch information
jodosha committed Feb 27, 2015
1 parent 89afe18 commit 956d655
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/lotus/controller/configuration.rb
Expand Up @@ -490,7 +490,9 @@ def default_charset(charset = nil)
# end
def default_headers(headers = nil)
if headers
@default_headers = headers
@default_headers.merge!(
headers.reject {|_,v| v.nil? }
)
else
@default_headers
end
Expand Down Expand Up @@ -537,7 +539,7 @@ def duplicate
c.formats = formats.dup
c.default_format = default_format
c.default_charset = default_charset
c.default_headers = default_headers
c.default_headers = default_headers.dup
end
end

Expand Down
29 changes: 29 additions & 0 deletions test/configuration_test.rb
Expand Up @@ -271,6 +271,10 @@ def hash
end

describe '#default_headers' do
after do
@configuration.reset!
end

describe "when not previously set" do
it 'returns default value' do
@configuration.default_headers.must_equal({})
Expand All @@ -287,6 +291,31 @@ def hash
it 'returns the value' do
@configuration.default_headers.must_equal headers
end

describe "multiple times" do
before do
@configuration.default_headers(headers)
@configuration.default_headers('X-Foo' => 'BAR')
end

it 'returns the value' do
@configuration.default_headers.must_equal({
'X-Frame-Options' => 'DENY',
'X-Foo' => 'BAR'
})
end
end

describe "with nil values" do
before do
@configuration.default_headers(headers)
@configuration.default_headers('X-NIL' => nil)
end

it 'rejects those' do
@configuration.default_headers.must_equal headers
end
end
end
end

Expand Down

0 comments on commit 956d655

Please sign in to comment.