Skip to content

Commit 5dd9913

Browse files
committed
Correctly read chunks of the response body according to the Rack SPEC.
1 parent 9cbb48c commit 5dd9913

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

spec/grape/api_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,9 +1903,9 @@ def foo
19031903
it 'avoids polluting global namespace' do
19041904
env = Rack::MockRequest.env_for('/')
19051905

1906-
expect(a.call(env)[2]).to eq(['foo'])
1907-
expect(b.call(env)[2]).to eq(['bar'])
1908-
expect(a.call(env)[2]).to eq(['foo'])
1906+
expect(read_chunks(a.call(env)[2])).to eq(['foo'])
1907+
expect(read_chunks(b.call(env)[2])).to eq(['bar'])
1908+
expect(read_chunks(a.call(env)[2])).to eq(['foo'])
19091909
end
19101910
end
19111911

spec/grape/endpoint_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def app
151151
it 'includes headers passed as symbols' do
152152
env = Rack::MockRequest.env_for('/headers')
153153
env['HTTP_SYMBOL_HEADER'.to_sym] = 'Goliath passes symbols'
154-
body = subject.call(env)[2].first
154+
body = read_chunks(subject.call(env)[2]).join
155155
expect(JSON.parse(body)['Symbol-Header']).to eq('Goliath passes symbols')
156156
end
157157
end

spec/grape/integration/rack_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
}
2222
env = Rack::MockRequest.env_for('/', options)
2323

24-
expect(JSON.parse(app.call(env)[2].first)['params_keys']).to match_array('test')
24+
expect(JSON.parse(read_chunks(app.call(env)[2]).join)['params_keys']).to match_array('test')
2525
ensure
2626
input.close
2727
input.unlink

spec/grape/middleware/formatter_spec.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,19 @@ def to_xml
196196
subject.options[:content_types][:custom] = "don't care"
197197
subject.options[:formatters][:custom] = ->(_obj, _env) { 'CUSTOM FORMAT' }
198198
_, _, body = subject.call('PATH_INFO' => '/info.custom')
199-
expect(body).to eq(['CUSTOM FORMAT'])
199+
expect(read_chunks(body)).to eq(['CUSTOM FORMAT'])
200200
end
201201
context 'default' do
202202
let(:body) { ['blah'] }
203203
it 'uses default json formatter' do
204204
_, _, body = subject.call('PATH_INFO' => '/info.json')
205-
expect(body).to eq(['["blah"]'])
205+
expect(read_chunks(body)).to eq(['["blah"]'])
206206
end
207207
end
208208
it 'uses custom json formatter' do
209209
subject.options[:formatters][:json] = ->(_obj, _env) { 'CUSTOM JSON FORMAT' }
210210
_, _, body = subject.call('PATH_INFO' => '/info.json')
211-
expect(body).to eq(['CUSTOM JSON FORMAT'])
211+
expect(read_chunks(body)).to eq(['CUSTOM JSON FORMAT'])
212212
end
213213
end
214214

@@ -384,11 +384,12 @@ def to_xml
384384
let(:app) { ->(_env) { [200, {}, file_body] } }
385385

386386
it 'returns a file response' do
387+
expect(file).to receive(:each).and_yield('data')
387388
env = { 'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json' }
388389
status, headers, body = subject.call(env)
389390
expect(status).to be == 200
390391
expect(headers).to be == { 'Content-Type' => 'application/json' }
391-
expect(body).to be file
392+
expect(read_chunks(body)).to be == ['data']
392393
end
393394
end
394395

@@ -410,8 +411,8 @@ def self.call(_, _)
410411

411412
it 'returns response by invalid formatter' do
412413
env = { 'PATH_INFO' => '/hello.invalid', 'HTTP_ACCEPT' => 'application/x-invalid' }
413-
_, _, bodies = *subject.call(env)
414-
expect(bodies.first).to eq({ message: 'invalid' }.to_json)
414+
_, _, body = *subject.call(env)
415+
expect(read_chunks(body).join).to eq({ message: 'invalid' }.to_json)
415416
end
416417
end
417418

spec/spec_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@
1818
# so it should be set to true here as well to reflect that.
1919
I18n.enforce_available_locales = true
2020

21+
module Chunks
22+
def read_chunks(body)
23+
buffer = []
24+
body.each { |chunk| buffer << chunk }
25+
26+
buffer
27+
end
28+
end
29+
2130
RSpec.configure do |config|
31+
config.include Chunks
2232
config.include Rack::Test::Methods
2333
config.include Spec::Support::Helpers
2434
config.raise_errors_for_deprecations!

0 commit comments

Comments
 (0)