Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add some features to Request and the corresponding tests for them

darcs-hash:20070515234231-cbc08-3272b1798cabaaf261e9c69ed191bcf5e84b315f.gz
  • Loading branch information...
commit de7411c5f9cd1b2b17520ba2422210c256e1310b 1 parent c17f371
@manveru manveru authored
Showing with 64 additions and 0 deletions.
  1. +22 −0 lib/rack/request.rb
  2. +42 −0 test/spec_rack_request.rb
View
22 lib/rack/request.rb
@@ -72,6 +72,28 @@ def params
self.GET.update(self.POST)
end
+ # shortcut for request.params[key]
+ def [](key)
+ params[key.to_s]
+ end
+
+ # shortcut for request.params[key] = value
+ def []=(key, value)
+ params[key.to_s] = value
+ end
+
+ # like Hash#values_at
+ def values_at(*keys)
+ keys.map{|key| params[key] }
+ end
+
+ # the referer of the client or '/'
+ def referer
+ @env['HTTP_REFERER'] || '/'
+ end
+ alias referrer referer
+
+
def cookies
return {} unless @env["HTTP_COOKIE"]
View
42 test/spec_rack_request.rb
@@ -52,6 +52,48 @@
req.params.should.equal "foo" => "bar", "quux" => "bla"
end
+ specify "can get value by key from params with #[]" do
+ req = Rack::Request.new \
+ Rack::MockRequest.env_for("?foo=quux")
+ req['foo'].should.equal 'quux'
+ req[:foo].should.equal 'quux'
+ end
+
+ specify "can set value to key on params with #[]=" do
+ req = Rack::Request.new \
+ Rack::MockRequest.env_for("?foo=duh")
+ req['foo'].should.equal 'duh'
+ req[:foo].should.equal 'duh'
+ req.params.should.equal 'foo' => 'duh'
+
+ req['foo'] = 'bar'
+ req.params.should.equal 'foo' => 'bar'
+ req['foo'].should.equal 'bar'
+ req[:foo].should.equal 'bar'
+
+ req[:foo] = 'jaz'
+ req.params.should.equal 'foo' => 'jaz'
+ req['foo'].should.equal 'jaz'
+ req[:foo].should.equal 'jaz'
+ end
+
+ specify "values_at answers values by keys in order given" do
+ req = Rack::Request.new \
+ Rack::MockRequest.env_for("?foo=baz&wun=der&bar=ful")
+ req.values_at('foo').should.equal ['baz']
+ req.values_at('foo', 'wun').should.equal ['baz', 'der']
+ req.values_at('bar', 'foo', 'wun').should.equal ['ful', 'baz', 'der']
+ end
+
+ specify "referrer should be extracted correct" do
+ req = Rack::Request.new \
+ Rack::MockRequest.env_for("/", "HTTP_REFERER" => "/some/path")
+ req.referer.should.equal "/some/path"
+
+ req = Rack::Request.new \
+ Rack::MockRequest.env_for("/")
+ req.referer.should.equal "/"
+ end
specify "can cache, but invalidates the cache" do
req = Rack::Request.new \
Please sign in to comment.
Something went wrong with that request. Please try again.