Skip to content
Browse files

Undeprecate headers helper method [#125]

  • Loading branch information...
1 parent 9af3772 commit 4fc9cb8b657da81e00faef053b47bc4bd2a975ab @rtomayko rtomayko committed Mar 2, 2009
Showing with 40 additions and 6 deletions.
  1. +7 −1 lib/sinatra/base.rb
  2. +3 −5 lib/sinatra/compat.rb
  3. +30 −0 test/helpers_test.rb
View
8 lib/sinatra/base.rb
@@ -100,6 +100,12 @@ def not_found(body=nil)
error 404, body
end
+ # Set multiple response headers with Hash.
+ def headers(hash=nil)
+ response.headers.merge! hash if hash
+ response.headers
+ end
+
# Access the underlying Rack session.
def session
env['rack.session'] ||= {}
@@ -379,7 +385,7 @@ def forward
status, headers, body = @app.call(@request.env)
@response.status = status
@response.body = body
- headers.each { |k, v| @response[k] = v }
+ @response.headers.merge! headers
nil
end
View
8 lib/sinatra/compat.rb
@@ -87,12 +87,10 @@ def self.const_missing(const_name) #:nodoc:
end
# Deprecated. Use: response['Header-Name']
- def headers(header=nil)
- sinatra_warn "The 'headers' method is deprecated; use 'response' instead."
- response.headers.merge!(header) if header
- response.headers
+ def header(header=nil)
+ sinatra_warn "The 'header' method is deprecated; use 'headers' instead."
+ headers(header)
end
- alias :header :headers
# Deprecated. Use: halt
def stop(*args, &block)
View
30 test/helpers_test.rb
@@ -140,6 +140,36 @@
end
end
+describe 'Helpers#headers' do
+ it 'sets headers on the response object when given a Hash' do
+ mock_app {
+ get '/' do
+ headers 'X-Foo' => 'bar', 'X-Baz' => 'bling'
+ 'kthx'
+ end
+ }
+
+ get '/'
+ assert ok?
+ assert_equal 'bar', response['X-Foo']
+ assert_equal 'bling', response['X-Baz']
+ assert_equal 'kthx', body
+ end
+
+ it 'returns the response headers hash when no hash provided' do
+ mock_app {
+ get '/' do
+ headers['X-Foo'] = 'bar'
+ 'kthx'
+ end
+ }
+
+ get '/'
+ assert ok?
+ assert_equal 'bar', response['X-Foo']
+ end
+end
+
describe 'Helpers#session' do
it 'uses the existing rack.session' do
mock_app {

0 comments on commit 4fc9cb8

Please sign in to comment.
Something went wrong with that request. Please try again.