Skip to content

Commit

Permalink
Merge pull request #1005 from urkle/fix-globals-middleware
Browse files Browse the repository at this point in the history
fix the Globals middleware
  • Loading branch information
dblock committed Apr 30, 2015
2 parents c9c0fd4 + f9e52fc commit dd0cae2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [#936](/~https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](/~https://github.com/dm1try).
* [#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).
* [#1001](/~https://github.com/intridea/grape/pull/1001): Fixed calling endpoint with specified format with format in its path - [@hodak](/~https://github.com/hodak).
* [#1005](/~https://github.com/intridea/grape/pull/1005): Fixed the Grape::Middleware::Globals - [@urkle](/~https://github.com/urkle).

* Your contribution here.

Expand Down
1 change: 1 addition & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ module Middleware
autoload :Versioner
autoload :Formatter
autoload :Error
autoload :Globals

module Auth
extend ActiveSupport::Autoload
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/middleware/globals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Grape
module Middleware
class Globals < Base
def before
@env['grape.request'] = Grape::Request.new(@env)
request = Grape::Request.new(@env)
@env['grape.request'] = request
@env['grape.request.headers'] = request.headers
@env['grape.request.params'] = request.params if @env['rack.input']
end
Expand Down
27 changes: 27 additions & 0 deletions spec/grape/middleware/globals_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

describe Grape::Middleware::Globals do
subject { Grape::Middleware::Globals.new(blank_app) }
before { allow(subject).to receive(:dup).and_return(subject) }

let(:blank_app) { lambda { |env| [200, {}, 'Hi there.'] } }

it 'calls through to the app' do
expect(subject.call({})).to eq([200, {}, 'Hi there.'])
end

context 'environment' do
it 'should set the grape.request environment' do
subject.call({})
expect(subject.env['grape.request']).to be_a(Grape::Request)
end
it 'should set the grape.request.headers environment' do
subject.call({})
expect(subject.env['grape.request.headers']).to be_a(Hash)
end
it 'should set the grape.request.params environment' do
subject.call('QUERY_STRING' => 'test=1', 'rack.input' => StringIO.new)
expect(subject.env['grape.request.params']).to be_a(Hash)
end
end
end

0 comments on commit dd0cae2

Please sign in to comment.