Skip to content

Commit

Permalink
fix specs for ruby / rack head
Browse files Browse the repository at this point in the history
  • Loading branch information
danmayer committed Oct 16, 2024
1 parent 23d36b9 commit bc65e07
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions test/test_rack_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
Rack::Session::Dalli.new(incrementor)
end

def clean_body(body)
body.lines.reject { |line| line.start_with?('#') }.join
end

let(:session_key) { Rack::Session::Dalli::DEFAULT_OPTIONS[:key] }
let(:session_match) do
/#{session_key}=([0-9a-fA-F]+);/
Expand Down Expand Up @@ -114,7 +118,7 @@
res = Rack::MockRequest.new(rsd).get('/')

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

it 'determines session from a cookie' do
Expand All @@ -123,8 +127,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}', clean_body(req.get('/', 'HTTP_COOKIE' => cookie).body)
assert_equal '{"counter"=>3}', clean_body(req.get('/', 'HTTP_COOKIE' => cookie).body)
end

it 'determines session only from a cookie by default' do
Expand All @@ -133,8 +137,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}', clean_body(req.get("/?rack.session=#{sid}").body)
assert_equal '{"counter"=>1}', clean_body(req.get("/?rack.session=#{sid}").body)
end

it 'determines session from params' do
Expand All @@ -143,8 +147,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}', clean_body(req.get("/?rack.session=#{sid}").body)
assert_equal '{"counter"=>3}', clean_body(req.get("/?rack.session=#{sid}").body)
end

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

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

refute_match(/#{bad_cookie}/, cookie)
Expand Down Expand Up @@ -208,17 +212,17 @@
res0 = req.get('/')
cookie = res0['Set-Cookie'][session_match]

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

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

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

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

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

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

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

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

assert_nil res2['Set-Cookie']
assert_equal '{"counter"=>2}', res2.body
assert_equal '{"counter"=>2}', clean_body(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}', clean_body(res3.body)
end

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

assert_equal '{"counter"=>1}', res1.body
assert_equal '{"counter"=>1}', clean_body(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}', clean_body(res2.body)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

refute_nil ses0
assert_equal '{"a"=>"b", "c"=>{"d"=>"e"}, "f"=>{"g"=>{"h"=>"i"}}, "test"=>true}', ses0.to_s
assert_equal '{"a"=>"b", "c"=>{"d"=>"e"}, "f"=>{"g"=>{"h"=>"i"}}, "test"=>true}', clean_body(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
assert_equal '{"a"=>"b", "c"=>{"d"=>"e"}, "f"=>{"g"=>{"h"=>"j"}}, "test"=>true}', clean_body(ses1.to_s)

refute_equal ses0, ses1
end
Expand Down

0 comments on commit bc65e07

Please sign in to comment.