Skip to content

Commit

Permalink
Undeprecate headers helper method [sinatra#125]
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Mar 2, 2009
1 parent 9af3772 commit 4fc9cb8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/sinatra/base.rb
Expand Up @@ -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'] ||= {}
Expand Down Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions lib/sinatra/compat.rb
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions test/helpers_test.rb
Expand Up @@ -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 {
Expand Down

0 comments on commit 4fc9cb8

Please sign in to comment.