Skip to content

Commit 4f74fe3

Browse files
committed
bad query params to #range_limit action should not result in uncaught exception
Note that raising these specific excpetions will be automatically turned by rails into BadRequest => http 400, and NotFound => http 404 response.
1 parent 5bbb8db commit 4f74fe3

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

lib/blacklight_range_limit/range_limit_builder.rb

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ def add_range_limit_params(solr_params)
4848
# range_field, range_start, range_end
4949
def fetch_specific_range_limit(solr_params)
5050
field_key = blacklight_params[:range_field] # what field to fetch for
51+
52+
unless blacklight_params[:range_start].present? && blacklight_params[:range_end].present?
53+
raise BlacklightRangeLimit::InvalidRange
54+
end
55+
5156
start = blacklight_params[:range_start].to_i
5257
finish = blacklight_params[:range_end].to_i
5358

@@ -61,6 +66,9 @@ def fetch_specific_range_limit(solr_params)
6166
solr_params[:rows] = 0
6267

6368
return solr_params
69+
rescue BlacklightRangeLimit::InvalidRange
70+
# This will make Rails return a 400
71+
raise ActionController::BadRequest, "invalid range_start (#{blacklight_params[:range_start]}) or range_end (#{blacklight_params[:range_end]})"
6472
end
6573

6674
# hacky polyfill for new Blacklight behavior we need, if we don't have it yet
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe CatalogController, type: :controller do
4+
# Note that ActionController::BadRequest is caught by rails and turned into a 400
5+
# response, and ActionController::RoutingError is caught by raisl and turned into 404
6+
describe "bad params" do
7+
let (:facet_field) { "pub_date_si" }
8+
9+
it "without start param present raise BadRequest " do
10+
expect {
11+
get :range_limit, params: {
12+
"range_field"=> facet_field,
13+
"range_start"=>"1931"
14+
}
15+
}.to raise_error(ActionController::BadRequest)
16+
end
17+
18+
it "without end param raise BadRequest " do
19+
expect {
20+
get :range_limit, params: {
21+
"range_field"=> facet_field,
22+
"range_start"=>"1931"
23+
}
24+
}.to raise_error(ActionController::BadRequest)
25+
end
26+
27+
it "without either boundary raise BadRequest" do
28+
expect {
29+
get :range_limit, params: {
30+
"range_field"=> facet_field,
31+
}
32+
}.to raise_error(ActionController::BadRequest)
33+
end
34+
35+
it "without a range_field raise RoutingError" do
36+
expect {
37+
get :range_limit, params: {}
38+
}.to raise_error(ActionController::RoutingError)
39+
end
40+
41+
it "with params out of order raise BadRequest" do
42+
expect {
43+
get :range_limit, params: {
44+
"range_field"=> facet_field,
45+
"range_start"=>"1940",
46+
"range_end"=>"1930"
47+
}
48+
}.to raise_error(ActionController::BadRequest)
49+
end
50+
51+
it "with one of the params is an array raise BadRequest" do
52+
expect {
53+
get :range_limit, params: {
54+
"range_field"=> facet_field,
55+
"range_start"=>"1931",
56+
"range_end"=>["1940"]
57+
}
58+
}.to raise_error(ActionController::BadRequest)
59+
end
60+
end
61+
end

0 commit comments

Comments
 (0)