Skip to content

Commit 1517aac

Browse files
committed
Memoize the result of Grape::Middleware::Base#response.
Signed-off-by: Hermann Mayer <[email protected]>
1 parent 54f86a9 commit 1517aac

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

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

1112
### 1.6.0 (2021/10/04)

lib/grape/middleware/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def after; end
6060
def response
6161
return @app_response if @app_response.is_a?(Rack::Response)
6262

63-
Rack::Response.new(@app_response[2], @app_response[0], @app_response[1])
63+
@app_response = Rack::Response.new(@app_response[2], @app_response[0], @app_response[1])
6464
end
6565

6666
def content_type_for(format)

spec/grape/middleware/base_spec.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -77,42 +77,46 @@
7777
describe '#response' do
7878
subject { Grape::Middleware::Base.new(response) }
7979

80+
before { subject.call({}) }
81+
8082
context Array do
8183
let(:response) { ->(_) { [204, { abc: 1 }, 'test'] } }
8284

8385
it 'status' do
84-
subject.call({})
8586
expect(subject.response.status).to eq(204)
8687
end
8788

8889
it 'body' do
89-
subject.call({})
9090
expect(subject.response.body).to eq(['test'])
9191
end
9292

9393
it 'header' do
94-
subject.call({})
9594
expect(subject.response.header).to have_key(:abc)
9695
end
96+
97+
it 'returns the memoized Rack::Response instance' do
98+
expect(subject.response).to be(subject.response)
99+
end
97100
end
98101

99102
context Rack::Response do
100103
let(:response) { ->(_) { Rack::Response.new('test', 204, abc: 1) } }
101104

102105
it 'status' do
103-
subject.call({})
104106
expect(subject.response.status).to eq(204)
105107
end
106108

107109
it 'body' do
108-
subject.call({})
109110
expect(subject.response.body).to eq(['test'])
110111
end
111112

112113
it 'header' do
113-
subject.call({})
114114
expect(subject.response.header).to have_key(:abc)
115115
end
116+
117+
it 'returns the memoized Rack::Response instance' do
118+
expect(subject.response).to be(subject.response)
119+
end
116120
end
117121
end
118122

0 commit comments

Comments
 (0)