Skip to content

Commit afaba48

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

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-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: 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)