test/spec_session_memcache.rb in rack-1.2.8 vs test/spec_session_memcache.rb in rack-1.3.0.beta

- old
+ new

@@ -22,20 +22,17 @@ defer_session = proc do |env| env['rack.session.options'][:defer] = true incrementor.call(env) end + # test memcache connection + Rack::Session::Memcache.new(incrementor) + it "faults on no connection" do - if RUBY_VERSION < "1.9" - lambda{ - Rack::Session::Memcache.new(incrementor, :memcache_server => 'nosuchserver') - }.should.raise - else - lambda{ - Rack::Session::Memcache.new(incrementor, :memcache_server => 'nosuchserver') - }.should.raise ArgumentError - end + lambda{ + Rack::Session::Memcache.new(incrementor, :memcache_server => 'nosuchserver') + }.should.raise end it "connects to existing server" do test_pool = MemCache.new(incrementor, :namespace => 'test:rack:session') test_pool.namespace.should.equal 'test:rack:session' @@ -62,10 +59,32 @@ body.should.equal '{"counter"=>2}' req.get("/", "HTTP_COOKIE" => cookie). body.should.equal '{"counter"=>3}' end + it "determines session only from a cookie by default" do + pool = Rack::Session::Memcache.new(incrementor) + req = Rack::MockRequest.new(pool) + res = req.get("/") + sid = res["Set-Cookie"][session_match, 1] + req.get("/?rack.session=#{sid}"). + body.should.equal '{"counter"=>1}' + req.get("/?rack.session=#{sid}"). + body.should.equal '{"counter"=>1}' + end + + it "determines session from params" do + pool = Rack::Session::Memcache.new(incrementor, :cookie_only => false) + req = Rack::MockRequest.new(pool) + res = req.get("/") + sid = res["Set-Cookie"][session_match, 1] + req.get("/?rack.session=#{sid}"). + body.should.equal '{"counter"=>2}' + req.get("/?rack.session=#{sid}"). + body.should.equal '{"counter"=>3}' + end + it "survives nonexistant cookies" do bad_cookie = "rack.session=blarghfasel" pool = Rack::Session::Memcache.new(incrementor) res = Rack::MockRequest.new(pool). get("/", "HTTP_COOKIE" => bad_cookie) @@ -87,27 +106,40 @@ res = Rack::MockRequest.new(pool).get('/', "HTTP_COOKIE" => cookie) res["Set-Cookie"].should.not.equal cookie res.body.should.include '"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::Memcache.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}' 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}' + res2 = req.get("/", "HTTP_COOKIE" => cookie) + res2["Set-Cookie"].should.be.nil + res2.body.should.equal '{"counter"=>3}' + end + + it "deletes cookies with :drop option" do + pool = Rack::Session::Memcache.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}' + res2 = dreq.get("/", "HTTP_COOKIE" => cookie) res2["Set-Cookie"].should.equal nil - res2.body.should.equal '{"counter"=>3}' + res2.body.should.equal '{"counter"=>2}' res3 = req.get("/", "HTTP_COOKIE" => cookie) res3["Set-Cookie"][session_match].should.not.equal session res3.body.should.equal '{"counter"=>1}' end @@ -116,72 +148,57 @@ pool = Rack::Session::Memcache.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}' - res1 = req.get("/", "HTTP_COOKIE" => cookie) - res1["Set-Cookie"][session_match].should.equal session - res1.body.should.equal '{"counter"=>2}' - 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}' 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}' + + # Old cookie was deleted + res4 = req.get("/", "HTTP_COOKIE" => cookie) + res4.body.should.equal '{"counter"=>1}' end it "omits cookie with :defer option" do pool = Rack::Session::Memcache.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 = dreq.get("/") + res0["Set-Cookie"].should.equal nil res0.body.should.equal '{"counter"=>1}' - - res1 = req.get("/", "HTTP_COOKIE" => cookie) - res1["Set-Cookie"][session_match].should.equal session - res1.body.should.equal '{"counter"=>2}' - - res2 = dreq.get("/", "HTTP_COOKIE" => cookie) - res2["Set-Cookie"].should.equal nil - res2.body.should.equal '{"counter"=>3}' - - res3 = req.get("/", "HTTP_COOKIE" => cookie) - res3["Set-Cookie"][session_match].should.equal session - res3.body.should.equal '{"counter"=>4}' end it "updates deep hashes correctly" do - store = nil hash_check = proc do |env| session = env['rack.session'] unless session.include? 'test' session.update :a => :b, :c => { :d => :e }, :f => { :g => { :h => :i} }, 'test' => true else session[:f][:g][:h] = :j end - [200, {}, session.inspect] + [200, {}, [session.inspect]] end pool = Rack::Session::Memcache.new(hash_check) req = Rack::MockRequest.new(pool) res0 = req.get("/") session_id = (cookie = res0["Set-Cookie"])[session_match, 1] ses0 = pool.pool.get(session_id, true) - res1 = req.get("/", "HTTP_COOKIE" => cookie) + req.get("/", "HTTP_COOKIE" => cookie) ses1 = pool.pool.get(session_id, true) ses1.should.not.equal ses0 end @@ -224,16 +241,9 @@ session.size.should.equal tnum+1 # counter session['counter'].should.equal 2 # meeeh tnum = rand(7).to_i+5 r = Array.new(tnum) do |i| - delta_time = proc do |env| - env['rack.session'][i] = Time.now - Thread.stop - env['rack.session'] = env['rack.session'].dup - env['rack.session'][i] -= Time.now - incrementor.call(env) - end app = Rack::Utils::Context.new pool, time_delta req = Rack::MockRequest.new app Thread.new(req) do |run| run.get('/', "HTTP_COOKIE" => cookie, 'rack.multithread' => true) end