Skip to content
This repository
Browse code

More convenience for Rack::Request

darcs-hash:20070218105007-4fc50-17859a47ae55c4c6704dbaefa5a7209ee1e0e32d.gz
  • Loading branch information...
commit 05ab0f9084ebee5aeb0b47e401e0b8c3ef0e8c08 1 parent 116f0ef
Christian Neukirchen chneukirchen authored

Showing 2 changed files with 35 additions and 1 deletion. Show diff stats Hide diff stats

  1. +11 1 lib/rack/request.rb
  2. +24 0 test/spec_rack_request.rb
12 lib/rack/request.rb
@@ -2,17 +2,27 @@
2 2
3 3 module Rack
4 4 class Request
  5 + attr_reader :env
  6 +
5 7 def initialize(env)
6 8 @env = env
7 9 end
8 10
9 11 def body; @env["rack.input"] end
10 12 def scheme; @env["rack.url_scheme"] end
11   - def method; @env["REQUEST_METHOD"] end
12 13 def script_name; @env["SCRIPT_NAME"].to_s end
13 14 def path_info; @env["PATH_INFO"].to_s end
14 15 def host; @env["HTTP_HOST"] || @env["SERVER_NAME"] end
15 16 def port; @env["SERVER_PORT"].to_i end
  17 + def method; @env["REQUEST_METHOD"] end
  18 +
  19 + def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
  20 + def path_info=(s); @env["PATH_INFO"] = s.to_s end
  21 +
  22 + def get?; method == "GET" end
  23 + def post?; method == "POST" end
  24 + def put?; method == "PUT" end
  25 + def delete?; method == "DELETE" end
16 26
17 27 def GET
18 28 Utils.parse_query(@env["QUERY_STRING"])
24 test/spec_rack_request.rb
@@ -11,6 +11,12 @@
11 11 req.body.should.respond_to? :gets
12 12 req.scheme.should.equal "http"
13 13 req.method.should.equal "GET"
  14 +
  15 + req.should.be.get
  16 + req.should.not.be.post
  17 + req.should.not.be.put
  18 + req.should.not.be.delete
  19 +
14 20 req.script_name.should.equal ""
15 21 req.path_info.should.equal "/"
16 22 req.host.should.equal "example.org"
@@ -49,4 +55,22 @@
49 55 req = Rack::Request.new(TestRequest.env({"HTTP_COOKIE" => "foo=bar;quux=h&m"}))
50 56 req.cookies.should.equal "foo" => "bar", "quux" => "h&m"
51 57 end
  58 +
  59 + specify "provides setters" do
  60 + req = Rack::Request.new(e=TestRequest.env({}))
  61 + req.script_name.should.equal ""
  62 + req.script_name = "/foo"
  63 + req.script_name.should.equal "/foo"
  64 + e["SCRIPT_NAME"].should.equal "/foo"
  65 +
  66 + req.path_info.should.equal "/"
  67 + req.path_info = "/foo"
  68 + req.path_info.should.equal "/foo"
  69 + e["PATH_INFO"].should.equal "/foo"
  70 + end
  71 +
  72 + specify "provides the original env" do
  73 + req = Rack::Request.new(e=TestRequest.env({}))
  74 + req.env.should.be e
  75 + end
52 76 end

0 comments on commit 05ab0f9

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