|
4 | 4 | subject { Grape::Middleware::Formatter.new(app) }
|
5 | 5 | before { allow(subject).to receive(:dup).and_return(subject) }
|
6 | 6 |
|
7 |
| - let(:app) { ->(_env) { [200, {}, [@body || { 'foo' => 'bar' }]] } } |
| 7 | + let(:body) { { 'foo' => 'bar' } } |
| 8 | + let(:app) { ->(_env) { [200, {}, [body]] } } |
8 | 9 |
|
9 | 10 | context 'serialization' do
|
| 11 | + let(:body) { { 'abc' => 'def' } } |
10 | 12 | it 'looks at the bodies for possibly serializable data' do
|
11 |
| - @body = { 'abc' => 'def' } |
12 | 13 | _, _, bodies = *subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json')
|
13 |
| - bodies.each { |b| expect(b).to eq(MultiJson.dump(@body)) } |
| 14 | + bodies.each { |b| expect(b).to eq(MultiJson.dump(body)) } |
14 | 15 | end
|
15 | 16 |
|
16 |
| - it 'calls #to_json since default format is json' do |
17 |
| - @body = ['foo'] |
18 |
| - @body.instance_eval do |
19 |
| - def to_json |
20 |
| - '"bar"' |
| 17 | + context 'default format' do |
| 18 | + let(:body) { ['foo'] } |
| 19 | + it 'calls #to_json since default format is json' do |
| 20 | + body.instance_eval do |
| 21 | + def to_json |
| 22 | + '"bar"' |
| 23 | + end |
21 | 24 | end
|
22 |
| - end |
23 | 25 |
|
24 |
| - subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('"bar"') } |
| 26 | + subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('"bar"') } |
| 27 | + end |
25 | 28 | end
|
26 | 29 |
|
27 |
| - it 'calls #to_json if the content type is jsonapi' do |
28 |
| - @body = { 'foos' => [{ 'bar' => 'baz' }] } |
29 |
| - @body.instance_eval do |
30 |
| - def to_json |
31 |
| - '{"foos":[{"bar":"baz"}] }' |
| 30 | + context 'jsonapi' do |
| 31 | + let(:body) { { 'foos' => [{ 'bar' => 'baz' }] } } |
| 32 | + it 'calls #to_json if the content type is jsonapi' do |
| 33 | + body.instance_eval do |
| 34 | + def to_json |
| 35 | + '{"foos":[{"bar":"baz"}] }' |
| 36 | + end |
32 | 37 | end
|
33 |
| - end |
34 | 38 |
|
35 |
| - subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/vnd.api+json').to_a.last.each { |b| expect(b).to eq('{"foos":[{"bar":"baz"}] }') } |
| 39 | + subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/vnd.api+json').to_a.last.each { |b| expect(b).to eq('{"foos":[{"bar":"baz"}] }') } |
| 40 | + end |
36 | 41 | end
|
37 | 42 |
|
38 |
| - it 'calls #to_xml if the content type is xml' do |
39 |
| - @body = 'string' |
40 |
| - @body.instance_eval do |
41 |
| - def to_xml |
42 |
| - '<bar/>' |
| 43 | + context 'xml' do |
| 44 | + let(:body) { 'string' } |
| 45 | + it 'calls #to_xml if the content type is xml' do |
| 46 | + body.instance_eval do |
| 47 | + def to_xml |
| 48 | + '<bar/>' |
| 49 | + end |
43 | 50 | end
|
44 |
| - end |
45 | 51 |
|
46 |
| - subject.call('PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('<bar/>') } |
| 52 | + subject.call('PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('<bar/>') } |
| 53 | + end |
47 | 54 | end
|
48 | 55 | end
|
49 | 56 |
|
@@ -189,10 +196,12 @@ def to_xml
|
189 | 196 | _, _, body = subject.call('PATH_INFO' => '/info.custom')
|
190 | 197 | expect(body.body).to eq(['CUSTOM FORMAT'])
|
191 | 198 | end
|
192 |
| - it 'uses default json formatter' do |
193 |
| - @body = ['blah'] |
194 |
| - _, _, body = subject.call('PATH_INFO' => '/info.json') |
195 |
| - expect(body.body).to eq(['["blah"]']) |
| 199 | + context 'default' do |
| 200 | + let(:body) { ['blah'] } |
| 201 | + it 'uses default json formatter' do |
| 202 | + _, _, body = subject.call('PATH_INFO' => '/info.json') |
| 203 | + expect(body.body).to eq(['["blah"]']) |
| 204 | + end |
196 | 205 | end
|
197 | 206 | it 'uses custom json formatter' do
|
198 | 207 | subject.options[:formatters][:json] = ->(_obj, _env) { 'CUSTOM JSON FORMAT' }
|
@@ -284,10 +293,10 @@ def to_xml
|
284 | 293 | end
|
285 | 294 |
|
286 | 295 | context 'send file' do
|
287 |
| - let(:app) { ->(_env) { [200, {}, @body] } } |
| 296 | + let(:body) { Grape::ServeFile::FileResponse.new('file') } |
| 297 | + let(:app) { ->(_env) { [200, {}, body] } } |
288 | 298 |
|
289 | 299 | it 'returns Grape::Uril::SendFileReponse' do
|
290 |
| - @body = Grape::ServeFile::FileResponse.new('file') |
291 | 300 | env = { 'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json' }
|
292 | 301 | expect(subject.call(env)).to be_a(Grape::ServeFile::SendfileResponse)
|
293 | 302 | end
|
|
0 commit comments