Skip to content

Commit

Permalink
Fix formatting so tests pass on head
Browse files Browse the repository at this point in the history
  • Loading branch information
petergoldstein committed Dec 22, 2024
1 parent f72450a commit 450fc98
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions test/test_rack_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
res = Rack::MockRequest.new(rsd).get('/')

assert_includes res['Set-Cookie'], "#{session_key}="
assert_equal '{"counter"=>1}', res.body
assert_equal ({ 'counter' => 1 }).to_s, res.body
end

it 'determines session from a cookie' do
Expand All @@ -123,8 +123,8 @@
res = req.get('/')
cookie = res['Set-Cookie']

assert_equal '{"counter"=>2}', req.get('/', 'HTTP_COOKIE' => cookie).body
assert_equal '{"counter"=>3}', req.get('/', 'HTTP_COOKIE' => cookie).body
assert_equal ({ 'counter' => 2 }).to_s, req.get('/', 'HTTP_COOKIE' => cookie).body
assert_equal ({ 'counter' => 3 }).to_s, req.get('/', 'HTTP_COOKIE' => cookie).body
end

it 'determines session only from a cookie by default' do
Expand All @@ -133,8 +133,8 @@
res = req.get('/')
sid = res['Set-Cookie'][session_match, 1]

assert_equal '{"counter"=>1}', req.get("/?rack.session=#{sid}").body
assert_equal '{"counter"=>1}', req.get("/?rack.session=#{sid}").body
assert_equal ({ 'counter' => 1 }).to_s, req.get("/?rack.session=#{sid}").body
assert_equal ({ 'counter' => 1 }).to_s, req.get("/?rack.session=#{sid}").body
end

it 'determines session from params' do
Expand All @@ -143,8 +143,8 @@
res = req.get('/')
sid = res['Set-Cookie'][session_match, 1]

assert_equal '{"counter"=>2}', req.get("/?rack.session=#{sid}").body
assert_equal '{"counter"=>3}', req.get("/?rack.session=#{sid}").body
assert_equal ({ 'counter' => 2 }).to_s, req.get("/?rack.session=#{sid}").body
assert_equal ({ 'counter' => 3 }).to_s, req.get("/?rack.session=#{sid}").body
end

it 'survives nonexistant cookies' do
Expand All @@ -153,7 +153,7 @@
res = Rack::MockRequest.new(rsd)
.get('/', 'HTTP_COOKIE' => bad_cookie)

assert_equal '{"counter"=>1}', res.body
assert_equal ({ 'counter' => 1 }).to_s, res.body
cookie = res['Set-Cookie'][session_match]

refute_match(/#{bad_cookie}/, cookie)
Expand All @@ -173,32 +173,32 @@
rsd = Rack::Session::Dalli.new(incrementor, expire_after: 3)
res = Rack::MockRequest.new(rsd).get('/')

assert_includes res.body, '"counter"=>1'
assert_includes res.body, { 'counter' => 1 }.to_s
cookie = res['Set-Cookie']
puts 'Sleeping to expire session' if $DEBUG
sleep 4
res = Rack::MockRequest.new(rsd).get('/', 'HTTP_COOKIE' => cookie)

refute_equal cookie, res['Set-Cookie']
assert_includes res.body, '"counter"=>1'
assert_includes res.body, { 'counter' => 1 }.to_s
end

it 'maintains freshness of existing sessions' do
rsd = Rack::Session::Dalli.new(incrementor, expire_after: 3)
res = Rack::MockRequest.new(rsd).get('/')

assert_includes res.body, '"counter"=>1'
assert_includes res.body, { 'counter' => 1 }.to_s
cookie = res['Set-Cookie']
res = Rack::MockRequest.new(rsd).get('/', 'HTTP_COOKIE' => cookie)

assert_equal cookie, res['Set-Cookie']
assert_includes res.body, '"counter"=>2'
assert_includes res.body, { 'counter' => 2 }.to_s
puts 'Sleeping to expire session' if $DEBUG
sleep 4
res = Rack::MockRequest.new(rsd).get('/', 'HTTP_COOKIE' => cookie)

refute_equal cookie, res['Set-Cookie']
assert_includes res.body, '"counter"=>1'
assert_includes res.body, { 'counter' => 1 }.to_s
end

it 'does not send the same session id if it did not change' do
Expand All @@ -208,17 +208,17 @@
res0 = req.get('/')
cookie = res0['Set-Cookie'][session_match]

assert_equal '{"counter"=>1}', res0.body
assert_equal ({ 'counter' => 1 }).to_s, res0.body

res1 = req.get('/', 'HTTP_COOKIE' => cookie)

assert_nil res1['Set-Cookie']
assert_equal '{"counter"=>2}', res1.body
assert_equal ({ 'counter' => 2 }).to_s, res1.body

res2 = req.get('/', 'HTTP_COOKIE' => cookie)

assert_nil res2['Set-Cookie']
assert_equal '{"counter"=>3}', res2.body
assert_equal ({ 'counter' => 3 }).to_s, res2.body
end

it 'deletes cookies with :drop option' do
Expand All @@ -230,17 +230,17 @@
res1 = req.get('/')
session = (cookie = res1['Set-Cookie'])[session_match]

assert_equal '{"counter"=>1}', res1.body
assert_equal ({ 'counter' => 1 }).to_s, res1.body

res2 = dreq.get('/', 'HTTP_COOKIE' => cookie)

assert_nil res2['Set-Cookie']
assert_equal '{"counter"=>2}', res2.body
assert_equal ({ 'counter' => 2 }).to_s, res2.body

res3 = req.get('/', 'HTTP_COOKIE' => cookie)

refute_equal session, res3['Set-Cookie'][session_match]
assert_equal '{"counter"=>1}', res3.body
assert_equal ({ 'counter' => 1 }).to_s, res3.body
end

it 'provides new session id with :renew option' do
Expand All @@ -252,23 +252,23 @@
res1 = req.get('/')
session = (cookie = res1['Set-Cookie'])[session_match]

assert_equal '{"counter"=>1}', res1.body
assert_equal ({ 'counter' => 1 }).to_s, res1.body

res2 = rreq.get('/', 'HTTP_COOKIE' => cookie)
new_cookie = res2['Set-Cookie']
new_session = new_cookie[session_match]

refute_equal session, new_session
assert_equal '{"counter"=>2}', res2.body
assert_equal ({ 'counter' => 2 }).to_s, res2.body

res3 = req.get('/', 'HTTP_COOKIE' => new_cookie)

assert_equal '{"counter"=>3}', res3.body
assert_equal ({ 'counter' => 3 }).to_s, res3.body

# Old cookie was deleted
res4 = req.get('/', 'HTTP_COOKIE' => cookie)

assert_equal '{"counter"=>1}', res4.body
assert_equal ({ 'counter' => 1 }).to_s, res4.body
end

it 'omits cookie with :defer option but still updates the state' do
Expand All @@ -281,15 +281,15 @@
res0 = dreq.get('/')

assert_nil res0['Set-Cookie']
assert_equal '{"counter"=>1}', res0.body
assert_equal ({ 'counter' => 1 }).to_s, res0.body

res0 = creq.get('/')
res1 = dreq.get('/', 'HTTP_COOKIE' => res0['Set-Cookie'])

assert_equal '{"counter"=>2}', res1.body
assert_equal ({ 'counter' => 2 }).to_s, res1.body
res2 = dreq.get('/', 'HTTP_COOKIE' => res0['Set-Cookie'])

assert_equal '{"counter"=>3}', res2.body
assert_equal ({ 'counter' => 3 }).to_s, res2.body
end

it 'omits cookie and state update with :skip option' do
Expand All @@ -302,15 +302,15 @@
res0 = sreq.get('/')

assert_nil res0['Set-Cookie']
assert_equal '{"counter"=>1}', res0.body
assert_equal ({ 'counter' => 1 }).to_s, res0.body

res0 = creq.get('/')
res1 = sreq.get('/', 'HTTP_COOKIE' => res0['Set-Cookie'])

assert_equal '{"counter"=>2}', res1.body
assert_equal ({ 'counter' => 2 }).to_s, res1.body
res2 = sreq.get('/', 'HTTP_COOKIE' => res0['Set-Cookie'])

assert_equal '{"counter"=>2}', res2.body
assert_equal ({ 'counter' => 2 }).to_s, res2.body
end

it 'updates deep hashes correctly' do
Expand All @@ -332,13 +332,17 @@
ses0 = JSON.parse(res0.body)

refute_nil ses0
assert_equal '{"a"=>"b", "c"=>{"d"=>"e"}, "f"=>{"g"=>{"h"=>"i"}}, "test"=>true}', ses0.to_s
h = { 'a' => 'b', 'c' => { 'd' => 'e' }, 'f' => { 'g' => { 'h' => 'i' } }, 'test' => true }

assert_equal h.to_s, ses0.to_s

res1 = req.get('/', 'HTTP_COOKIE' => cookie)
ses1 = JSON.parse(res1.body)

refute_nil ses1
assert_equal '{"a"=>"b", "c"=>{"d"=>"e"}, "f"=>{"g"=>{"h"=>"j"}}, "test"=>true}', ses1.to_s
h = { 'a' => 'b', 'c' => { 'd' => 'e' }, 'f' => { 'g' => { 'h' => 'j' } }, 'test' => true }

assert_equal h.to_s, ses1.to_s

refute_equal ses0, ses1
end
Expand Down

0 comments on commit 450fc98

Please sign in to comment.