Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 7f271a3

Browse files
Align implementation with Rack SPEC for grape 1.2
Ref: ruby-grape#1956 Signed-off-by: Luong Vo <[email protected]>
1 parent 9f786ad commit 7f271a3

File tree

9 files changed

+47
-15
lines changed

9 files changed

+47
-15
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ matrix:
1313
- bundle exec danger
1414
- rvm: 2.7.0
1515
gemfile: Gemfile
16+
- rvm: 2.7.0
17+
gemfile: gemfiles/rack1.gemfile
18+
- rvm: 2.7.0
19+
gemfile: gemfiles/rack2-0.gemfile
1620
- rvm: 2.7.0
1721
gemfile: gemfiles/rack_edge.gemfile
1822
- rvm: 2.7.0

gemfiles/rack1.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
eval_gemfile('../Gemfile')
4+
5+
gem 'rack', '~> 1.0'

gemfiles/rack2-0.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
eval_gemfile('../Gemfile')
4+
5+
gem 'rack', '~> 1.0'

lib/grape/middleware/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def default_options
2525
end
2626

2727
def call(env)
28-
dup.call!(env)
28+
dup.call!(env).to_a
2929
end
3030

3131
def call!(env)

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].body).to eq(['foo'])
1907-
expect(b.call(env)[2].body).to eq(['bar'])
1908-
expect(a.call(env)[2].body).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].body.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].body.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: 14 additions & 9 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.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.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.body).to eq(['CUSTOM JSON FORMAT'])
211+
expect(read_chunks(body)).to eq(['CUSTOM JSON FORMAT'])
212212
end
213213
end
214214

@@ -379,12 +379,17 @@ def to_xml
379379
end
380380

381381
context 'send file' do
382-
let(:body) { Grape::ServeFile::FileResponse.new('file') }
383-
let(:app) { ->(_env) { [200, {}, body] } }
382+
let(:file) { double(File) }
383+
let(:file_body) { Grape::ServeFile::FileResponse.new(file) }
384+
let(:app) { ->(_env) { [200, {}, file_body] } }
384385

385-
it 'returns Grape::Uril::SendFileReponse' do
386+
it 'returns a file response' do
387+
expect(file).to receive(:each).and_yield('data')
386388
env = { 'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json' }
387-
expect(subject.call(env)).to be_a(Grape::ServeFile::SendfileResponse)
389+
status, headers, body = subject.call(env)
390+
expect(status).to be == 200
391+
expect(headers).to be == { 'Content-Type' => 'application/json' }
392+
expect(read_chunks(body)).to be == ['data']
388393
end
389394
end
390395

@@ -406,8 +411,8 @@ def self.call(_, _)
406411

407412
it 'returns response by invalid formatter' do
408413
env = { 'PATH_INFO' => '/hello.invalid', 'HTTP_ACCEPT' => 'application/x-invalid' }
409-
_, _, bodies = *subject.call(env)
410-
expect(bodies.body.first).to eq({ message: 'invalid' }.to_json)
414+
_, _, body = *subject.call(env)
415+
expect(read_chunks(body).join).to eq({ message: 'invalid' }.to_json)
411416
end
412417
end
413418

spec/spec_helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,26 @@
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!
2535
config.filter_run_when_matching :focus
2636

2737
config.before(:each) { Grape::Util::InheritableSetting.reset_global! }
38+
39+
# Enable flags like --only-failures and --next-failure
40+
config.example_status_persistence_file_path = '.rspec_status'
2841
end
2942

3043
require 'coveralls'

0 commit comments

Comments
 (0)