Skip to content

Commit 5b20f2a

Browse files
committed
Add integration test to show missing documented DSL method (.insert)
It is documented in the README for this project that there is a DSL method (`.insert`) that can be utilised but this is not the case. The notable passage is: ``` You can add your custom middleware with use, that push the middleware onto the stack, and you can also control where the middleware is inserted using insert, insert_before and insert_after. ``` The notable example of this is: ``` class CustomOverwriter < Grape::Middleware::Base def after [200, { 'Content-Type' => 'text/plain' }, [@options[:message]]] end end class API < Grape::API use Overwriter insert_before Overwriter, CustomOverwriter, message: 'Overwritten again.' insert 0, CustomOverwriter, message: 'Overwrites all other middleware.' get '/' do end end ``` This method is not actually available though and when invoked will raise a NoMethodError. This appears to have just been an error of omission in the original commit adding the middleware stack and extending the DSL to include the ability to interact with it in a more granular fashion (c2e41f7). This commit adds a failing test case which attempts to utilise this documented feature.
1 parent c688af1 commit 5b20f2a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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|

0 commit comments

Comments
 (0)