|
3 | 3 | describe Grape::Middleware::Formatter do |
4 | 4 | subject{ Grape::Middleware::Formatter.new(app, :default_format => :json)} |
5 | 5 | before{ subject.stub!(:dup).and_return(subject) } |
6 | | - |
| 6 | + |
7 | 7 | let(:app){ lambda{|env| [200, {}, [@body]]} } |
8 | | - |
| 8 | + |
9 | 9 | context 'serialization' do |
10 | 10 | it 'should look at the bodies for possibly serializable data' do |
11 | 11 | @body = {"abc" => "def"} |
12 | 12 | status, headers, bodies = *subject.call({'PATH_INFO' => '/somewhere'}) |
13 | 13 | bodies.each{|b| b.should == MultiJson.encode(@body) } |
14 | 14 | end |
15 | | - |
| 15 | + |
16 | 16 | it 'should call #to_json first if it is available' do |
17 | 17 | @body = ['foo'] |
18 | 18 | @body.instance_eval do |
19 | 19 | def to_json |
20 | 20 | "\"bar\"" |
21 | 21 | end |
22 | 22 | end |
23 | | - |
| 23 | + |
24 | 24 | subject.call({'PATH_INFO' => '/somewhere'}).last.each{|b| b.should == '"bar"'} |
25 | 25 | end |
26 | | - |
| 26 | + |
27 | 27 | it 'should serialize the #serializable_hash if that is available' do |
28 | 28 | class SimpleExample |
29 | 29 | def serializable_hash |
30 | 30 | {:abc => 'def'} |
31 | 31 | end |
32 | 32 | end |
33 | | - |
| 33 | + |
| 34 | + @body = [SimpleExample.new, SimpleExample.new] |
| 35 | + |
| 36 | + subject.call({'PATH_INFO' => '/somewhere'}).last.each{|b| b.should == '[{"abc":"def"},{"abc":"def"}]'} |
| 37 | + end |
| 38 | + |
| 39 | + it 'should serialize multiple objects that respond to #serializable_hash' do |
| 40 | + class SimpleExample |
| 41 | + def serializable_hash |
| 42 | + {:abc => 'def'} |
| 43 | + end |
| 44 | + end |
| 45 | + |
34 | 46 | @body = SimpleExample.new |
35 | | - |
| 47 | + |
36 | 48 | subject.call({'PATH_INFO' => '/somewhere'}).last.each{|b| b.should == '{"abc":"def"}'} |
37 | 49 | end |
38 | | - |
| 50 | + |
39 | 51 | it 'should call #to_xml if the content type is xml' do |
40 | 52 | @body = "string" |
41 | 53 | @body.instance_eval do |
42 | 54 | def to_xml |
43 | 55 | "<bar/>" |
44 | 56 | end |
45 | 57 | end |
46 | | - |
| 58 | + |
47 | 59 | subject.call({'PATH_INFO' => '/somewhere.xml'}).last.each{|b| b.should == '<bar/>'} |
48 | 60 | end |
49 | 61 | end |
50 | | - |
| 62 | + |
51 | 63 | context 'detection' do |
52 | 64 | it 'should use the extension if one is provided' do |
53 | 65 | subject.call({'PATH_INFO' => '/info.xml'}) |
54 | 66 | subject.env['api.format'].should == :xml |
55 | 67 | subject.call({'PATH_INFO' => '/info.json'}) |
56 | 68 | subject.env['api.format'].should == :json |
57 | 69 | end |
58 | | - |
| 70 | + |
59 | 71 | it 'should use the default format if none is provided' do |
60 | 72 | subject.call({'PATH_INFO' => '/info'}) |
61 | 73 | subject.env['api.format'].should == :json |
62 | 74 | end |
63 | | - |
| 75 | + |
64 | 76 | it 'should throw an error on an unrecognized format' do |
65 | 77 | err = catch(:error){ subject.call({'PATH_INFO' => '/info.barklar'}) } |
66 | 78 | err.should == {:status => 406, :message => "The requested format is not supported."} |
67 | 79 | end |
68 | 80 | end |
69 | | - |
| 81 | + |
70 | 82 | context 'Accept header detection' do |
71 | 83 | it 'should detect from the Accept header' do |
72 | 84 | subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/xml'}) |
73 | 85 | subject.env['api.format'].should == :xml |
74 | 86 | end |
75 | | - |
| 87 | + |
76 | 88 | it 'should look for case-indifferent headers' do |
77 | 89 | subject.call({'PATH_INFO' => '/info', 'accept' => 'application/xml'}) |
78 | 90 | subject.env['api.format'].should == :xml |
79 | 91 | end |
80 | | - |
| 92 | + |
81 | 93 | it 'should use quality rankings to determine formats' do |
82 | 94 | subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/json; q=0.3,application/xml; q=1.0'}) |
83 | 95 | subject.env['api.format'].should == :xml |
84 | 96 | subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/json; q=1.0,application/xml; q=0.3'}) |
85 | 97 | subject.env['api.format'].should == :json |
86 | 98 | end |
87 | | - |
| 99 | + |
88 | 100 | it 'should handle quality rankings mixed with nothing' do |
89 | 101 | subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/json,application/xml; q=1.0'}) |
90 | 102 | subject.env['api.format'].should == :xml |
91 | 103 | end |
92 | | - |
| 104 | + |
93 | 105 | it 'should properly parse headers with other attributes' do |
94 | 106 | subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/json; abc=2.3; q=1.0,application/xml; q=0.7'}) |
95 | 107 | subject.env['api.format'].should == :json |
|
0 commit comments