spec/grape/middleware/formatter_spec.rb in grape-0.2.5 vs spec/grape/middleware/formatter_spec.rb in grape-0.2.6

- old
+ new

@@ -65,11 +65,11 @@ subject.call({'PATH_INFO' => '/info.txt', 'HTTP_ACCEPT' => 'application/json'}) subject.env['api.format'].should == :txt end end - context 'Accept header detection' do + context 'accept header detection' do it 'detects from the Accept header' do subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/xml'}) subject.env['api.format'].should == :xml end @@ -104,11 +104,11 @@ subject.call({'PATH_INFO' => '/info', 'http_accept' => 'application/xml', :system_time => '091293'}) subject.env[:system_time].should == '091293' end end - context 'Content-type' do + context 'content-type' do it 'is set for json' do _, headers, _ = subject.call({'PATH_INFO' => '/info.json'}) headers['Content-type'].should == 'application/json' end it 'is set for xml' do @@ -125,11 +125,11 @@ _, headers, _ = subject.call({'PATH_INFO' => '/info.custom'}) headers['Content-type'].should == 'application/x-custom' end end - context 'Format' do + context 'format' do it 'uses custom formatter' do subject.options[:content_types] = {} subject.options[:content_types][:custom] = "don't care" subject.options[:formatters][:custom] = lambda { |obj, env| 'CUSTOM FORMAT' } _, _, body = subject.call({'PATH_INFO' => '/info.custom'}) @@ -145,22 +145,24 @@ _, _, body = subject.call({'PATH_INFO' => '/info.json'}) body.body.should == ['CUSTOM JSON FORMAT'] end end - context 'Input' do - it 'parses the body from a POST/PUT and put the contents into rack.request.form_hash' do - io = StringIO.new('{"is_boolean":true,"string":"thing"}') - subject.call({ - 'PATH_INFO' => '/info', - 'REQUEST_METHOD' => 'POST', - 'CONTENT_TYPE' => 'application/json', - 'rack.input' => io, - 'CONTENT_LENGTH' => io.length - }) - subject.env['rack.request.form_hash']['is_boolean'].should be_true - subject.env['rack.request.form_hash']['string'].should == 'thing' + context 'input' do + [ "application/json", "application/json; charset=utf-8" ].each do |content_type| + it 'parses the body from a POST/PUT and put the contents into rack.request.form_hash for #{content_type}' do + io = StringIO.new('{"is_boolean":true,"string":"thing"}') + subject.call({ + 'PATH_INFO' => '/info', + 'REQUEST_METHOD' => 'POST', + 'CONTENT_TYPE' => content_type, + 'rack.input' => io, + 'CONTENT_LENGTH' => io.length + }) + subject.env['rack.request.form_hash']['is_boolean'].should be_true + subject.env['rack.request.form_hash']['string'].should == 'thing' + end end it 'parses the body from an xml POST/PUT and put the contents into rack.request.from_hash' do io = StringIO.new('<thing><name>Test</name></thing>') subject.call({ 'PATH_INFO' => '/info.xml', @@ -169,18 +171,20 @@ 'rack.input' => io, 'CONTENT_LENGTH' => io.length }) subject.env['rack.request.form_hash']['thing']['name'].should == 'Test' end - it 'is able to fail gracefully if the body is regular POST content' do - io = StringIO.new('name=Other+Test+Thing') - subject.call({ - 'PATH_INFO' => '/info', - 'REQUEST_METHOD' => 'POST', - 'CONTENT_TYPE' => 'application/json', - 'rack.input' => io, - 'CONTENT_LENGTH' => io.length - }) - subject.env['rack.request.form_hash'].should be_nil + [ Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES ].flatten.each do |content_type| + it "ignores #{content_type}" do + io = StringIO.new('name=Other+Test+Thing') + subject.call({ + 'PATH_INFO' => '/info', + 'REQUEST_METHOD' => 'POST', + 'CONTENT_TYPE' => content_type, + 'rack.input' => io, + 'CONTENT_LENGTH' => io.length + }) + subject.env['rack.request.form_hash'].should be_nil + end end end end