Permalink
Browse files

fix nil.dup in api versions/downloads conroller

caused by Date.parse receiving nil param value
  • Loading branch information...
1 parent 5fd937d commit dfaa52000fe66a10ee2c4b8c14edd7674622d807 @cldwalker cldwalker committed Sep 1, 2011
@@ -1,3 +1,17 @@
class Api::BaseController < ApplicationController
skip_before_filter :require_ssl
+
+ private
+
+ def has_required_params?(*keys)
+ if keys.all? {|key| params[key] }
+ true
+ else
+ missing_params = keys.select {|key| !params[key] }
+ str = missing_params.size > 1 ? 'params' : 'param'
+ render :text => "Request is missing #{str} #{missing_params.map(&:inspect).to_sentence}",
+ :status => :bad_request
+ false
+ end
+ end
end
@@ -4,19 +4,8 @@ class Api::V1::SearchesController < Api::BaseController
respond_to :json, :xml, :yaml
def show
- has_required_param(:query) do
- @rubygems = Rubygem.search(params[:query]).with_versions.paginate(:page => params[:page])
- respond_with(@rubygems, :yamlish => true)
- end
- end
-
- private
-
- def has_required_param(key)
- if params[key]
- yield
- else
- render :text => "Request is missing param #{key.inspect}", :status => :bad_request
- end
+ return unless has_required_params?(:query)
+ @rubygems = Rubygem.search(params[:query]).with_versions.paginate(:page => params[:page])
+ respond_with(@rubygems, :yamlish => true)
end
end
@@ -10,6 +10,8 @@ def index
end
def search
+ return unless has_required_params?(:from, :to)
+
start, stop = [params[:from], params[:to]].map do |d|
Date.parse(d)
end
@@ -95,6 +95,18 @@ def self.should_respond_to(format)
end
end
+ context "on GET to search with missing params" do
+ setup do
+ version = Factory(:version, :indexed => false)
+ get :search, :version_id => version.full_name, :format => 'json'
+ end
+
+ should respond_with :bad_request
+ should "explain failed request" do
+ assert page.has_content?("Request is missing params :from and :to")
+ end
+ end
+
def self.should_respond_to(format)
context "with #{format.to_s.upcase}" do
should "return download stats for the days specified for at most 90 days" do

0 comments on commit dfaa520

Please sign in to comment.