Skip to content

Commit 514a2e5

Browse files
committed
Merge pull request #952 from dabrorius/filter_invalid_response_codes
Raise error if unknow status code is passed to #status
2 parents 95331d6 + 2225485 commit 514a2e5

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).
77
* [#942](https://github.com/intridea/grape/pull/942): Fixed forced presence for optional params when based on a reused entity that was also required in another context - [@croeck](https://github.com/croeck).
88
* [#950](https://github.com/intridea/grape/pull/950): Status method can now accept one of Rack::Utils status code symbols (:ok, :found, :bad_request, etc.). - [@dabrorius](https://github.com/dabrorius).
9+
* [#952](https://github.com/intridea/grape/pull/952): Status method now raises error when called with invalid status code. - [@dabrorius](https://github.com/dabrorius).
910

1011
* Your contribution here.
1112

lib/grape/dsl/inside_route.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,29 @@ def redirect(url, options = {})
9494
#
9595
# @param status [Integer] The HTTP Status Code to return for this request.
9696
def status(status = nil)
97-
if status.is_a? Symbol
97+
case status
98+
when Symbol
9899
if Rack::Utils::SYMBOL_TO_STATUS_CODE.keys.include?(status)
99100
@status = Rack::Utils.status_code(status)
100101
else
101102
fail ArgumentError, "Status code :#{status} is invalid."
102103
end
103-
elsif status
104-
@status = status
105-
else
104+
when Fixnum
105+
if Rack::Utils::HTTP_STATUS_CODES.keys.include?(status)
106+
@status = status
107+
else
108+
fail ArgumentError, "Status code #{status} is invalid."
109+
end
110+
when nil
106111
return @status if @status
107112
case request.request_method.to_s.upcase
108113
when 'POST'
109114
201
110115
else
111116
200
112117
end
118+
else
119+
fail ArgumentError, 'Status code must be Fixnum or Symbol.'
113120
end
114121
end
115122

spec/grape/dsl/inside_route_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ def initialize
117117
expect { subject.status :foo_bar }
118118
.to raise_error(ArgumentError, 'Status code :foo_bar is invalid.')
119119
end
120+
121+
it 'raises error if unknow status code is passed' do
122+
expect { subject.status 210 }
123+
.to raise_error(ArgumentError, 'Status code 210 is invalid.')
124+
end
125+
126+
it 'raises error if status is not a fixnum or symbol' do
127+
expect { subject.status Object.new }
128+
.to raise_error(ArgumentError, 'Status code must be Fixnum or Symbol.')
129+
end
120130
end
121131

122132
describe '#header' do

0 commit comments

Comments
 (0)