spec/grape/middleware/formatter_spec.rb in grape-0.2.1.1 vs spec/grape/middleware/formatter_spec.rb in grape-0.2.2
- old
+ new
@@ -46,10 +46,22 @@
@body = SimpleExample.new
subject.call({'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json'}).last.each{|b| b.should == '{"abc":"def"}'}
end
+ it 'should serialize objects that respond to #serializable_hash if there is a root element' do
+ class SimpleExample
+ def serializable_hash
+ {:abc => 'def'}
+ end
+ end
+
+ @body = {"root" => SimpleExample.new}
+
+ subject.call({'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json'}).last.each{|b| b.should == '{"root":{"abc":"def"}}'}
+ end
+
it 'should call #to_xml if the content type is xml' do
@body = "string"
@body.instance_eval do
def to_xml
"<bar/>"
@@ -64,9 +76,16 @@
it 'should use the extension if one is provided' do
subject.call({'PATH_INFO' => '/info.xml'})
subject.env['api.format'].should == :xml
subject.call({'PATH_INFO' => '/info.json'})
subject.env['api.format'].should == :json
+ end
+
+ it 'should use the format parameter if one is provided' do
+ subject.call({'PATH_INFO' => '/somewhere','QUERY_STRING' => 'format=json'})
+ subject.env['api.format'].should == :json
+ subject.call({'PATH_INFO' => '/somewhere','QUERY_STRING' => 'format=xml'})
+ subject.env['api.format'].should == :xml
end
it 'should use the default format if none is provided' do
subject.call({'PATH_INFO' => '/info'})
subject.env['api.format'].should == :txt