Permalink
Browse files

only update param where it was previously defined

  • Loading branch information...
1 parent 02c4ecd commit a5ba82b085a08c9bf1537fa92817fafc06fc25bb @seamusabshere seamusabshere committed May 2, 2012
Showing with 37 additions and 3 deletions.
  1. +17 −3 lib/rack/request.rb
  2. +20 −0 test/spec_request.rb
View
@@ -227,17 +227,31 @@ def params
# Destructively update a parameter, whether it's in GET and/or POST. Returns nil.
#
- # The parameter is added to both GET and POST to reduce confusion.
+ # The parameter is updated wherever it was previous defined, so GET, POST, or both. If it wasn't previously defined, it's inserted into GET.
+ #
+ # env['rack.input'] is not touched.
def update_param(k, v)
- self.GET[k] = v
- self.POST[k] = v
+ found = false
+ if self.GET.has_key?(k)
+ found = true
+ self.GET[k] = v
+ end
+ if self.POST.has_key?(k)
+ found = true
+ self.POST[k] = v
+ end
+ unless found
+ self.GET[k] = v
+ end
@params = nil
nil
end
# Destructively delete a parameter, whether it's in GET or POST. Returns the value of the deleted parameter.
#
# If the parameter is in both GET and POST, the POST value takes precedence since that's how #params works.
+ #
+ # env['rack.input'] is not touched.
def delete_param(k)
v = [ self.POST.delete(k), self.GET.delete(k) ].compact.first
@params = nil
View
@@ -451,6 +451,26 @@
req2.params.should.equal 'foo' => 'bar'
end
+ should "modify params hash by changing only GET" do
+ e = Rack::MockRequest.env_for("?foo=duhget")
+ req = Rack::Request.new(e)
+ req.GET.should.equal 'foo' => 'duhget'
+ req.POST.should.equal({})
+ req.update_param 'foo', 'bar'
+ req.GET.should.equal 'foo' => 'bar'
+ req.POST.should.equal({})
+ end
+
+ should "modify params hash by changing only POST" do
+ e = Rack::MockRequest.env_for("", "REQUEST_METHOD" => 'POST', :input => "foo=duhpost")
+ req = Rack::Request.new(e)
+ req.GET.should.equal({})
+ req.POST.should.equal 'foo' => 'duhpost'
+ req.update_param 'foo', 'bar'
+ req.GET.should.equal({})
+ req.POST.should.equal 'foo' => 'bar'
+ end
+
should "modify params hash, even if param is defined in both POST and GET" do
e = Rack::MockRequest.env_for("?foo=duhget", "REQUEST_METHOD" => 'POST', :input => "foo=duhpost")
req1 = Rack::Request.new(e)

0 comments on commit a5ba82b

Please sign in to comment.