Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/grape/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def call!(env)
ensure
after_response = after
end
after_response || @app_response
response_valid?(after_response) ? after_response : @app_response
end

# @abstract
Expand Down Expand Up @@ -65,6 +65,17 @@ def mime_types
end
types_without_params
end

def response_valid?(response)
if response.is_a?(Array)
status, headers, body = *response
(status >= 100 && status < 600) && (headers.is_a?(Hash)) && body.respond_to?(:each)
else
response.is_a?(Rack::Response)
end
rescue
false
end
end
end
end
14 changes: 12 additions & 2 deletions spec/grape/middleware/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,21 @@

context 'after callback' do
before do
allow(subject).to receive(:after).and_return([200, {}, 'Hello from after callback'])
allow(subject).to receive(:after).and_return([200, {}, ['Hello from after callback']])
end

it 'overwrites application response' do
expect(subject.call!({}).last).to eq('Hello from after callback')
expect(subject.call!({}).last).to eq(['Hello from after callback'])
end
end

context 'after callback with invalid response' do
before do
allow(subject).to receive(:after).and_return('invalid response')
end

it 'does not overwrite application response' do
expect(subject.call!({}).last).to eq('Hi there.')
end
end

Expand Down