1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/sinatra-contrib/spec/cookies_spec.rb

826 lines
20 KiB
Ruby
Raw Normal View History

2014-05-09 02:39:16 -04:00
require 'spec_helper'
2011-09-04 21:13:40 -04:00
RSpec.describe Sinatra::Cookies do
Setup Rubocop (#1537) * Initialize rubocop * Style/StringLiterals: prefer single quotes * Style/AndOr: use `&&` and `||`, instead of `and` and `or` * Style/HashSyntax: use new hash syntax * Layout/EmptyLineAfterGuardClause: add empty lines after guard clause * Style/SingleLineMethods: temporary disable It breaks layout of the code, it is better to fix it manually * Style/Proc: prefer `proc` vs `Proc.new` * Disable Lint/AmbiguousBlockAssociation It affects proc definitions for sinatra DSL * Disable Style/CaseEquality * Lint/UnusedBlockArgument: put underscore in front of it * Style/Alias: prefer alias vs alias_method in a class body * Layout/EmptyLineBetweenDefs: add empty lines between defs * Style/ParallelAssignment: don't use parallel assigment * Style/RegexpLiteral: prefer %r for regular expressions * Naming/UncommunicativeMethodParamName: fix abbrevs * Style/PerlBackrefs: disable cop * Layout/SpaceAfterComma: add missing spaces * Style/Documentation: disable cop * Style/FrozenStringLiteralComment: add frozen_string_literal * Layout/AlignHash: align hash * Layout/ExtraSpacing: allow for alignment * Layout/SpaceAroundOperators: add missing spaces * Style/Not: prefer `!` instead of `not` * Style/GuardClause: add guard conditions * Style/MutableConstant: freeze contants * Lint/IneffectiveAccessModifier: disable cop * Lint/RescueException: disable cop * Style/SpecialGlobalVars: disable cop * Layout/DotPosition: fix position of dot for multiline method chains * Layout/SpaceInsideArrayLiteralBrackets: don't use spaces inside arrays * Layout/SpaceInsideBlockBraces: add space for blocks * Layout/SpaceInsideHashLiteralBraces: add spaces for hashes * Style/FormatString: use format string syntax * Style/StderrPuts: `warn` is preferable to `$stderr.puts` * Bundler/DuplicatedGem: disable cop * Layout/AlignArray: fix warning * Lint/AssignmentInCondition: remove assignments from conditions * Layout/IndentHeredoc: disable cop * Layout/SpaceInsideParens: remove extra spaces * Lint/UnusedMethodArgument: put underscore in front of unused arg * Naming/RescuedExceptionsVariableName: use `e` for exceptions * Style/CommentedKeyword: put comments before the method * Style/FormatStringToken: disable cop * Style/MultilineIfModifier: move condition before the method * Style/SignalException: prefer `raise` to `fail` * Style/SymbolArray: prefer %i for array of symbols * Gemspec/OrderedDependencies: Use alphabetical order for dependencies * Lint/UselessAccessModifier: disable cop * Naming/HeredocDelimiterNaming: change delimiter's name * Style/ClassCheck: prefer `is_a?` to `kind_of?` * Style/ClassVars: disable cop * Style/Encoding: remove coding comment * Style/RedundantParentheses: remove extra parentheses * Style/StringLiteralsInInterpolation: prefer singl quotes * Layout/AlignArguments: fix alignment * Layout/ClosingHeredocIndentation: align heredoc * Layout/EmptyLineAfterMagicComment: add empty line * Set RubyVersion for rubocop * Lint/UselessAssignment: disable cop * Style/EmptyLiteral: disable cop Causes test failures * Minor code-style fixes with --safe-auto-correct option * Disable the rest of the cops that cause warnings It would be easier to re-enable them in separate PRs * Add rubocop check to the default Rake task * Update to rubocop 1.32.0 * Rubocop updates for rack-protection and sinatra-contrib * Disable Style/SlicingWithRange cop * Make suggested updates Co-authored-by: Jordan Owens <jkowens@gmail.com>
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
Setup Rubocop (#1537) * Initialize rubocop * Style/StringLiterals: prefer single quotes * Style/AndOr: use `&&` and `||`, instead of `and` and `or` * Style/HashSyntax: use new hash syntax * Layout/EmptyLineAfterGuardClause: add empty lines after guard clause * Style/SingleLineMethods: temporary disable It breaks layout of the code, it is better to fix it manually * Style/Proc: prefer `proc` vs `Proc.new` * Disable Lint/AmbiguousBlockAssociation It affects proc definitions for sinatra DSL * Disable Style/CaseEquality * Lint/UnusedBlockArgument: put underscore in front of it * Style/Alias: prefer alias vs alias_method in a class body * Layout/EmptyLineBetweenDefs: add empty lines between defs * Style/ParallelAssignment: don't use parallel assigment * Style/RegexpLiteral: prefer %r for regular expressions * Naming/UncommunicativeMethodParamName: fix abbrevs * Style/PerlBackrefs: disable cop * Layout/SpaceAfterComma: add missing spaces * Style/Documentation: disable cop * Style/FrozenStringLiteralComment: add frozen_string_literal * Layout/AlignHash: align hash * Layout/ExtraSpacing: allow for alignment * Layout/SpaceAroundOperators: add missing spaces * Style/Not: prefer `!` instead of `not` * Style/GuardClause: add guard conditions * Style/MutableConstant: freeze contants * Lint/IneffectiveAccessModifier: disable cop * Lint/RescueException: disable cop * Style/SpecialGlobalVars: disable cop * Layout/DotPosition: fix position of dot for multiline method chains * Layout/SpaceInsideArrayLiteralBrackets: don't use spaces inside arrays * Layout/SpaceInsideBlockBraces: add space for blocks * Layout/SpaceInsideHashLiteralBraces: add spaces for hashes * Style/FormatString: use format string syntax * Style/StderrPuts: `warn` is preferable to `$stderr.puts` * Bundler/DuplicatedGem: disable cop * Layout/AlignArray: fix warning * Lint/AssignmentInCondition: remove assignments from conditions * Layout/IndentHeredoc: disable cop * Layout/SpaceInsideParens: remove extra spaces * Lint/UnusedMethodArgument: put underscore in front of unused arg * Naming/RescuedExceptionsVariableName: use `e` for exceptions * Style/CommentedKeyword: put comments before the method * Style/FormatStringToken: disable cop * Style/MultilineIfModifier: move condition before the method * Style/SignalException: prefer `raise` to `fail` * Style/SymbolArray: prefer %i for array of symbols * Gemspec/OrderedDependencies: Use alphabetical order for dependencies * Lint/UselessAccessModifier: disable cop * Naming/HeredocDelimiterNaming: change delimiter's name * Style/ClassCheck: prefer `is_a?` to `kind_of?` * Style/ClassVars: disable cop * Style/Encoding: remove coding comment * Style/RedundantParentheses: remove extra parentheses * Style/StringLiteralsInInterpolation: prefer singl quotes * Layout/AlignArguments: fix alignment * Layout/ClosingHeredocIndentation: align heredoc * Layout/EmptyLineAfterMagicComment: add empty line * Set RubyVersion for rubocop * Lint/UselessAssignment: disable cop * Style/EmptyLiteral: disable cop Causes test failures * Minor code-style fixes with --safe-auto-correct option * Disable the rest of the cops that cause warnings It would be easier to re-enable them in separate PRs * Add rubocop check to the default Rake task * Update to rubocop 1.32.0 * Rubocop updates for rack-protection and sinatra-contrib * Disable Style/SlicingWithRange cop * Make suggested updates Co-authored-by: Jordan Owens <jkowens@gmail.com>
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
it 'sets domain to nil if localhost' do
Setup Rubocop (#1537) * Initialize rubocop * Style/StringLiterals: prefer single quotes * Style/AndOr: use `&&` and `||`, instead of `and` and `or` * Style/HashSyntax: use new hash syntax * Layout/EmptyLineAfterGuardClause: add empty lines after guard clause * Style/SingleLineMethods: temporary disable It breaks layout of the code, it is better to fix it manually * Style/Proc: prefer `proc` vs `Proc.new` * Disable Lint/AmbiguousBlockAssociation It affects proc definitions for sinatra DSL * Disable Style/CaseEquality * Lint/UnusedBlockArgument: put underscore in front of it * Style/Alias: prefer alias vs alias_method in a class body * Layout/EmptyLineBetweenDefs: add empty lines between defs * Style/ParallelAssignment: don't use parallel assigment * Style/RegexpLiteral: prefer %r for regular expressions * Naming/UncommunicativeMethodParamName: fix abbrevs * Style/PerlBackrefs: disable cop * Layout/SpaceAfterComma: add missing spaces * Style/Documentation: disable cop * Style/FrozenStringLiteralComment: add frozen_string_literal * Layout/AlignHash: align hash * Layout/ExtraSpacing: allow for alignment * Layout/SpaceAroundOperators: add missing spaces * Style/Not: prefer `!` instead of `not` * Style/GuardClause: add guard conditions * Style/MutableConstant: freeze contants * Lint/IneffectiveAccessModifier: disable cop * Lint/RescueException: disable cop * Style/SpecialGlobalVars: disable cop * Layout/DotPosition: fix position of dot for multiline method chains * Layout/SpaceInsideArrayLiteralBrackets: don't use spaces inside arrays * Layout/SpaceInsideBlockBraces: add space for blocks * Layout/SpaceInsideHashLiteralBraces: add spaces for hashes * Style/FormatString: use format string syntax * Style/StderrPuts: `warn` is preferable to `$stderr.puts` * Bundler/DuplicatedGem: disable cop * Layout/AlignArray: fix warning * Lint/AssignmentInCondition: remove assignments from conditions * Layout/IndentHeredoc: disable cop * Layout/SpaceInsideParens: remove extra spaces * Lint/UnusedMethodArgument: put underscore in front of unused arg * Naming/RescuedExceptionsVariableName: use `e` for exceptions * Style/CommentedKeyword: put comments before the method * Style/FormatStringToken: disable cop * Style/MultilineIfModifier: move condition before the method * Style/SignalException: prefer `raise` to `fail` * Style/SymbolArray: prefer %i for array of symbols * Gemspec/OrderedDependencies: Use alphabetical order for dependencies * Lint/UselessAccessModifier: disable cop * Naming/HeredocDelimiterNaming: change delimiter's name * Style/ClassCheck: prefer `is_a?` to `kind_of?` * Style/ClassVars: disable cop * Style/Encoding: remove coding comment * Style/RedundantParentheses: remove extra parentheses * Style/StringLiteralsInInterpolation: prefer singl quotes * Layout/AlignArguments: fix alignment * Layout/ClosingHeredocIndentation: align heredoc * Layout/EmptyLineAfterMagicComment: add empty line * Set RubyVersion for rubocop * Lint/UselessAssignment: disable cop * Style/EmptyLiteral: disable cop Causes test failures * Minor code-style fixes with --safe-auto-correct option * Disable the rest of the cops that cause warnings It would be easier to re-enable them in separate PRs * Add rubocop check to the default Rake task * Update to rubocop 1.32.0 * Rubocop updates for rack-protection and sinatra-contrib * Disable Style/SlicingWithRange cop * Make suggested updates Co-authored-by: Jordan Owens <jkowens@gmail.com>
2022-07-31 08:56:44 -04:00
headers = {'HTTP_HOST' => 'localhost'}
expect(cookie_route(headers: headers) do
cookies['foo'] = 'bar'
response['Set-Cookie']
2016-05-10 10:08:54 -04:00
end).not_to include("domain")
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
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'
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
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")
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
error = if defined? JRUBY_VERSION
IndexError
else
2016-05-07 05:57:06 -04:00
KeyError
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'
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
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