Skip to content

Commit

Permalink
Fix default response headers to work with Rack 3 (#2455)
Browse files Browse the repository at this point in the history
* Default response headers are using Grape::Util::Header for rack 3 compatibility
Add gcompat for nokogiri in Dockerfile. Needed for testing a Rails app
Add tzinfo-data in Rails's Gemfiles. Needed for testing a Rails app
Add integration test rails thats mounts a Grape API within a Rails App
Move railtie_spec.rb to rails integration

* Add CHANGELOG.md
Add rails integrations tests

* Fix config.load_defaults

* Fix config.load_defaults in railtie_spec.rb

* Change anonymous class to named class with stub_const

* Reset Singleton ActiveSupport::Dependencies.autoload_paths and autoload_once_paths

* Add comment about ActiveSupport::Dependencies

* Replace responds by cascades
  • Loading branch information
ericproulx authored Jun 20, 2024
1 parent 3a26c2c commit b1123d8
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 23 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ jobs:
- ruby: '3.3'
gemfile: gemfiles/dry_validation.gemfile
specs: 'spec/integration/dry_validation'
- ruby: '3.3'
gemfile: gemfiles/rails_6_1.gemfile
specs: 'spec/integration/rails'
- ruby: '3.3'
gemfile: gemfiles/rails_7_0.gemfile
specs: 'spec/integration/rails'
- ruby: '3.3'
gemfile: gemfiles/rails_7_1.gemfile
specs: 'spec/integration/rails'
runs-on: ubuntu-latest
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#### Fixes

* [#2453](https://github.com/ruby-grape/grape/pull/2453): Fix context in rescue_from - [@ericproulx](https://github.com/ericproulx).
* [#2455](https://github.com/ruby-grape/grape/pull/2455): Fix default response headers to work with Rack 3 - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

### 2.1.0 (2024/06/15)
Expand Down
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ FROM ruby:$RUBY_VERSION-alpine
ENV BUNDLE_PATH /usr/local/bundle/gems
ENV LIB_PATH /var/grape

RUN apk add --update --no-cache make gcc git libc-dev && \
RUN apk add --update --no-cache make gcc git libc-dev gcompat && \
gem update --system && gem install bundler

WORKDIR $LIB_PATH

COPY /docker/entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

ENTRYPOINT ["docker-entrypoint.sh"]
ENTRYPOINT ["docker-entrypoint.sh"]
1 change: 1 addition & 0 deletions gemfiles/rails_6_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
eval_gemfile '../Gemfile'

gem 'rails', '~> 6.0.0'
gem 'tzinfo-data', require: false
1 change: 1 addition & 0 deletions gemfiles/rails_6_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
eval_gemfile '../Gemfile'

gem 'rails', '~> 6.1'
gem 'tzinfo-data', require: false
1 change: 1 addition & 0 deletions gemfiles/rails_7_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
eval_gemfile '../Gemfile'

gem 'rails', '~> 7.0.0'
gem 'tzinfo-data', require: false
1 change: 1 addition & 0 deletions gemfiles/rails_7_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
eval_gemfile '../Gemfile'

gem 'rails', '~> 7.1.0'
gem 'tzinfo-data', require: false
1 change: 1 addition & 0 deletions gemfiles/rails_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
eval_gemfile '../Gemfile'

gem 'rails', github: 'rails/rails'
gem 'tzinfo-data', require: false
3 changes: 2 additions & 1 deletion lib/grape/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ def with_optimization
end

def default_response
[404, { Grape::Http::Headers::X_CASCADE => 'pass' }, ['404 Not Found']]
headers = Grape::Util::Header.new.merge(Grape::Http::Headers::X_CASCADE => 'pass')
[404, headers, ['404 Not Found']]
end

def match?(input, method)
Expand Down
20 changes: 0 additions & 20 deletions spec/grape/railtie_spec.rb

This file was deleted.

51 changes: 51 additions & 0 deletions spec/integration/rails/mounting_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

describe 'Rails', if: defined?(Rails) do
context 'rails mounted' do
let(:api) do
Class.new(Grape::API) do
get('/test_grape') { 'rails mounted' }
end
end

let(:app) do
require 'rails'
require 'action_controller/railtie'

# https://github.com/rails/rails/issues/51784
# same error as described if not redefining the following
ActiveSupport::Dependencies.autoload_paths = []
ActiveSupport::Dependencies.autoload_once_paths = []

Class.new(Rails::Application) do
config.eager_load = false
config.load_defaults "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}"
config.api_only = true
config.consider_all_requests_local = true
config.hosts << 'example.org'

routes.append do
mount GrapeApi => '/'

get 'up', to: lambda { |_env|
['200', {}, ['hello world']]
}
end
end
end

before do
stub_const('GrapeApi', api)
app.initialize!
end

it 'cascades' do
get '/test_grape'
expect(last_response).to be_successful
expect(last_response.body).to eq('rails mounted')
get '/up'
expect(last_response).to be_successful
expect(last_response.body).to eq('hello world')
end
end
end
25 changes: 25 additions & 0 deletions spec/integration/rails/railtie_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

if defined?(Rails) && ActiveSupport.gem_version >= Gem::Version.new('7.1')
describe Grape::Railtie do
describe '.railtie' do
subject { test_app.deprecators[:grape] }

let(:test_app) do
# https://github.com/rails/rails/issues/51784
# same error as described if not redefining the following
ActiveSupport::Dependencies.autoload_paths = []
ActiveSupport::Dependencies.autoload_once_paths = []

Class.new(Rails::Application) do
config.eager_load = false
config.load_defaults "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}"
end
end

before { test_app.initialize! }

it { is_expected.to be(Grape.deprecator) }
end
end
end

0 comments on commit b1123d8

Please sign in to comment.