Skip to content

Commit 21925fc

Browse files
leifgdblock
authored andcommitted
Correctly initialize env and headers (#1446)
Closes #1444.
1 parent c9a81a8 commit 21925fc

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#### Fixes
1515

16+
* [#1446](https://github.com/ruby-grape/grape/pull/1446): Fix for `env` inside `before` when using not allowed method - [@leifg](https://github.com/leifg).
1617
* [#1430](https://github.com/ruby-grape/grape/pull/1430): Fix for `declared(params)` inside `route_param` - [@Arkanain](https://github.com/Arkanain).
1718
* [#1405](https://github.com/ruby-grape/grape/pull/1405): Fix priority of `rescue_from` clauses applying - [@hedgesky](https://github.com/hedgesky).
1819
* [#1365](https://github.com/ruby-grape/grape/pull/1365): Fix finding exception handler in error middleware - [@ktimothy](https://github.com/ktimothy).

lib/grape/router.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ def method_not_allowed(env, methods, endpoint)
136136
env[Grape::Env::GRAPE_METHOD_NOT_ALLOWED] = true
137137
current = endpoint.dup
138138
current.instance_eval do
139+
@env = env
140+
@header = {}
141+
142+
@request = Grape::Request.new(env)
143+
@params = @request.params
144+
@headers = @request.headers
145+
139146
@lazy_initialized = false
140147
lazy_initialize!
141148
run_filters befores, :before

spec/grape/api_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,21 @@ def subject.enable_root_route!
556556
end
557557
end
558558

559+
context 'when accessing env' do
560+
it 'returns a 405 for an unsupported method' do
561+
subject.before do
562+
_custom_header_1 = headers['X-Custom-Header']
563+
_custom_header_2 = env['HTTP_X_CUSTOM_HEADER']
564+
end
565+
subject.get 'example' do
566+
'example'
567+
end
568+
put '/example'
569+
expect(last_response.status).to eql 405
570+
expect(last_response.body).to eql '405 Not Allowed'
571+
end
572+
end
573+
559574
specify '405 responses includes an Allow header specifying supported methods' do
560575
subject.get 'example' do
561576
'example'

0 commit comments

Comments
 (0)