Permalink
Browse files

More convenience for Rack::Request

darcs-hash:20070218105007-4fc50-17859a47ae55c4c6704dbaefa5a7209ee1e0e32d.gz
  • Loading branch information...
1 parent 116f0ef commit 05ab0f9084ebee5aeb0b47e401e0b8c3ef0e8c08 @chneukirchen chneukirchen committed Feb 18, 2007
Showing with 35 additions and 1 deletion.
  1. +11 −1 lib/rack/request.rb
  2. +24 −0 test/spec_rack_request.rb
View
@@ -2,17 +2,27 @@
module Rack
class Request
+ attr_reader :env
+
def initialize(env)
@env = env
end
def body; @env["rack.input"] end
def scheme; @env["rack.url_scheme"] end
- def method; @env["REQUEST_METHOD"] end
def script_name; @env["SCRIPT_NAME"].to_s end
def path_info; @env["PATH_INFO"].to_s end
def host; @env["HTTP_HOST"] || @env["SERVER_NAME"] end
def port; @env["SERVER_PORT"].to_i end
+ def method; @env["REQUEST_METHOD"] end
+
+ def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
+ def path_info=(s); @env["PATH_INFO"] = s.to_s end
+
+ def get?; method == "GET" end
+ def post?; method == "POST" end
+ def put?; method == "PUT" end
+ def delete?; method == "DELETE" end
def GET
Utils.parse_query(@env["QUERY_STRING"])
@@ -11,6 +11,12 @@
req.body.should.respond_to? :gets
req.scheme.should.equal "http"
req.method.should.equal "GET"
+
+ req.should.be.get
+ req.should.not.be.post
+ req.should.not.be.put
+ req.should.not.be.delete
+
req.script_name.should.equal ""
req.path_info.should.equal "/"
req.host.should.equal "example.org"
@@ -49,4 +55,22 @@
req = Rack::Request.new(TestRequest.env({"HTTP_COOKIE" => "foo=bar;quux=h&m"}))
req.cookies.should.equal "foo" => "bar", "quux" => "h&m"
end
+
+ specify "provides setters" do
+ req = Rack::Request.new(e=TestRequest.env({}))
+ req.script_name.should.equal ""
+ req.script_name = "/foo"
+ req.script_name.should.equal "/foo"
+ e["SCRIPT_NAME"].should.equal "/foo"
+
+ req.path_info.should.equal "/"
+ req.path_info = "/foo"
+ req.path_info.should.equal "/foo"
+ e["PATH_INFO"].should.equal "/foo"
+ end
+
+ specify "provides the original env" do
+ req = Rack::Request.new(e=TestRequest.env({}))
+ req.env.should.be e
+ end
end

0 comments on commit 05ab0f9

Please sign in to comment.