adds explicit set method to contrib/cookies

This commit is contained in:
Andrew Allen 2017-01-24 13:40:53 -08:00
parent 27d36fdad2
commit 7c71684610
2 changed files with 28 additions and 1 deletions

View File

@ -86,7 +86,7 @@ module Sinatra
end
def []=(key, value)
@response.set_cookie key.to_s, @options.merge(:value => value)
set(key, value: value)
end
def assoc(key)
@ -240,6 +240,10 @@ module Sinatra
alias select! keep_if if Hash.method_defined? :select!
def set(key, options = {})
@response.set_cookie key.to_s, @options.merge(options)
end
def shift
key, value = to_hash.shift
delete(key)

View File

@ -719,6 +719,29 @@ describe Sinatra::Cookies do
end
end
describe :set do
it 'sets a cookie' do
cookie_route { cookies.set('foo', value: 'bar') }
expect(cookie_jar['foo']).to eq('bar')
end
it 'sets a cookie with HttpOnly' do
expect(cookie_route do
request.script_name = '/foo'
cookies.set('foo', value: 'bar', httponly: true)
response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
end).to include('HttpOnly')
end
it 'sets a cookie without HttpOnly' do
expect(cookie_route do
request.script_name = '/foo'
cookies.set('foo', value: 'bar', httponly: false)
response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
end).not_to include('HttpOnly')
end
end
describe :select do
it 'removes entries from new hash' do
jar = cookies('foo=bar', 'bar=baz')