From 7ad647a873b666ad90ebd512c58d037db2f6148e Mon Sep 17 00:00:00 2001 From: Dmitry Polushkin Date: Thu, 25 Jun 2015 08:46:33 +0100 Subject: [PATCH] Do not convert Rack::Response to Rack::Response While using https://github.com/aserafin/grape_logging grape middleware after an upgrading grape from 0.11 to 0.12 I've been stuck with an issue: ``` NoMethodError (undefined method `downcase' for 2:Fixnum) ``` That's because `Middleware#response` tries to convert `Rack::Response` to `Rack::Response` and fails. --- CHANGELOG.md | 1 + lib/grape/middleware/base.rb | 1 + spec/grape/middleware/base_spec.rb | 44 ++++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 452a21f6ed..f2315a5dd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Next Release * [#1038](https://github.com/intridea/grape/pull/1038): Avoid dup-ing the String class when used in inherited params - [@rnubel](https://github.com/rnubel). * [#1042](https://github.com/intridea/grape/issues/1042): Fix coercion of complex arrays - [@dim](https://github.com/dim). +* [#1045](https://github.com/intridea/grape/pull/1045): Do not convert `Rack::Response` to `Rack::Response` in middleware - [@dmitry](https://github.com/dmitry). 0.12.0 (6/18/2015) ================== diff --git a/lib/grape/middleware/base.rb b/lib/grape/middleware/base.rb index 3bbaf6bd7c..9b85cb5fc9 100644 --- a/lib/grape/middleware/base.rb +++ b/lib/grape/middleware/base.rb @@ -37,6 +37,7 @@ def after end def response + return @app_response if @app_response.is_a?(Rack::Response) Rack::Response.new(@app_response[2], @app_response[0], @app_response[1]) end diff --git a/spec/grape/middleware/base_spec.rb b/spec/grape/middleware/base_spec.rb index b45634ab5d..239eb88b19 100644 --- a/spec/grape/middleware/base_spec.rb +++ b/spec/grape/middleware/base_spec.rb @@ -36,21 +36,43 @@ describe '#response' do subject { Grape::Middleware::Base.new(response) } - let(:response) { ->(_) { [204, { abc: 1 }, 'test'] } } - it 'status' do - subject.call({}) - expect(subject.response.status).to eq(204) - end + context Array do + let(:response) { ->(_) { [204, { abc: 1 }, 'test'] } } + + it 'status' do + subject.call({}) + expect(subject.response.status).to eq(204) + end - it 'body' do - subject.call({}) - expect(subject.response.body).to eq(['test']) + it 'body' do + subject.call({}) + expect(subject.response.body).to eq(['test']) + end + + it 'header' do + subject.call({}) + expect(subject.response.header).to have_key(:abc) + end end - it 'header' do - subject.call({}) - expect(subject.response.header).to have_key(:abc) + context Rack::Response do + let(:response) { ->(_) { Rack::Response.new('test', 204, abc: 1) } } + + it 'status' do + subject.call({}) + expect(subject.response.status).to eq(204) + end + + it 'body' do + subject.call({}) + expect(subject.response.body).to eq(['test']) + end + + it 'header' do + subject.call({}) + expect(subject.response.header).to have_key(:abc) + end end end