File tree Expand file tree Collapse file tree 2 files changed +22
-11
lines changed
Expand file tree Collapse file tree 2 files changed +22
-11
lines changed Original file line number Diff line number Diff line change @@ -38,17 +38,21 @@ def retrieve_params(env)
3838body = env [ 'rack.input' ] . read
3939env [ 'rack.input' ] . rewind
4040
41- begin
42- post_params = case ( env [ 'CONTENT_TYPE' ] )
43- when URL_ENCODED then
44- ::Rack ::Utils . parse_nested_query ( body )
45- when JSON_ENCODED then
46- MultiJson . decode ( body )
47- else
48- { }
41+ unless body . empty?
42+ begin
43+ post_params = case ( env [ 'CONTENT_TYPE' ] )
44+ when URL_ENCODED then
45+ ::Rack ::Utils . parse_nested_query ( body )
46+ when JSON_ENCODED then
47+ MultiJson . decode ( body )
48+ else
49+ { }
50+ end
51+ rescue StandardError => e
52+ raise Goliath ::Validation ::BadRequestError , "Invalid parameters: #{ e . class . to_s } "
4953end
50- rescue StandardError => e
51- raise Goliath :: Validation :: BadRequestError . new ( "Invalid parameters: #{ e . class . to_s } " )
54+ else
55+ post_params = { }
5256end
5357end
5458
Original file line number Diff line number Diff line change 141141ret = @params . retrieve_params ( @env )
142142ret [ 'foo' ] . should == 'bar'
143143end
144+
145+ it "handles empty input gracefully on JSON" do
146+ @env [ 'CONTENT_TYPE' ] = 'application/json'
147+ @env [ 'rack.input' ] = StringIO . new
148+
149+ ret = @params . retrieve_params ( @env )
150+ ret . should be_empty
151+ end
144152
145153it "raises a BadRequestError on invalid JSON" do
146154@env [ 'CONTENT_TYPE' ] = 'application/json'
151159lambda { @params . retrieve_params ( @env ) } . should raise_error ( Goliath ::Validation ::BadRequestError )
152160end
153161
154-
155162it "doesn't parse unknown content types" do
156163@env [ 'CONTENT_TYPE' ] = 'fake/form -- type'
157164@env [ 'rack.input' ] = StringIO . new
You can’t perform that action at this time.
0 commit comments