spec/grape/middleware/formatter_spec.rb in grape-1.2.5 vs spec/grape/middleware/formatter_spec.rb in grape-1.3.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'spec_helper'
describe Grape::Middleware::Formatter do
subject { Grape::Middleware::Formatter.new(app) }
before { allow(subject).to receive(:dup).and_return(subject) }
@@ -39,11 +41,11 @@
subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/vnd.api+json').to_a.last.each { |b| expect(b).to eq('{"foos":[{"bar":"baz"}] }') }
end
end
context 'xml' do
- let(:body) { 'string' }
+ let(:body) { +'string' }
it 'calls #to_xml if the content type is xml' do
body.instance_eval do
def to_xml
'<bar/>'
end
@@ -192,23 +194,23 @@
it 'uses custom formatter' do
subject.options[:content_types] = {}
subject.options[:content_types][:custom] = "don't care"
subject.options[:formatters][:custom] = ->(_obj, _env) { 'CUSTOM FORMAT' }
_, _, body = subject.call('PATH_INFO' => '/info.custom')
- expect(body.body).to eq(['CUSTOM FORMAT'])
+ expect(read_chunks(body)).to eq(['CUSTOM FORMAT'])
end
context 'default' do
let(:body) { ['blah'] }
it 'uses default json formatter' do
_, _, body = subject.call('PATH_INFO' => '/info.json')
- expect(body.body).to eq(['["blah"]'])
+ expect(read_chunks(body)).to eq(['["blah"]'])
end
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'])
+ expect(read_chunks(body)).to eq(['CUSTOM JSON FORMAT'])
end
end
context 'no content responses' do
let(:no_content_response) { ->(status) { [status, {}, ['']] } }
@@ -375,16 +377,21 @@
end
end
end
context 'send file' do
- let(:body) { Grape::ServeFile::FileResponse.new('file') }
- let(:app) { ->(_env) { [200, {}, body] } }
+ let(:file) { double(File) }
+ let(:file_body) { Grape::ServeFile::FileResponse.new(file) }
+ let(:app) { ->(_env) { [200, {}, file_body] } }
- it 'returns Grape::Uril::SendFileReponse' do
+ it 'returns a file response' do
+ expect(file).to receive(:each).and_yield('data')
env = { 'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json' }
- expect(subject.call(env)).to be_a(Grape::ServeFile::SendfileResponse)
+ status, headers, body = subject.call(env)
+ expect(status).to be == 200
+ expect(headers).to be == { 'Content-Type' => 'application/json' }
+ expect(read_chunks(body)).to be == ['data']
end
end
context 'inheritable formatters' do
class InvalidFormatter
@@ -402,11 +409,11 @@
Grape::Formatter.default_elements.delete(:invalid)
end
it 'returns response by invalid formatter' do
env = { 'PATH_INFO' => '/hello.invalid', 'HTTP_ACCEPT' => 'application/x-invalid' }
- _, _, bodies = *subject.call(env)
- expect(bodies.body.first).to eq({ message: 'invalid' }.to_json)
+ _, _, body = *subject.call(env)
+ expect(read_chunks(body).join).to eq({ message: 'invalid' }.to_json)
end
end
context 'custom parser raises exception and rescue options are enabled for backtrace and original_exception' do
it 'adds the backtrace and original_exception to the error output' do