From f9e52fc0cbadee127862cf40403fc185350a434d Mon Sep 17 00:00:00 2001 From: Edward Rudd Date: Wed, 29 Apr 2015 09:41:46 -0400 Subject: [PATCH] fix the Globals middleware - add to the autoload configuration - fix issues with it accessing an undefined var/method --- CHANGELOG.md | 1 + lib/grape.rb | 1 + lib/grape/middleware/globals.rb | 3 ++- spec/grape/middleware/globals_spec.rb | 27 +++++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 spec/grape/middleware/globals_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cfa5408d4..dc933a3f45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/grape.rb b/lib/grape.rb index 9ed945df93..85ac50f979 100644 --- a/lib/grape.rb +++ b/lib/grape.rb @@ -104,6 +104,7 @@ module Middleware autoload :Versioner autoload :Formatter autoload :Error + autoload :Globals module Auth extend ActiveSupport::Autoload diff --git a/lib/grape/middleware/globals.rb b/lib/grape/middleware/globals.rb index ec3aa16954..126d8a5e63 100644 --- a/lib/grape/middleware/globals.rb +++ b/lib/grape/middleware/globals.rb @@ -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 diff --git a/spec/grape/middleware/globals_spec.rb b/spec/grape/middleware/globals_spec.rb new file mode 100644 index 0000000000..d7d14c0bdf --- /dev/null +++ b/spec/grape/middleware/globals_spec.rb @@ -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