/
formatter_spec.rb
192 lines (162 loc) · 7.31 KB
/
formatter_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
require 'spec_helper'
describe Grape::Middleware::Formatter do
subject{ Grape::Middleware::Formatter.new(app) }
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', 'HTTP_ACCEPT' => 'application/json'})
bodies.each{|b| b.should == MultiJson.dump(@body) }
end
it 'should call #to_json first if it is available' do
@body = ['foo']
@body.instance_eval do
def to_json
"\"bar\""
end
end
subject.call({'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json'}).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, SimpleExample.new]
subject.call({'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json'}).last.each{|b| b.should == '[{"abc":"def"},{"abc":"def"}]'}
end
it 'should serialize multiple objects that respond to #serializable_hash' do
class SimpleExample
def serializable_hash
{:abc => 'def'}
end
end
@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/>"
end
end
subject.call({'PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json'}).last.each{|b| b.should == '<bar/>'}
end
end
context 'detection' do
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 default format if none is provided' do
subject.call({'PATH_INFO' => '/info'})
subject.env['api.format'].should == :txt
end
it 'should use the requested format if provided in headers' do
subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json'})
subject.env['api.format'].should == :json
end
it 'should use the file extension format if provided before headers' do
subject.call({'PATH_INFO' => '/info.txt', 'HTTP_ACCEPT' => 'application/json'})
subject.env['api.format'].should == :txt
end
end
context 'Accept header detection' do
it 'should detect from the Accept header' do
subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/xml'})
subject.env['api.format'].should == :xml
end
it 'should look for case-indifferent headers' do
subject.call({'PATH_INFO' => '/info', 'http_accept' => 'application/xml'})
subject.env['api.format'].should == :xml
end
it 'should use quality rankings to determine formats' do
subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json; q=0.3,application/xml; q=1.0'})
subject.env['api.format'].should == :xml
subject.call({'PATH_INFO' => '/info', 'HTTP_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', 'HTTP_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', 'HTTP_ACCEPT' => 'application/json; abc=2.3; q=1.0,application/xml; q=0.7'})
subject.env['api.format'].should == :json
end
it 'should properly parse headers with vendor and api version' do
subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test-v1+xml'})
subject.env['api.format'].should == :xml
end
it 'should properly parse headers with symbols as hash keys' do
subject.call({'PATH_INFO' => '/info', 'http_accept' => 'application/xml', :system_time => '091293'})
subject.env[:system_time].should == '091293'
end
end
context 'Content-type' do
it 'should be set for json' do
_, headers, _ = subject.call({'PATH_INFO' => '/info.json'})
headers['Content-type'].should == 'application/json'
end
it 'should be set for xml' do
_, headers, _ = subject.call({'PATH_INFO' => '/info.xml'})
headers['Content-type'].should == 'application/xml'
end
it 'should be set for txt' do
_, headers, _ = subject.call({'PATH_INFO' => '/info.txt'})
headers['Content-type'].should == 'text/plain'
end
it 'should be set for custom' do
subject.options[:content_types][:custom] = 'application/x-custom'
_, headers, _ = subject.call({'PATH_INFO' => '/info.custom'})
headers['Content-type'].should == 'application/x-custom'
end
end
context 'Format' do
it 'should use custom formatter' do
subject.options[:content_types][:custom] = "don't care"
subject.options[:formatters][:custom] = lambda { |obj| 'CUSTOM FORMAT' }
_, _, body = subject.call({'PATH_INFO' => '/info.custom'})
body.body.should == ['CUSTOM FORMAT']
end
it 'should use default json formatter' do
@body = ['blah']
_, _, body = subject.call({'PATH_INFO' => '/info.json'})
body.body.should == ['["blah"]']
end
it 'should use custom json formatter' do
subject.options[:formatters][:json] = lambda { |obj| 'CUSTOM JSON FORMAT' }
_, _, body = subject.call({'PATH_INFO' => '/info.json'})
body.body.should == ['CUSTOM JSON FORMAT']
end
end
context 'Input' do
it 'should parse the body from a POST/PUT and put the contents into rack.request.form_hash' do
subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json', 'rack.input' => StringIO.new('{"is_boolean":true,"string":"thing"}')})
subject.env['rack.request.form_hash']['is_boolean'].should be_true
subject.env['rack.request.form_hash']['string'].should == 'thing'
end
it 'should parse the body from an xml POST/PUT and put the contents into rack.request.from_hash' do
subject.call({'PATH_INFO' => '/info.xml', 'HTTP_ACCEPT' => 'application/xml', 'rack.input' => StringIO.new('<thing><name>Test</name></thing>')})
subject.env['rack.request.form_hash']['thing']['name'].should == 'Test'
end
it 'should be able to fail gracefully if the body is regular POST content' do
subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json', 'rack.input' => StringIO.new('name=Other+Test+Thing')})
subject.env['rack.request.form_hash'].should be_nil
end
end
end