Skip to content

Commit

Permalink
Cache the parsed things in Rack::Request
Browse files Browse the repository at this point in the history
darcs-hash:20070219102328-4fc50-7ab0cded7d668f04d3427e7fc55158c61e28b003.gz
  • Loading branch information
leahneukirchen committed Feb 19, 2007
1 parent a18fc32 commit 6c80c6c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/rack/request.rb
Expand Up @@ -25,20 +25,39 @@ def put?; request_method == "PUT" end
def delete?; request_method == "DELETE" end

def GET
Utils.parse_query(@env["QUERY_STRING"])
if @env["rack.request.query_string"] == @env["QUERY_STRING"]
@env["rack.request.query_hash"]
else
@env["rack.request.query_string"] = @env["QUERY_STRING"]
@env["rack.request.query_hash"] =
Utils.parse_query(@env["QUERY_STRING"])
end
end

def POST
@env["rack.request.formvars"] ||= body.read
Utils.parse_query(@env["rack.request.formvars"])
if @env["rack.request.form_input"] == @env["rack.input"]
@env["rack.request.form_hash"]
else
@env["rack.request.form_input"] = @env["rack.input"]
@env["rack.request.form_vars"] = body.read
@env["rack.request.form_hash"] =
Utils.parse_query(@env["rack.request.form_vars"])
end
end

def params
self.GET.update(self.POST)
end

def cookies
Utils.parse_query(@env["HTTP_COOKIE"], ';,') # XXX sure?
if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
@env["rack.request.cookie_hash"]
else
@env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
# XXX sure?
@env["rack.request.cookie_hash"] =
Utils.parse_query(@env["rack.request.cookie_string"], ';,')
end
end

def xhr?
Expand Down
20 changes: 20 additions & 0 deletions test/spec_rack_request.rb
Expand Up @@ -43,6 +43,23 @@
req.params.should.equal "foo" => "bar", "quux" => "bla"
end


specify "can cache, but invalidates the cache" do
req = Rack::Request.new(TestRequest.env("QUERY_STRING"=>"foo=quux",
"rack.input" => StringIO.new("foo=bar&quux=bla")))
req.GET.should.equal "foo" => "quux"
req.GET.should.equal "foo" => "quux"
req.env["QUERY_STRING"] = "bla=foo"
req.GET.should.equal "bla" => "foo"
req.GET.should.equal "bla" => "foo"

req.POST.should.equal "foo" => "bar", "quux" => "bla"
req.POST.should.equal "foo" => "bar", "quux" => "bla"
req.env["rack.input"] = StringIO.new("foo=bla&quux=bar")
req.POST.should.equal "foo" => "bla", "quux" => "bar"
req.POST.should.equal "foo" => "bla", "quux" => "bar"
end

specify "can figure out if called via XHR" do
req = Rack::Request.new(TestRequest.env({}))
req.should.not.be.xhr
Expand All @@ -54,6 +71,9 @@
specify "can parse cookies" do
req = Rack::Request.new(TestRequest.env({"HTTP_COOKIE" => "foo=bar;quux=h&m"}))
req.cookies.should.equal "foo" => "bar", "quux" => "h&m"
req.cookies.should.equal "foo" => "bar", "quux" => "h&m"
req.env.delete("HTTP_COOKIE")
req.cookies.should.equal({})
end

specify "provides setters" do
Expand Down

0 comments on commit 6c80c6c

Please sign in to comment.