Skip to content

Commit 0b2b69c

Browse files
committed
correctly initialize env and headers
1 parent c9a81a8 commit 0b2b69c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,20 @@ 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 = env['HTTP_X_CUSTOM_HEADER']
563+
end
564+
subject.get 'example' do
565+
'example'
566+
end
567+
put '/example'
568+
expect(last_response.status).to eql 405
569+
expect(last_response.body).to eql '405 Not Allowed'
570+
end
571+
end
572+
559573
specify '405 responses includes an Allow header specifying supported methods' do
560574
subject.get 'example' do
561575
'example'

0 commit comments

Comments
 (0)