spec/grape/middleware/formatter_spec.rb in grape-0.13.0 vs spec/grape/middleware/formatter_spec.rb in grape-0.14.0
- old
+ new
@@ -130,10 +130,22 @@
it 'parses headers with vendor and api version' do
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test-v1+xml')
expect(subject.env['api.format']).to eq(:xml)
end
+ context 'with custom vendored content types' do
+ before do
+ subject.options[:content_types] = {}
+ subject.options[:content_types][:custom] = 'application/vnd.test+json'
+ end
+
+ it 'it uses the custom type' do
+ subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
+ expect(subject.env['api.format']).to eq(:custom)
+ end
+ end
+
it 'parses headers with symbols as hash keys' do
subject.call('PATH_INFO' => '/info', 'http_accept' => 'application/xml', system_time: '091293')
expect(subject.env[:system_time]).to eq('091293')
end
end
@@ -155,10 +167,20 @@
subject.options[:content_types] = {}
subject.options[:content_types][:custom] = 'application/x-custom'
_, headers, = subject.call('PATH_INFO' => '/info.custom')
expect(headers['Content-type']).to eq('application/x-custom')
end
+ it 'is set for vendored with registered type' do
+ subject.options[:content_types] = {}
+ subject.options[:content_types][:custom] = 'application/vnd.test+json'
+ _, headers, = subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
+ expect(headers['Content-type']).to eq('application/vnd.test+json')
+ end
+ it 'is set to closest generic for custom vendored/versioned without registered type' do
+ _, headers, = subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
+ expect(headers['Content-type']).to eq('application/json')
+ end
end
context 'format' do
it 'uses custom formatter' do
subject.options[:content_types] = {}
@@ -174,9 +196,21 @@
end
it 'uses custom json formatter' do
subject.options[:formatters][:json] = ->(_obj, _env) { 'CUSTOM JSON FORMAT' }
_, _, body = subject.call('PATH_INFO' => '/info.json')
expect(body.body).to eq(['CUSTOM JSON FORMAT'])
+ end
+ end
+
+ context 'no content responses' do
+ let(:no_content_response) { ->(status) { [status, {}, ['']] } }
+
+ Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.each do |status|
+ it "does not modify a #{status} response" do
+ expected_response = no_content_response[status]
+ allow(app).to receive(:call).and_return(expected_response)
+ expect(subject.call({})).to eq(expected_response)
+ end
end
end
context 'input' do
%w(POST PATCH PUT DELETE).each do |method|