2014-05-09 02:39:16 -04:00
|
|
|
require 'spec_helper'
|
2011-09-04 21:13:40 -04:00
|
|
|
|
2022-02-05 07:27:48 -05:00
|
|
|
RSpec.describe Sinatra::Cookies do
|
2022-07-31 08:56:44 -04:00
|
|
|
def cookie_route(*cookies, headers: {}, &block)
|
2011-09-04 21:13:40 -04:00
|
|
|
result = nil
|
|
|
|
set_cookie(cookies)
|
|
|
|
@cookie_app.get('/') do
|
|
|
|
result = instance_eval(&block)
|
|
|
|
"ok"
|
|
|
|
end
|
2022-07-31 08:56:44 -04:00
|
|
|
get '/', {}, headers || {}
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(last_response).to be_ok
|
|
|
|
expect(body).to eq("ok")
|
2011-09-04 21:13:40 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def cookies(*set_cookies)
|
|
|
|
cookie_route(*set_cookies) { cookies }
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
app = nil
|
|
|
|
mock_app do
|
|
|
|
helpers Sinatra::Cookies
|
|
|
|
app = self
|
|
|
|
end
|
|
|
|
@cookie_app = app
|
|
|
|
clear_cookies
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :cookie_route do
|
|
|
|
it 'runs the block' do
|
|
|
|
ran = false
|
|
|
|
cookie_route { ran = true }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(ran).to be true
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the block result' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route { 42 }).to eq(42)
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :== do
|
|
|
|
it 'is comparable to hashes' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies).to eq({})
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is comparable to anything that responds to to_hash' do
|
|
|
|
other = Struct.new(:to_hash).new({})
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies).to eq(other)
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :[] do
|
|
|
|
it 'allows access to request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies("foo=bar")["foo"]).to eq("bar")
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'takes symbols as keys' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies("foo=bar")[:foo]).to eq("bar")
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil for missing keys' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies("foo=bar")['bar']).to be_nil
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows access to response cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
response.set_cookie 'foo', 'bar'
|
|
|
|
cookies['foo']
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to eq('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'favors response cookies over request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route('foo=bar') do
|
2011-09-04 21:13:40 -04:00
|
|
|
response.set_cookie 'foo', 'baz'
|
|
|
|
cookies['foo']
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to eq('baz')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
it 'takes the last value for response cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
response.set_cookie 'foo', 'bar'
|
|
|
|
response.set_cookie 'foo', 'baz'
|
|
|
|
cookies['foo']
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to eq('baz')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :[]= do
|
|
|
|
it 'sets cookies to httponly' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to include('HttpOnly')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
2015-05-22 18:40:08 -04:00
|
|
|
it 'sets domain to nil if localhost' do
|
2022-07-31 08:56:44 -04:00
|
|
|
headers = {'HTTP_HOST' => 'localhost'}
|
|
|
|
expect(cookie_route(headers: headers) do
|
2015-05-22 18:40:08 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
response['Set-Cookie']
|
2016-05-10 10:08:54 -04:00
|
|
|
end).not_to include("domain")
|
2015-05-22 18:40:08 -04:00
|
|
|
end
|
|
|
|
|
2011-09-04 21:13:40 -04:00
|
|
|
it 'sets the domain' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to include('domain=example.org')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets path to / by default' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to include('path=/')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets path to the script_name if app is nested' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
request.script_name = '/foo'
|
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to include('path=/foo')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets a cookie' do
|
|
|
|
cookie_route { cookies['foo'] = 'bar' }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_jar['foo']).to eq('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds a value to the cookies hash' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
cookies['foo']
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to eq('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :assoc do
|
|
|
|
it 'behaves like Hash#assoc' do
|
|
|
|
cookies('foo=bar').assoc('foo') == ['foo', 'bar']
|
|
|
|
end
|
|
|
|
end if Hash.method_defined? :assoc
|
|
|
|
|
|
|
|
describe :clear do
|
|
|
|
it 'removes request cookies from cookies hash' do
|
|
|
|
jar = cookies('foo=bar')
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar['foo']).to eq('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
jar.clear
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar['foo']).to be_nil
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes response cookies from cookies hash' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
cookies.clear
|
|
|
|
cookies['foo']
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to be_nil
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
2012-11-09 09:07:40 -05:00
|
|
|
it 'expires existing cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route("foo=bar") do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies.clear
|
|
|
|
response['Set-Cookie']
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to include("foo=;", "expires=", "1970 00:00:00")
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :compare_by_identity? do
|
2016-05-10 10:08:54 -04:00
|
|
|
it { expect(cookies).not_to be_compare_by_identity }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe :default do
|
2016-05-10 10:08:54 -04:00
|
|
|
it { expect(cookies.default).to be_nil }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe :default_proc do
|
2016-05-10 10:08:54 -04:00
|
|
|
it { expect(cookies.default_proc).to be_nil }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe :delete do
|
|
|
|
it 'removes request cookies from cookies hash' do
|
|
|
|
jar = cookies('foo=bar')
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar['foo']).to eq('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
jar.delete 'foo'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar['foo']).to be_nil
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes response cookies from cookies hash' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
2012-11-09 09:07:40 -05:00
|
|
|
cookies.delete 'foo'
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo']
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to be_nil
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
2012-11-09 09:07:40 -05:00
|
|
|
it 'expires existing cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route("foo=bar") do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies.delete 'foo'
|
|
|
|
response['Set-Cookie']
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to include("foo=;", "expires=", "1970 00:00:00")
|
2012-11-09 09:07:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'honours the app cookie_options' do
|
|
|
|
@cookie_app.class_eval do
|
|
|
|
set :cookie_options, {
|
|
|
|
:path => '/foo',
|
|
|
|
:domain => 'bar.com',
|
|
|
|
:secure => true,
|
|
|
|
:httponly => true
|
|
|
|
}
|
|
|
|
end
|
|
|
|
cookie_header = cookie_route("foo=bar") do
|
|
|
|
cookies.delete 'foo'
|
|
|
|
response['Set-Cookie']
|
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_header).to include("path=/foo;", "domain=bar.com;", "secure;", "HttpOnly")
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not touch other cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route("foo=bar", "bar=baz") do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies.delete 'foo'
|
|
|
|
cookies['bar']
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to eq('baz')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the previous value for request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route("foo=bar") do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies.delete "foo"
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to eq("bar")
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the previous value for response cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
cookies.delete "foo"
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to eq("bar")
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil for non-existing cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route { cookies.delete("foo") }).to be_nil
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :delete_if do
|
|
|
|
it 'deletes cookies that match the block' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route('foo=bar') do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['bar'] = 'baz'
|
|
|
|
cookies['baz'] = 'foo'
|
|
|
|
cookies.delete_if { |*a| a.include? 'bar' }
|
|
|
|
cookies.values_at 'foo', 'bar', 'baz'
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to eq([nil, nil, 'foo'])
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :each do
|
|
|
|
it 'loops through cookies' do
|
|
|
|
keys = []
|
|
|
|
foo = nil
|
|
|
|
bar = nil
|
|
|
|
|
|
|
|
cookie_route('foo=bar', 'bar=baz') do
|
|
|
|
cookies.each do |key, value|
|
|
|
|
foo = value if key == 'foo'
|
|
|
|
bar = value if key == 'bar'
|
|
|
|
keys << key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(keys.sort).to eq(['bar', 'foo'])
|
|
|
|
expect(foo).to eq('bar')
|
|
|
|
expect(bar).to eq('baz')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'favors response over request cookies' do
|
|
|
|
seen = false
|
2016-05-10 10:08:54 -04:00
|
|
|
key = nil
|
|
|
|
value = nil
|
2011-09-04 21:13:40 -04:00
|
|
|
cookie_route('foo=bar') do
|
|
|
|
cookies[:foo] = 'baz'
|
2016-05-10 10:08:54 -04:00
|
|
|
cookies.each do |k,v|
|
|
|
|
key = k
|
|
|
|
value = v
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(key).to eq('foo')
|
|
|
|
expect(value).to eq('baz')
|
|
|
|
expect(seen).to eq(false)
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not loop through deleted cookies' do
|
|
|
|
cookie_route('foo=bar') do
|
|
|
|
cookies.delete :foo
|
|
|
|
cookies.each { fail }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an enumerator' do
|
2016-05-10 10:08:54 -04:00
|
|
|
keys = []
|
2011-09-04 21:13:40 -04:00
|
|
|
cookie_route('foo=bar') do
|
|
|
|
enum = cookies.each
|
2016-05-10 10:08:54 -04:00
|
|
|
enum.each { |key, value| keys << key }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
keys.each{ |key| expect(key).to eq('foo')}
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :each_key do
|
|
|
|
it 'loops through cookies' do
|
|
|
|
keys = []
|
|
|
|
|
|
|
|
cookie_route('foo=bar', 'bar=baz') do
|
|
|
|
cookies.each_key do |key|
|
|
|
|
keys << key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(keys.sort).to eq(['bar', 'foo'])
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'only yields keys once' do
|
|
|
|
seen = false
|
|
|
|
cookie_route('foo=bar') do
|
|
|
|
cookies[:foo] = 'baz'
|
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(seen).to eq(false)
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not loop through deleted cookies' do
|
|
|
|
cookie_route('foo=bar') do
|
|
|
|
cookies.delete :foo
|
|
|
|
cookies.each_key { fail }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an enumerator' do
|
2016-05-10 10:08:54 -04:00
|
|
|
keys = []
|
2011-09-04 21:13:40 -04:00
|
|
|
cookie_route('foo=bar') do
|
|
|
|
enum = cookies.each_key
|
2016-05-10 10:08:54 -04:00
|
|
|
enum.each { |key| keys << key }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
keys.each{ |key| expect(key).to eq('foo')}
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :each_pair do
|
|
|
|
it 'loops through cookies' do
|
|
|
|
keys = []
|
|
|
|
foo = nil
|
|
|
|
bar = nil
|
|
|
|
|
|
|
|
cookie_route('foo=bar', 'bar=baz') do
|
|
|
|
cookies.each_pair do |key, value|
|
|
|
|
foo = value if key == 'foo'
|
|
|
|
bar = value if key == 'bar'
|
|
|
|
keys << key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(keys.sort).to eq(['bar', 'foo'])
|
|
|
|
expect(foo).to eq('bar')
|
|
|
|
expect(bar).to eq('baz')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'favors response over request cookies' do
|
|
|
|
seen = false
|
2016-05-10 10:08:54 -04:00
|
|
|
key = nil
|
|
|
|
value = nil
|
2011-09-04 21:13:40 -04:00
|
|
|
cookie_route('foo=bar') do
|
|
|
|
cookies[:foo] = 'baz'
|
2016-05-10 10:08:54 -04:00
|
|
|
cookies.each_pair do |k, v|
|
|
|
|
key = k
|
|
|
|
value = v
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(key).to eq('foo')
|
|
|
|
expect(value).to eq('baz')
|
|
|
|
expect(seen).to eq(false)
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not loop through deleted cookies' do
|
|
|
|
cookie_route('foo=bar') do
|
|
|
|
cookies.delete :foo
|
|
|
|
cookies.each_pair { fail }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an enumerator' do
|
2016-05-10 10:08:54 -04:00
|
|
|
keys = []
|
2011-09-04 21:13:40 -04:00
|
|
|
cookie_route('foo=bar') do
|
|
|
|
enum = cookies.each_pair
|
2016-05-10 10:08:54 -04:00
|
|
|
enum.each { |key, value| keys << key }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
keys.each{ |key| expect(key).to eq('foo')}
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :each_value do
|
|
|
|
it 'loops through cookies' do
|
|
|
|
values = []
|
|
|
|
|
|
|
|
cookie_route('foo=bar', 'bar=baz') do
|
|
|
|
cookies.each_value do |value|
|
|
|
|
values << value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(values.sort).to eq(['bar', 'baz'])
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'favors response over request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
value = nil
|
2011-09-04 21:13:40 -04:00
|
|
|
cookie_route('foo=bar') do
|
|
|
|
cookies[:foo] = 'baz'
|
2016-05-10 10:08:54 -04:00
|
|
|
cookies.each_value do |v|
|
|
|
|
value = v
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(value).to eq('baz')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not loop through deleted cookies' do
|
|
|
|
cookie_route('foo=bar') do
|
|
|
|
cookies.delete :foo
|
|
|
|
cookies.each_value { fail }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an enumerator' do
|
2016-05-10 10:08:54 -04:00
|
|
|
enum = nil
|
2011-09-04 21:13:40 -04:00
|
|
|
cookie_route('foo=bar') do
|
|
|
|
enum = cookies.each_value
|
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
enum.each { |value| expect(value).to eq('bar') }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :empty? do
|
|
|
|
it 'returns true if there are no cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies).to be_empty
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if there are request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies('foo=bar')).not_to be_empty
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if there are response cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
cookies.empty?
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to be false
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'becomes true if response cookies are removed' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
cookies.delete :foo
|
|
|
|
cookies.empty?
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to be true
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'becomes true if request cookies are removed' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route('foo=bar') do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies.delete :foo
|
|
|
|
cookies.empty?
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to be_truthy
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'becomes true after clear' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route('foo=bar', 'bar=baz') do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
cookies.clear
|
|
|
|
cookies.empty?
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to be_truthy
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :fetch do
|
|
|
|
it 'returns values from request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies('foo=bar').fetch('foo')).to eq('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns values from response cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
cookies.fetch('foo')
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to eq('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'favors response over request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookie_route('foo=baz') do
|
2011-09-04 21:13:40 -04:00
|
|
|
cookies['foo'] = 'bar'
|
|
|
|
cookies.fetch('foo')
|
2016-05-10 10:08:54 -04:00
|
|
|
end).to eq('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception if key does not exist' do
|
2012-06-11 18:27:40 -04:00
|
|
|
error = if defined? JRUBY_VERSION
|
|
|
|
IndexError
|
|
|
|
else
|
2016-05-07 05:57:06 -04:00
|
|
|
KeyError
|
2012-06-11 18:27:40 -04:00
|
|
|
end
|
2011-09-04 21:13:40 -04:00
|
|
|
expect { cookies.fetch('foo') }.to raise_exception(error)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the block result if missing' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies.fetch('foo') { 'bar' }).to eq('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :flatten do
|
2016-05-10 10:08:54 -04:00
|
|
|
it { expect(cookies('foo=bar').flatten).to eq({'foo' => 'bar'}.flatten) }
|
2011-09-04 21:13:40 -04:00
|
|
|
end if Hash.method_defined? :flatten
|
|
|
|
|
|
|
|
describe :has_key? do
|
|
|
|
it 'checks request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies('foo=bar')).to have_key('foo')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks response cookies' do
|
|
|
|
jar = cookies
|
|
|
|
jar['foo'] = 'bar'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).to have_key(:foo)
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not use deleted cookies' do
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.delete :foo
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).not_to have_key('foo')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :has_value? do
|
|
|
|
it 'checks request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies('foo=bar')).to have_value('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks response cookies' do
|
|
|
|
jar = cookies
|
|
|
|
jar[:foo] = 'bar'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).to have_value('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not use deleted cookies' do
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.delete :foo
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).not_to have_value('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :include? do
|
|
|
|
it 'checks request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies('foo=bar')).to include('foo')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks response cookies' do
|
|
|
|
jar = cookies
|
|
|
|
jar['foo'] = 'bar'
|
2018-10-10 12:43:43 -04:00
|
|
|
expect(jar).to include(:foo)
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not use deleted cookies' do
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.delete :foo
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).not_to include('foo')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :keep_if do
|
|
|
|
it 'removes entries' do
|
|
|
|
jar = cookies('foo=bar', 'bar=baz')
|
|
|
|
jar.keep_if { |*args| args == ['bar', 'baz'] }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).to eq({'bar' => 'baz'})
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :key do
|
|
|
|
it 'checks request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies('foo=bar').key('bar')).to eq('foo')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks response cookies' do
|
|
|
|
jar = cookies
|
|
|
|
jar['foo'] = 'bar'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar.key('bar')).to eq('foo')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil when missing' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies('foo=bar').key('baz')).to be_nil
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :key? do
|
|
|
|
it 'checks request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies('foo=bar').key?('foo')).to be true
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks response cookies' do
|
|
|
|
jar = cookies
|
|
|
|
jar['foo'] = 'bar'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar.key?(:foo)).to be true
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not use deleted cookies' do
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.delete :foo
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar.key?('foo')).to be false
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :keys do
|
2016-05-10 10:08:54 -04:00
|
|
|
it { expect(cookies('foo=bar').keys).to eq(['foo']) }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe :length do
|
2016-05-10 10:08:54 -04:00
|
|
|
it { expect(cookies.length).to eq(0) }
|
|
|
|
it { expect(cookies('foo=bar').length).to eq(1) }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe :member? do
|
|
|
|
it 'checks request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies('foo=bar').member?('foo')).to be true
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks response cookies' do
|
|
|
|
jar = cookies
|
|
|
|
jar['foo'] = 'bar'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar.member?(:foo)).to be true
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not use deleted cookies' do
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.delete :foo
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar.member?('foo')).to be false
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :merge do
|
|
|
|
it 'is mergable with a hash' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies('foo=bar').merge(:bar => :baz)).to eq({"foo" => "bar", :bar => :baz})
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create cookies' do
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.merge(:bar => 'baz')
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).not_to include(:bar)
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'takes a block for conflict resolution' do
|
|
|
|
update = {'foo' => 'baz', 'bar' => 'baz'}
|
|
|
|
merged = cookies('foo=bar').merge(update) do |key, old, other|
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(key).to eq('foo')
|
|
|
|
expect(old).to eq('bar')
|
|
|
|
expect(other).to eq('baz')
|
2011-09-04 21:13:40 -04:00
|
|
|
'foo'
|
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(merged['foo']).to eq('foo')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :merge! do
|
|
|
|
it 'creates cookies' do
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.merge! :bar => 'baz'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).to include('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'overrides existing values' do
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.merge! :foo => "baz"
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar["foo"]).to eq("baz")
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'takes a block for conflict resolution' do
|
|
|
|
update = {'foo' => 'baz', 'bar' => 'baz'}
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.merge!(update) do |key, old, other|
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(key).to eq('foo')
|
|
|
|
expect(old).to eq('bar')
|
|
|
|
expect(other).to eq('baz')
|
2011-09-04 21:13:40 -04:00
|
|
|
'foo'
|
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar['foo']).to eq('foo')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :rassoc do
|
|
|
|
it 'behaves like Hash#assoc' do
|
|
|
|
cookies('foo=bar').rassoc('bar') == ['foo', 'bar']
|
|
|
|
end
|
|
|
|
end if Hash.method_defined? :rassoc
|
|
|
|
|
|
|
|
describe :reject do
|
|
|
|
it 'removes entries from new hash' do
|
|
|
|
jar = cookies('foo=bar', 'bar=baz')
|
|
|
|
sub = jar.reject { |*args| args == ['bar', 'baz'] }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(sub).to eq({'foo' => 'bar'})
|
|
|
|
expect(jar['bar']).to eq('baz')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :reject! do
|
|
|
|
it 'removes entries' do
|
|
|
|
jar = cookies('foo=bar', 'bar=baz')
|
|
|
|
jar.reject! { |*args| args == ['bar', 'baz'] }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).to eq({'foo' => 'bar'})
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :replace do
|
|
|
|
it 'replaces entries' do
|
|
|
|
jar = cookies('foo=bar', 'bar=baz')
|
|
|
|
jar.replace 'foo' => 'baz', 'baz' => 'bar'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).to eq({'foo' => 'baz', 'baz' => 'bar'})
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-24 16:40:53 -05:00
|
|
|
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
|
|
|
|
|
2011-09-04 21:13:40 -04:00
|
|
|
describe :select do
|
|
|
|
it 'removes entries from new hash' do
|
|
|
|
jar = cookies('foo=bar', 'bar=baz')
|
|
|
|
sub = jar.select { |*args| args != ['bar', 'baz'] }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(sub).to eq({'foo' => 'bar'}.select { true })
|
|
|
|
expect(jar['bar']).to eq('baz')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :select! do
|
|
|
|
it 'removes entries' do
|
|
|
|
jar = cookies('foo=bar', 'bar=baz')
|
|
|
|
jar.select! { |*args| args != ['bar', 'baz'] }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).to eq({'foo' => 'bar'})
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end if Hash.method_defined? :select!
|
|
|
|
|
|
|
|
describe :shift do
|
|
|
|
it 'removes from the hash' do
|
|
|
|
jar = cookies('foo=bar')
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar.shift).to eq(['foo', 'bar'])
|
|
|
|
expect(jar).not_to include('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :size do
|
2016-05-10 10:08:54 -04:00
|
|
|
it { expect(cookies.size).to eq(0) }
|
|
|
|
it { expect(cookies('foo=bar').size).to eq(1) }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe :update do
|
|
|
|
it 'creates cookies' do
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.update :bar => 'baz'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar).to include('bar')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'overrides existing values' do
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.update :foo => "baz"
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar["foo"]).to eq("baz")
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'takes a block for conflict resolution' do
|
|
|
|
merge = {'foo' => 'baz', 'bar' => 'baz'}
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.update(merge) do |key, old, other|
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(key).to eq('foo')
|
|
|
|
expect(old).to eq('bar')
|
|
|
|
expect(other).to eq('baz')
|
2011-09-04 21:13:40 -04:00
|
|
|
'foo'
|
|
|
|
end
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar['foo']).to eq('foo')
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :value? do
|
|
|
|
it 'checks request cookies' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(cookies('foo=bar').value?('bar')).to be true
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks response cookies' do
|
|
|
|
jar = cookies
|
|
|
|
jar[:foo] = 'bar'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar.value?('bar')).to be true
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not use deleted cookies' do
|
|
|
|
jar = cookies('foo=bar')
|
|
|
|
jar.delete :foo
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(jar.value?('bar')).to be false
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe :values do
|
2016-05-10 10:08:54 -04:00
|
|
|
it { expect(cookies('foo=bar', 'bar=baz').values.sort).to eq(['bar', 'baz']) }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe :values_at do
|
2016-05-10 10:08:54 -04:00
|
|
|
it { expect(cookies('foo=bar', 'bar=baz').values_at('foo')).to eq(['bar']) }
|
2011-09-04 21:13:40 -04:00
|
|
|
end
|
|
|
|
end
|