Skip to content

Commit

Permalink
Merge pull request #2192 from Jack12816/memoized-response-middleware-…
Browse files Browse the repository at this point in the history
…helper

Memoize the result of Grape::Middleware::Base#response.
  • Loading branch information
dblock authored Oct 4, 2021
2 parents 54f86a9 + 1517aac commit 4edf54b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

* [#2192](https://github.com/ruby-grape/grape/pull/2192): Memoize the result of Grape::Middleware::Base#response - [@Jack12816](https://github.com/Jack12816).
* Your contribution here.

### 1.6.0 (2021/10/04)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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])
@app_response = Rack::Response.new(@app_response[2], @app_response[0], @app_response[1])
end

def content_type_for(format)
Expand Down
16 changes: 10 additions & 6 deletions spec/grape/middleware/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,42 +77,46 @@
describe '#response' do
subject { Grape::Middleware::Base.new(response) }

before { subject.call({}) }

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'])
end

it 'header' do
subject.call({})
expect(subject.response.header).to have_key(:abc)
end

it 'returns the memoized Rack::Response instance' do
expect(subject.response).to be(subject.response)
end
end

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

it 'returns the memoized Rack::Response instance' do
expect(subject.response).to be(subject.response)
end
end
end

Expand Down

0 comments on commit 4edf54b

Please sign in to comment.