|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe 'Grape::Rabl formatter' do |
| 4 | + subject do |
| 5 | + Class.new(Grape::API) |
| 6 | + end |
| 7 | + |
| 8 | + let(:xml_render) do |
| 9 | + %(<?xml version="1.0" encoding="UTF-8"?> |
| 10 | +<hash> |
| 11 | + <errors type="array"> |
| 12 | + <error>bad</error> |
| 13 | + <error>things</error> |
| 14 | + <error>happened</error> |
| 15 | + </errors> |
| 16 | +</hash> |
| 17 | +) |
| 18 | + end |
| 19 | + |
| 20 | + def app |
| 21 | + subject |
| 22 | + end |
| 23 | + |
| 24 | + context 'rendering' do |
| 25 | + context 'when no rabl template is specified' do |
| 26 | + before do |
| 27 | + # Grape::API defaults to the following declarations: |
| 28 | + # content_type :xml, 'application/xml' |
| 29 | + # content_type :json, 'application/json' |
| 30 | + # content_type :binary, 'application/octet-stream' |
| 31 | + # content_type :txt, 'text/plain' |
| 32 | + # default_format :txt |
| 33 | + subject.formatter :xml, Grape::Formatter::Rabl |
| 34 | + subject.formatter :txt, Grape::Formatter::Rabl |
| 35 | + subject.get('/oops') { { errors: %w[bad things happened] } } |
| 36 | + expect_any_instance_of(Grape::Rabl::Formatter).to receive(:render).and_call_original |
| 37 | + end |
| 38 | + |
| 39 | + it 'falls back to :txt given no other format information' do |
| 40 | + get '/oops' |
| 41 | + expect(last_response.body).to eq('{:errors=>["bad", "things", "happened"]}') |
| 42 | + expect(last_response.headers['Content-Type']).to eq('text/plain') |
| 43 | + end |
| 44 | + |
| 45 | + it 'falls back to the file extension if it is a valid format' do |
| 46 | + get '/oops.xml' |
| 47 | + expect(last_response.body).to eq(xml_render) |
| 48 | + expect(last_response.headers['Content-Type']).to eq('application/xml') |
| 49 | + end |
| 50 | + |
| 51 | + it 'falls back to the value of the `format` parameter in the query string if it is provided' do |
| 52 | + get '/oops?format=xml' |
| 53 | + expect(last_response.body).to eq(xml_render) |
| 54 | + expect(last_response.headers['Content-Type']).to eq('application/xml') |
| 55 | + end |
| 56 | + |
| 57 | + it 'falls back to the format set by the `format` option if it is a valid format' do |
| 58 | + # `format` option must be declared before endpoint |
| 59 | + subject.format :xml |
| 60 | + subject.get('/oops/2') { { errors: %w[bad things happened] } } |
| 61 | + |
| 62 | + get '/oops/2' |
| 63 | + expect(last_response.body).to eq(xml_render) |
| 64 | + expect(last_response.headers['Content-Type']).to eq('application/xml') |
| 65 | + end |
| 66 | + |
| 67 | + it 'falls back to the `Accept` header if it is a valid format' do |
| 68 | + get '/oops', {}, 'HTTP_ACCEPT' => 'application/xml' |
| 69 | + expect(last_response.body).to eq(xml_render) |
| 70 | + expect(last_response.headers['Content-Type']).to eq('application/xml') |
| 71 | + end |
| 72 | + |
| 73 | + it 'falls back to the default_format option if it is a valid format' do |
| 74 | + # `default_format` option must be declared before endpoint |
| 75 | + subject.default_format :xml |
| 76 | + subject.get('/oops/2') { { errors: %w[bad things happened] } } |
| 77 | + |
| 78 | + get '/oops/2' |
| 79 | + expect(last_response.body).to eq(xml_render) |
| 80 | + expect(last_response.headers['Content-Type']).to eq('application/xml') |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments