spec/grape/middleware/formatter_spec.rb in grape-0.0.0.alpha.2 vs spec/grape/middleware/formatter_spec.rb in grape-0.1.0

- old
+ new

@@ -1,19 +1,42 @@ require 'spec_helper' describe Grape::Middleware::Formatter do - subject{ Grape::Middleware::Formatter.new(app)} + subject{ Grape::Middleware::Formatter.new(app, :default_format => :json)} before{ subject.stub!(:dup).and_return(subject) } let(:app){ lambda{|env| [200, {}, [@body]]} } context 'serialization' do it 'should look at the bodies for possibly serializable data' do @body = {"abc" => "def"} status, headers, bodies = *subject.call({'PATH_INFO' => '/somewhere'}) - bodies.first.should == ActiveSupport::JSON.encode(@body) + bodies.each{|b| b.should == MultiJson.encode(@body) } end + + it 'should call #to_json first if it is available' do + @body = "string" + @body.instance_eval do + def to_json + "\"bar\"" + end + end + + subject.call({'PATH_INFO' => '/somewhere'}).last.each{|b| b.should == '"bar"'} + end + + it 'should serialize the #serializable_hash if that is available' do + class SimpleExample + def serializable_hash + {:abc => 'def'} + end + end + + @body = SimpleExample.new + + subject.call({'PATH_INFO' => '/somewhere'}).last.each{|b| b.should == '{"abc":"def"}'} + end end context 'detection' do it 'should use the extension if one is provided' do subject.call({'PATH_INFO' => '/info.xml'}) @@ -28,8 +51,37 @@ end it 'should throw an error on an unrecognized format' do err = catch(:error){ subject.call({'PATH_INFO' => '/info.barklar'}) } err.should == {:status => 406, :message => "The requested format is not supported."} + end + end + + context 'Accept header detection' do + it 'should detect from the Accept header' do + subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/xml'}) + subject.env['api.format'].should == :xml + end + + it 'should look for case-indifferent headers' do + subject.call({'PATH_INFO' => '/info', 'accept' => 'application/xml'}) + subject.env['api.format'].should == :xml + end + + it 'should use quality rankings to determine formats' do + subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/json; q=0.3,application/xml; q=1.0'}) + subject.env['api.format'].should == :xml + subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/json; q=1.0,application/xml; q=0.3'}) + subject.env['api.format'].should == :json + end + + it 'should handle quality rankings mixed with nothing' do + subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/json,application/xml; q=1.0'}) + subject.env['api.format'].should == :xml + end + + it 'should properly parse headers with other attributes' do + subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/json; abc=2.3; q=1.0,application/xml; q=0.7'}) + subject.env['api.format'].should == :json end end end \ No newline at end of file