Skip to content
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

Correctly initialize env and headers #1446

Merged
merged 2 commits into from
Jul 20, 2016
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#### Fixes

* [#1446](https://github.com/ruby-grape/grape/pull/1446): Fix for `env` inside `before` when using not allowed method - [@leifg](https://github.com/leifg).
* [#1430](https://github.com/ruby-grape/grape/pull/1430): Fix for `declared(params)` inside `route_param` - [@Arkanain](https://github.com/Arkanain).
* [#1405](https://github.com/ruby-grape/grape/pull/1405): Fix priority of `rescue_from` clauses applying - [@hedgesky](https://github.com/hedgesky).
* [#1365](https://github.com/ruby-grape/grape/pull/1365): Fix finding exception handler in error middleware - [@ktimothy](https://github.com/ktimothy).
Expand Down
7 changes: 7 additions & 0 deletions lib/grape/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ def method_not_allowed(env, methods, endpoint)
env[Grape::Env::GRAPE_METHOD_NOT_ALLOWED] = true
current = endpoint.dup
current.instance_eval do
@env = env
@header = {}

@request = Grape::Request.new(env)
@params = @request.params
@headers = @request.headers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am merging this. But I wonder whether we can introduce a module/class/code that can be injected here and in other places where these properties would be memoized the first time we request them. Especially the construction of Grape::Request is not free as it parses the environment.


@lazy_initialized = false
lazy_initialize!
run_filters befores, :before
Expand Down
15 changes: 15 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,21 @@ def subject.enable_root_route!
end
end

context 'when accessing env' do
it 'returns a 405 for an unsupported method' do
subject.before do
_custom_header_1 = headers['X-Custom-Header']
_custom_header_2 = env['HTTP_X_CUSTOM_HEADER']
end
subject.get 'example' do
'example'
end
put '/example'
expect(last_response.status).to eql 405
expect(last_response.body).to eql '405 Not Allowed'
end
end

specify '405 responses includes an Allow header specifying supported methods' do
subject.get 'example' do
'example'
Expand Down