Skip to content

Modify cookie[]= to take a hash of extra options #91

Closed
wants to merge 1 commit into from
View
3 lib/sinatra/cookies.rb
@@ -88,7 +88,8 @@ def [](key)
end
def []=(key, value)
- @response.set_cookie key.to_s, @options.merge(:value => value)
+ options = (value.is_a?(Array) && value[1].is_a?(Hash)) ? value[1] : {}
+ @response.set_cookie key.to_s, @options.merge(options).merge(:value => value)
end
def assoc(key)
View
7 spec/cookies_spec.rb
@@ -124,6 +124,13 @@ def cookies(*set_cookies)
cookie_jar['foo'].should be == 'bar'
end
+ it 'sets a cookie with extra options' do
+ cookie_route do
+ cookies['foo'] = 'bar', {:path => '/baz'}
+ response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
+ end.should include('path=/baz')
+ end
+
it 'adds a value to the cookies hash' do
cookie_route do
cookies['foo'] = 'bar'
Something went wrong with that request. Please try again.