Skip to content

Commit f9e52fc

Browse files
committed
fix the Globals middleware
- add to the autoload configuration - fix issues with it accessing an undefined var/method
1 parent c9c0fd4 commit f9e52fc

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).
2020
* [#942](https://github.com/intridea/grape/pull/942): Fixed forced presence for optional params when based on a reused entity that was also required in another context - [@croeck](https://github.com/croeck).
2121
* [#1001](https://github.com/intridea/grape/pull/1001): Fixed calling endpoint with specified format with format in its path - [@hodak](https://github.com/hodak).
22+
* [#1005](https://github.com/intridea/grape/pull/1005): Fixed the Grape::Middleware::Globals - [@urkle](https://github.com/urkle).
2223

2324
* Your contribution here.
2425

lib/grape.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ module Middleware
104104
autoload :Versioner
105105
autoload :Formatter
106106
autoload :Error
107+
autoload :Globals
107108

108109
module Auth
109110
extend ActiveSupport::Autoload

lib/grape/middleware/globals.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module Grape
44
module Middleware
55
class Globals < Base
66
def before
7-
@env['grape.request'] = Grape::Request.new(@env)
7+
request = Grape::Request.new(@env)
8+
@env['grape.request'] = request
89
@env['grape.request.headers'] = request.headers
910
@env['grape.request.params'] = request.params if @env['rack.input']
1011
end

spec/grape/middleware/globals_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'spec_helper'
2+
3+
describe Grape::Middleware::Globals do
4+
subject { Grape::Middleware::Globals.new(blank_app) }
5+
before { allow(subject).to receive(:dup).and_return(subject) }
6+
7+
let(:blank_app) { lambda { |env| [200, {}, 'Hi there.'] } }
8+
9+
it 'calls through to the app' do
10+
expect(subject.call({})).to eq([200, {}, 'Hi there.'])
11+
end
12+
13+
context 'environment' do
14+
it 'should set the grape.request environment' do
15+
subject.call({})
16+
expect(subject.env['grape.request']).to be_a(Grape::Request)
17+
end
18+
it 'should set the grape.request.headers environment' do
19+
subject.call({})
20+
expect(subject.env['grape.request.headers']).to be_a(Hash)
21+
end
22+
it 'should set the grape.request.params environment' do
23+
subject.call('QUERY_STRING' => 'test=1', 'rack.input' => StringIO.new)
24+
expect(subject.env['grape.request.params']).to be_a(Hash)
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)