Permalink
Browse files

Added validation to the Request object.

  • Loading branch information...
1 parent d3db73c commit a2de6bd56ac57e85f6976510961c5fd20b6b3e7e Phillip Toland committed Jan 19, 2009
Showing with 66 additions and 5 deletions.
  1. +14 −5 lib/patron/request.rb
  2. +52 −0 spec/request_spec.rb
View
@@ -25,17 +25,26 @@ def action=(new_action)
end
def timeout=(new_timeout)
- # TODO add validation
- @timeout = new_timeout
+ if new_timeout.to_i < 1
+ raise ArgumentError, "Timeout must be a positive integer greater than 0"
+ end
+
+ @timeout = new_timeout.to_i
end
def max_redirects=(new_max_redirects)
- # TODO add validation
- @max_redirects = new_max_redirects
+ if new_max_redirects.to_i < -1
+ raise ArgumentError, "Max redirects must be a positive integer, 0 or -1"
+ end
+
+ @max_redirects = new_max_redirects.to_i
end
def headers=(new_headers)
- # TODO add validation
+ if !new_headers.kind_of?(Hash)
+ raise ArgumentError, "Headers must be a hash"
+ end
+
@headers = new_headers
end
View
@@ -0,0 +1,52 @@
+require File.dirname(__FILE__) + '/spec_helper.rb'
+
+
+describe Patron::Request do
+
+ before(:each) do
+ @request = Patron::Request.new
+ end
+
+ describe :action do
+
+ it "should accept :get, :put, :post, :delete and :head" do
+ [:get, :put, :post, :delete, :head].each do |action|
+ lambda {@request.action = action}.should_not raise_error
+ end
+ end
+
+ it "should raise an exception when assigned a bad value" do
+ lambda {@request.action = :foo}.should raise_error(ArgumentError)
+ end
+
+ end
+
+ describe :timeout do
+
+ it "should raise an exception when assigned a negative number" do
+ lambda {@request.timeout = -1}.should raise_error(ArgumentError)
+ end
+
+ it "should raise an exception when assigned 0" do
+ lambda {@request.timeout = -1}.should raise_error(ArgumentError)
+ end
+
+ end
+
+ describe :max_redirects do
+
+ it "should raise an error when assigned an integer smaller than -1" do
+ lambda {@request.max_redirects = -2}.should raise_error(ArgumentError)
+ end
+
+ end
+
+ describe :headers do
+
+ it "should raise an error when assigned something other than a hash" do
+ lambda {@request.headers = :foo}.should raise_error(ArgumentError)
+ end
+
+ end
+
+end

0 comments on commit a2de6bd

Please sign in to comment.