Skip to content

Commit 5ae64f0

Browse files
authored
Merge pull request #1787 from michaellennox/add-middleware-insert-dsl
Add documented but not implemented feature so a user can insert a middleware at a specific point in the stack
2 parents c688af1 + 435ff14 commit 5ae64f0

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
* Your contribution here.
1010
* [#1776](https://github.com/ruby-grape/grape/pull/1776): Validate response returned by the exception handler - [@darren987469](https://github.com/darren987469).
11+
* [#1787](https://github.com/ruby-grape/grape/pull/1787): Add documented but not implemented ability to `.insert` a middleware in the stack - [@michaellennox](https://github.com/michaellennox).
1112

1213
### 1.1.0 (8/4/2018)
1314

lib/grape/dsl/middleware.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ def use(middleware_class, *args, &block)
2121
namespace_stackable(:middleware, arr)
2222
end
2323

24+
def insert(*args, &block)
25+
arr = [:insert, *args]
26+
arr << block if block_given?
27+
28+
namespace_stackable(:middleware, arr)
29+
end
30+
2431
def insert_before(*args, &block)
2532
arr = [:insert_before, *args]
2633
arr << block if block_given?

spec/grape/api_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,28 @@ def call(env)
13481348
end
13491349
end
13501350

1351+
describe '.insert' do
1352+
it 'inserts middleware in a specific location in the stack' do
1353+
m = Class.new(Grape::Middleware::Base) do
1354+
def call(env)
1355+
env['phony.args'] ||= []
1356+
env['phony.args'] << @options[:message]
1357+
@app.call(env)
1358+
end
1359+
end
1360+
1361+
subject.use ApiSpec::PhonyMiddleware, 'bye'
1362+
subject.insert 0, m, message: 'good'
1363+
subject.insert 0, m, message: 'hello'
1364+
subject.get '/' do
1365+
env['phony.args'].join(' ')
1366+
end
1367+
1368+
get '/'
1369+
expect(last_response.body).to eql 'hello good bye'
1370+
end
1371+
end
1372+
13511373
describe '.http_basic' do
13521374
it 'protects any resources on the same scope' do
13531375
subject.http_basic do |u, _p|

spec/grape/dsl/middleware_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ class Dummy
2222
end
2323
end
2424

25+
describe '.insert' do
26+
it 'adds a middleware with the right operation' do
27+
expect(subject).to receive(:namespace_stackable).with(:middleware, [:insert, 0, :arg1, proc])
28+
29+
subject.insert 0, :arg1, &proc
30+
end
31+
end
32+
2533
describe '.insert_before' do
2634
it 'adds a middleware with the right operation' do
2735
expect(subject).to receive(:namespace_stackable).with(:middleware, [:insert_before, foo_middleware, :arg1, proc])

0 commit comments

Comments
 (0)