Skip to content
Browse files

Fix Rack::Session::Cookie

darcs-hash:20070310143850-4fc50-2a23e7d6893c7d93ce512505be794cabb1e540db.gz
  • Loading branch information...
1 parent 417ac6a commit cb306d7db5edd34276f8f5ae52f0e3244b366f49 @chneukirchen committed Mar 10, 2007
Showing with 40 additions and 3 deletions.
  1. +1 −0 AUTHORS
  2. +1 −0 README
  3. +4 −0 lib/rack.rb
  4. +3 −3 lib/rack/session/cookie.rb
  5. +31 −0 test/spec_rack_session_cookie.rb
View
1 AUTHORS
@@ -1,4 +1,5 @@
* Christian Neukirchen <chneukirchen@gmail.com>
* Rails adapter: Christoffer Sawicki <christoffer.sawicki@gmail.com>
* HTTP authentication: Tim Fletcher <twoggle@gmail.com>
+* Cookie sessions: Luc Heinrich <luc@honk-honk.com>
* Official Logo: Armin Ronacher
View
1 README
@@ -102,6 +102,7 @@ at my site:
* XXX: Second public release 0.2.
* Bug fixes in the Camping handler.
* HTTP Basic authentication.
+ * Cookie Sessions.
== Contact
View
4 lib/rack.rb
@@ -43,6 +43,10 @@ module Auth
autoload :Request, "rack/auth/request"
end
+ module Session
+ autoload :Cookie, "rack/session/cookie"
+ end
+
# *Adapters* connect Rack with third party web frameworks.
#
# Rack includes adapters for Camping and Rails.
View
6 lib/rack/session/cookie.rb
@@ -19,7 +19,7 @@ module Session
class Cookie
- def initialize(app, options)
+ def initialize(app, options={})
@app = app
@key = options[:key] || "rack.session"
@default_options = {:domain => nil,
@@ -50,7 +50,7 @@ def load_session(env)
end
def commit_session(env, status, headers, body)
- session_data = Marshal.dump(env["rack.session_hash"])
+ session_data = Marshal.dump(env["rack.session"])
session_data = Base64.encode64(session_data)
if session_data.size > (4096 - @key.size)
@@ -71,4 +71,4 @@ def commit_session(env, status, headers, body)
end
- end
+ end
View
31 test/spec_rack_session_cookie.rb
@@ -0,0 +1,31 @@
+require 'test/spec'
+
+require 'rack/session/cookie'
+require 'rack/mock'
+require 'rack/response'
+
+context "Rack::Session::Cookie" do
+ incrementor = lambda { |env|
+ env["rack.session"]["counter"] ||= 0
+ env["rack.session"]["counter"] += 1
+ Rack::Response.new(env["rack.session"].inspect).to_a
+ }
+
+ specify "creates a new cookie" do
+ res = Rack::MockRequest.new(Rack::Session::Cookie.new(incrementor)).get("/")
+ res["Set-Cookie"].should.match("rack.session=")
+ res.body.should.equal '{"counter"=>1}'
+ end
+
+ specify "loads from a cookie" do
+ res = Rack::MockRequest.new(Rack::Session::Cookie.new(incrementor)).get("/")
+ cookie = res["Set-Cookie"]
+ res = Rack::MockRequest.new(Rack::Session::Cookie.new(incrementor)).
+ get("/", "HTTP_COOKIE" => cookie)
+ res.body.should.equal '{"counter"=>2}'
+ cookie = res["Set-Cookie"]
+ res = Rack::MockRequest.new(Rack::Session::Cookie.new(incrementor)).
+ get("/", "HTTP_COOKIE" => cookie)
+ res.body.should.equal '{"counter"=>3}'
+ end
+end

0 comments on commit cb306d7

Please sign in to comment.
Something went wrong with that request. Please try again.