test/spec_session_pool.rb in rack-1.2.8 vs test/spec_session_pool.rb in rack-1.3.0.beta
- old
+ new
@@ -10,10 +10,18 @@
env["rack.session"]["counter"] ||= 0
env["rack.session"]["counter"] += 1
Rack::Response.new(env["rack.session"].inspect).to_a
end
+ session_id = lambda do |env|
+ Rack::Response.new(env["rack.session"].inspect).to_a
+ end
+
+ nothing = lambda do |env|
+ Rack::Response.new("Nothing").to_a
+ end
+
drop_session = lambda do |env|
env['rack.session.options'][:drop] = true
incrementor.call(env)
end
@@ -49,29 +57,44 @@
res = Rack::MockRequest.new(pool).
get("/", "HTTP_COOKIE" => "#{session_key}=blarghfasel")
res.body.should.equal '{"counter"=>1}'
end
- it "deletes cookies with :drop option" do
+ it "does not send the same session id if it did not change" do
pool = Rack::Session::Pool.new(incrementor)
req = Rack::MockRequest.new(pool)
- drop = Rack::Utils::Context.new(pool, drop_session)
- dreq = Rack::MockRequest.new(drop)
res0 = req.get("/")
- session = (cookie = res0["Set-Cookie"])[session_match]
+ cookie = res0["Set-Cookie"][session_match]
res0.body.should.equal '{"counter"=>1}'
pool.pool.size.should.equal 1
res1 = req.get("/", "HTTP_COOKIE" => cookie)
- res1["Set-Cookie"][session_match].should.equal session
+ res1["Set-Cookie"].should.be.nil
res1.body.should.equal '{"counter"=>2}'
pool.pool.size.should.equal 1
- res2 = dreq.get("/", "HTTP_COOKIE" => cookie)
- res2["Set-Cookie"].should.equal nil
+ res2 = req.get("/", "HTTP_COOKIE" => cookie)
+ res2["Set-Cookie"].should.be.nil
res2.body.should.equal '{"counter"=>3}'
+ pool.pool.size.should.equal 1
+ end
+
+ it "deletes cookies with :drop option" do
+ pool = Rack::Session::Pool.new(incrementor)
+ req = Rack::MockRequest.new(pool)
+ drop = Rack::Utils::Context.new(pool, drop_session)
+ dreq = Rack::MockRequest.new(drop)
+
+ res1 = req.get("/")
+ session = (cookie = res1["Set-Cookie"])[session_match]
+ res1.body.should.equal '{"counter"=>1}'
+ pool.pool.size.should.equal 1
+
+ res2 = dreq.get("/", "HTTP_COOKIE" => cookie)
+ res2["Set-Cookie"].should.be.nil
+ res2.body.should.equal '{"counter"=>2}'
pool.pool.size.should.equal 0
res3 = req.get("/", "HTTP_COOKIE" => cookie)
res3["Set-Cookie"][session_match].should.not.equal session
res3.body.should.equal '{"counter"=>1}'
@@ -82,58 +105,40 @@
pool = Rack::Session::Pool.new(incrementor)
req = Rack::MockRequest.new(pool)
renew = Rack::Utils::Context.new(pool, renew_session)
rreq = Rack::MockRequest.new(renew)
- res0 = req.get("/")
- session = (cookie = res0["Set-Cookie"])[session_match]
- res0.body.should.equal '{"counter"=>1}'
+ res1 = req.get("/")
+ session = (cookie = res1["Set-Cookie"])[session_match]
+ res1.body.should.equal '{"counter"=>1}'
pool.pool.size.should.equal 1
- res1 = req.get("/", "HTTP_COOKIE" => cookie)
- res1["Set-Cookie"][session_match].should.equal session
- res1.body.should.equal '{"counter"=>2}'
- pool.pool.size.should.equal 1
-
res2 = rreq.get("/", "HTTP_COOKIE" => cookie)
new_cookie = res2["Set-Cookie"]
new_session = new_cookie[session_match]
new_session.should.not.equal session
- res2.body.should.equal '{"counter"=>3}'
+ res2.body.should.equal '{"counter"=>2}'
pool.pool.size.should.equal 1
res3 = req.get("/", "HTTP_COOKIE" => new_cookie)
- res3["Set-Cookie"][session_match].should.equal new_session
- res3.body.should.equal '{"counter"=>4}'
+ res3.body.should.equal '{"counter"=>3}'
pool.pool.size.should.equal 1
+
+ res4 = req.get("/", "HTTP_COOKIE" => cookie)
+ res4.body.should.equal '{"counter"=>1}'
+ pool.pool.size.should.equal 2
end
it "omits cookie with :defer option" do
pool = Rack::Session::Pool.new(incrementor)
- req = Rack::MockRequest.new(pool)
defer = Rack::Utils::Context.new(pool, defer_session)
dreq = Rack::MockRequest.new(defer)
- res0 = req.get("/")
- session = (cookie = res0["Set-Cookie"])[session_match]
- res0.body.should.equal '{"counter"=>1}'
+ res1 = dreq.get("/")
+ res1["Set-Cookie"].should.equal nil
+ res1.body.should.equal '{"counter"=>1}'
pool.pool.size.should.equal 1
-
- res1 = req.get("/", "HTTP_COOKIE" => cookie)
- res1["Set-Cookie"][session_match].should.equal session
- res1.body.should.equal '{"counter"=>2}'
- pool.pool.size.should.equal 1
-
- res2 = dreq.get("/", "HTTP_COOKIE" => cookie)
- res2["Set-Cookie"].should.equal nil
- res2.body.should.equal '{"counter"=>3}'
- pool.pool.size.should.equal 1
-
- res3 = req.get("/", "HTTP_COOKIE" => cookie)
- res3["Set-Cookie"][session_match].should.equal session
- res3.body.should.equal '{"counter"=>4}'
- pool.pool.size.should.equal 1
end
# anyone know how to do this better?
it "should merge sessions when multithreaded" do
unless $DEBUG
@@ -163,15 +168,33 @@
r = Array.new(tnum) do
Thread.new(treq) do |run|
run.get('/', "HTTP_COOKIE" => cookie, 'rack.multithread' => true)
end
end.reverse.map{|t| t.run.join.value }
- r.each do |res|
- res['Set-Cookie'].should.equal cookie
- res.body.should.include '"counter"=>2'
+ r.each do |resp|
+ resp['Set-Cookie'].should.equal cookie
+ resp.body.should.include '"counter"=>2'
end
session = pool.pool[sess_id]
session.size.should.equal tnum+1 # counter
session['counter'].should.equal 2 # meeeh
+ end
+
+ it "does not return a cookie if cookie was not read/written" do
+ app = Rack::Session::Cookie.new(nothing)
+ res = Rack::MockRequest.new(app).get("/")
+ res["Set-Cookie"].should.be.nil
+ end
+
+ it "does not return a cookie if cookie was not written (only read)" do
+ app = Rack::Session::Cookie.new(session_id)
+ res = Rack::MockRequest.new(app).get("/")
+ res["Set-Cookie"].should.be.nil
+ end
+
+ it "returns even if not read/written if :expire_after is set" do
+ app = Rack::Session::Cookie.new(nothing, :expire_after => 3600)
+ res = Rack::MockRequest.new(app).get("/")
+ res["Set-Cookie"].should.not.be.nil
end
end