Skip to content

Commit

Permalink
Correctly initialize env and headers (#1446)
Browse files Browse the repository at this point in the history
Closes #1444.
  • Loading branch information
leifg authored and dblock committed Jul 20, 2016
1 parent c9a81a8 commit 21925fc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
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

@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

0 comments on commit 21925fc

Please sign in to comment.