Skip to content

Align implementation with Rack SPEC #1956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ matrix:
- bundle exec danger
- rvm: 2.7.0
gemfile: Gemfile
- rvm: 2.7.0
gemfile: gemfiles/rack1.gemfile
- rvm: 2.7.0
gemfile: gemfiles/rack2-0.gemfile
- rvm: 2.7.0
gemfile: gemfiles/rack_edge.gemfile
- rvm: 2.7.0
Expand Down
5 changes: 5 additions & 0 deletions gemfiles/rack1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

eval_gemfile('../Gemfile')

gem 'rack', '~> 1.0'
5 changes: 5 additions & 0 deletions gemfiles/rack2-0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

eval_gemfile('../Gemfile')

gem 'rack', '~> 1.0'
2 changes: 1 addition & 1 deletion lib/grape/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def default_options
end

def call(env)
dup.call!(env)
dup.call!(env).to_a
end

def call!(env)
Expand Down
6 changes: 3 additions & 3 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1903,9 +1903,9 @@ def foo
it 'avoids polluting global namespace' do
env = Rack::MockRequest.env_for('/')

expect(a.call(env)[2].body).to eq(['foo'])
expect(b.call(env)[2].body).to eq(['bar'])
expect(a.call(env)[2].body).to eq(['foo'])
expect(read_chunks(a.call(env)[2])).to eq(['foo'])
expect(read_chunks(b.call(env)[2])).to eq(['bar'])
expect(read_chunks(a.call(env)[2])).to eq(['foo'])
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def app
it 'includes headers passed as symbols' do
env = Rack::MockRequest.env_for('/headers')
env['HTTP_SYMBOL_HEADER'.to_sym] = 'Goliath passes symbols'
body = subject.call(env)[2].body.first
body = read_chunks(subject.call(env)[2]).join
expect(JSON.parse(body)['Symbol-Header']).to eq('Goliath passes symbols')
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/grape/integration/rack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
env = Rack::MockRequest.env_for('/', options)

expect(JSON.parse(app.call(env)[2].body.first)['params_keys']).to match_array('test')
expect(JSON.parse(read_chunks(app.call(env)[2]).join)['params_keys']).to match_array('test')
ensure
input.close
input.unlink
Expand Down
23 changes: 14 additions & 9 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,19 @@ def to_xml
subject.options[:content_types][:custom] = "don't care"
subject.options[:formatters][:custom] = ->(_obj, _env) { 'CUSTOM FORMAT' }
_, _, body = subject.call('PATH_INFO' => '/info.custom')
expect(body.body).to eq(['CUSTOM FORMAT'])
expect(read_chunks(body)).to eq(['CUSTOM FORMAT'])
end
context 'default' do
let(:body) { ['blah'] }
it 'uses default json formatter' do
_, _, body = subject.call('PATH_INFO' => '/info.json')
expect(body.body).to eq(['["blah"]'])
expect(read_chunks(body)).to eq(['["blah"]'])
end
end
it 'uses custom json formatter' do
subject.options[:formatters][:json] = ->(_obj, _env) { 'CUSTOM JSON FORMAT' }
_, _, body = subject.call('PATH_INFO' => '/info.json')
expect(body.body).to eq(['CUSTOM JSON FORMAT'])
expect(read_chunks(body)).to eq(['CUSTOM JSON FORMAT'])
end
end

Expand Down Expand Up @@ -379,12 +379,17 @@ def to_xml
end

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

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

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

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

Expand Down
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@
# so it should be set to true here as well to reflect that.
I18n.enforce_available_locales = true

module Chunks
def read_chunks(body)
buffer = []
body.each { |chunk| buffer << chunk }

buffer
end
end

RSpec.configure do |config|
config.include Chunks
config.include Rack::Test::Methods
config.include Spec::Support::Helpers
config.raise_errors_for_deprecations!
config.filter_run_when_matching :focus

config.before(:each) { Grape::Util::InheritableSetting.reset_global! }

# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
end

require 'coveralls'
Expand Down