PageRenderTime 48ms CodeModel.GetById 11ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/jruby/2.1/gems/rack-1.5.2/test/spec_session_pool.rb

https://github.com/delowong/logstash
Ruby | 209 lines | 172 code | 35 blank | 2 comment | 7 complexity | 019bb243b6d029184481e51377a2b334 MD5 | raw file
  1require 'thread'
  2require 'rack/lint'
  3require 'rack/mock'
  4require 'rack/session/pool'
  5
  6describe Rack::Session::Pool do
  7  session_key = Rack::Session::Pool::DEFAULT_OPTIONS[:key]
  8  session_match = /#{session_key}=[0-9a-fA-F]+;/
  9
 10  incrementor = lambda do |env|
 11    env["rack.session"]["counter"] ||= 0
 12    env["rack.session"]["counter"] += 1
 13    Rack::Response.new(env["rack.session"].inspect).to_a
 14  end
 15
 16  session_id = Rack::Lint.new(lambda do |env|
 17    Rack::Response.new(env["rack.session"].inspect).to_a
 18  end)
 19
 20  nothing = Rack::Lint.new(lambda do |env|
 21    Rack::Response.new("Nothing").to_a
 22  end)
 23
 24  drop_session = Rack::Lint.new(lambda do |env|
 25    env['rack.session.options'][:drop] = true
 26    incrementor.call(env)
 27  end)
 28
 29  renew_session = Rack::Lint.new(lambda do |env|
 30    env['rack.session.options'][:renew] = true
 31    incrementor.call(env)
 32  end)
 33
 34  defer_session = Rack::Lint.new(lambda do |env|
 35    env['rack.session.options'][:defer] = true
 36    incrementor.call(env)
 37  end)
 38  
 39  incrementor = Rack::Lint.new(incrementor)
 40
 41  it "creates a new cookie" do
 42    pool = Rack::Session::Pool.new(incrementor)
 43    res = Rack::MockRequest.new(pool).get("/")
 44    res["Set-Cookie"].should.match session_match
 45    res.body.should.equal '{"counter"=>1}'
 46  end
 47
 48  it "determines session from a cookie" do
 49    pool = Rack::Session::Pool.new(incrementor)
 50    req = Rack::MockRequest.new(pool)
 51    cookie = req.get("/")["Set-Cookie"]
 52    req.get("/", "HTTP_COOKIE" => cookie).
 53      body.should.equal '{"counter"=>2}'
 54    req.get("/", "HTTP_COOKIE" => cookie).
 55      body.should.equal '{"counter"=>3}'
 56  end
 57
 58  it "survives nonexistant cookies" do
 59    pool = Rack::Session::Pool.new(incrementor)
 60    res = Rack::MockRequest.new(pool).
 61      get("/", "HTTP_COOKIE" => "#{session_key}=blarghfasel")
 62    res.body.should.equal '{"counter"=>1}'
 63  end
 64
 65  it "does not send the same session id if it did not change" do
 66    pool = Rack::Session::Pool.new(incrementor)
 67    req = Rack::MockRequest.new(pool)
 68
 69    res0 = req.get("/")
 70    cookie = res0["Set-Cookie"][session_match]
 71    res0.body.should.equal '{"counter"=>1}'
 72    pool.pool.size.should.equal 1
 73
 74    res1 = req.get("/", "HTTP_COOKIE" => cookie)
 75    res1["Set-Cookie"].should.be.nil
 76    res1.body.should.equal '{"counter"=>2}'
 77    pool.pool.size.should.equal 1
 78
 79    res2 = req.get("/", "HTTP_COOKIE" => cookie)
 80    res2["Set-Cookie"].should.be.nil
 81    res2.body.should.equal '{"counter"=>3}'
 82    pool.pool.size.should.equal 1
 83  end
 84
 85  it "deletes cookies with :drop option" do
 86    pool = Rack::Session::Pool.new(incrementor)
 87    req = Rack::MockRequest.new(pool)
 88    drop = Rack::Utils::Context.new(pool, drop_session)
 89    dreq = Rack::MockRequest.new(drop)
 90
 91    res1 = req.get("/")
 92    session = (cookie = res1["Set-Cookie"])[session_match]
 93    res1.body.should.equal '{"counter"=>1}'
 94    pool.pool.size.should.equal 1
 95
 96    res2 = dreq.get("/", "HTTP_COOKIE" => cookie)
 97    res2["Set-Cookie"].should.be.nil
 98    res2.body.should.equal '{"counter"=>2}'
 99    pool.pool.size.should.equal 0
100
101    res3 = req.get("/", "HTTP_COOKIE" => cookie)
102    res3["Set-Cookie"][session_match].should.not.equal session
103    res3.body.should.equal '{"counter"=>1}'
104    pool.pool.size.should.equal 1
105  end
106
107  it "provides new session id with :renew option" do
108    pool = Rack::Session::Pool.new(incrementor)
109    req = Rack::MockRequest.new(pool)
110    renew = Rack::Utils::Context.new(pool, renew_session)
111    rreq = Rack::MockRequest.new(renew)
112
113    res1 = req.get("/")
114    session = (cookie = res1["Set-Cookie"])[session_match]
115    res1.body.should.equal '{"counter"=>1}'
116    pool.pool.size.should.equal 1
117
118    res2 = rreq.get("/", "HTTP_COOKIE" => cookie)
119    new_cookie = res2["Set-Cookie"]
120    new_session = new_cookie[session_match]
121    new_session.should.not.equal session
122    res2.body.should.equal '{"counter"=>2}'
123    pool.pool.size.should.equal 1
124
125    res3 = req.get("/", "HTTP_COOKIE" => new_cookie)
126    res3.body.should.equal '{"counter"=>3}'
127    pool.pool.size.should.equal 1
128
129    res4 = req.get("/", "HTTP_COOKIE" => cookie)
130    res4.body.should.equal '{"counter"=>1}'
131    pool.pool.size.should.equal 2
132  end
133
134  it "omits cookie with :defer option" do
135    pool = Rack::Session::Pool.new(incrementor)
136    defer = Rack::Utils::Context.new(pool, defer_session)
137    dreq = Rack::MockRequest.new(defer)
138
139    res1 = dreq.get("/")
140    res1["Set-Cookie"].should.equal nil
141    res1.body.should.equal '{"counter"=>1}'
142    pool.pool.size.should.equal 1
143  end
144
145  # anyone know how to do this better?
146  it "should merge sessions when multithreaded" do
147    unless $DEBUG
148      1.should.equal 1
149      next
150    end
151
152    warn 'Running multithread tests for Session::Pool'
153    pool = Rack::Session::Pool.new(incrementor)
154    req = Rack::MockRequest.new(pool)
155
156    res = req.get('/')
157    res.body.should.equal '{"counter"=>1}'
158    cookie = res["Set-Cookie"]
159    sess_id = cookie[/#{pool.key}=([^,;]+)/,1]
160
161    delta_incrementor = lambda do |env|
162      # emulate disconjoinment of threading
163      env['rack.session'] = env['rack.session'].dup
164      Thread.stop
165      env['rack.session'][(Time.now.usec*rand).to_i] = true
166      incrementor.call(env)
167    end
168    tses = Rack::Utils::Context.new pool, delta_incrementor
169    treq = Rack::MockRequest.new(tses)
170    tnum = rand(7).to_i+5
171    r = Array.new(tnum) do
172      Thread.new(treq) do |run|
173        run.get('/', "HTTP_COOKIE" => cookie, 'rack.multithread' => true)
174      end
175    end.reverse.map{|t| t.run.join.value }
176    r.each do |resp|
177      resp['Set-Cookie'].should.equal cookie
178      resp.body.should.include '"counter"=>2'
179    end
180
181    session = pool.pool[sess_id]
182    session.size.should.equal tnum+1 # counter
183    session['counter'].should.equal 2 # meeeh
184  end
185
186  it "does not return a cookie if cookie was not read/written" do
187    app = Rack::Session::Pool.new(nothing)
188    res = Rack::MockRequest.new(app).get("/")
189    res["Set-Cookie"].should.be.nil
190  end
191
192  it "does not return a cookie if cookie was not written (only read)" do
193    app = Rack::Session::Pool.new(session_id)
194    res = Rack::MockRequest.new(app).get("/")
195    res["Set-Cookie"].should.be.nil
196  end
197
198  it "returns even if not read/written if :expire_after is set" do
199    app = Rack::Session::Pool.new(nothing, :expire_after => 3600)
200    res = Rack::MockRequest.new(app).get("/", 'rack.session' => {'not' => 'empty'})
201    res["Set-Cookie"].should.not.be.nil
202  end
203
204  it "returns no cookie if no data was written and no session was created previously, even if :expire_after is set" do
205    app = Rack::Session::Pool.new(nothing, :expire_after => 3600)
206    res = Rack::MockRequest.new(app).get("/")
207    res["Set-Cookie"].should.be.nil
208  end
209end