Commits

Michael Granger  committed ad9220d

Make HTTPRequest#params always return a Hash

  • Participants
  • Parent commits dbac1c7

Comments (0)

Files changed (2)

File lib/strelka/httprequest.rb

 
 	### Parse the request parameters and return them as a Hash. For GET requests, these are
 	### taken from the query arguments.  For requests that commonly
-	### contain an entity-body, try and parse that.
+	### contain an entity-body, this method will attempt to parse that.
 	###
 	###   # For a handler with a route of '/user', for the request:
 	###   # "GET /user/1/profile?checkbox=1&checkbox=2&text=foo HTTP/1.1"
 	###   # r.params
 	###   # => {"checkbox"=>["1", "2"], "text"=>"foo"}
+	###
+	### If the request body is not a Hash, an empty Hash with the body's value as the default
+	### value will be returned instead.
 	def params
 		unless @params
+			value = nil
+
 			case self.verb
 			when :GET, :HEAD
-				@params = self.parse_query_args
+				value = self.parse_query_args
 			when :POST, :PUT
-				@params = self.parse_form_data
+				value = self.parse_form_data
 			when :TRACE
 				self.log.debug "No parameters for a TRACE request."
 			else
-				@params = self.parse_form_data if self.content_type
+				value = self.parse_form_data if self.content_type
 			end
+
+			value = Hash.new( value ) unless value.is_a?( Hash )
+			@params = value
 		end
 
 		return @params

File spec/strelka/httprequest_spec.rb

 			end
 
 
-			it "Doesn't respond with a 400 (BAD_REQUEST)" do
-				expect( @req.params ).to be_nil
+			it "params are all nil" do
+				expect( @req.params[:foo] ).to be_nil
 			end
 		end
 
 
 			it "returns an empty Hash for an empty body" do
 				@req.body = ''
-				expect( @req.params ).to eq({})
+				expect( @req.params['foo'] ).to be_nil
 			end
 
 			it "has a params Hash with the key/value pair in it if the form data has " +
 					'Content-type' => 'application/json' )
 			end
 
-			it "returns nil for an empty body" do
+			it "returns a default params hash for an empty body" do
 				@req.body = ''
-				expect( @req.params ).to be_nil()
+				expect( @req.params[:foo] ).to be_nil
 			end
 
 			it "has the JSON data as the params if it has a body with a JSON object in it" do
 				expect( @req.params ).to eq( data )
 			end
 
+			it "has the array as the default param value if the body has a JSON array in it" do
+				data = %w[an array of stuff]
+				@req.body = Yajl.dump( data )
+				expect( @req.params[:foo] ).to eq( data )
+			end
+
 		end
 
 		context "a POST request with a 'text/x-yaml' body" do
 					'Content-type' => 'text/x-yaml' )
 			end
 
-			it "returns nil for an empty body" do
+			it "returns a params hash in which all values are nil for an empty body" do
 				@req.body = ''
-				expect( @req.params ).to be_false()
+				expect( @req.params[:profile] ).to be_false
 			end
 
 			it "has the YAML data as the params if it has a body with YAML in it" do
 				})
 			end
 
+			it "handles null entity bodies by returning an empty Hash" do
+				@req.body = ''
+				expect( @req.params ).to eq( {} )
+			end
+
 		end
 
 	end