Merge pull request #1240 from allenan/sinatra-contrib-cookies-set

Add explicit set method to contrib/cookies
This commit is contained in:
Zachary Scott 2017-01-30 12:13:09 +09:00 committed by GitHub
commit 272fe3a112
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')