Given a Rack app that is not Rails,
builder.rb:
def app
Rack::Builder.new{
use Rack::Static, urls:static_paths, root:'public'
run ThaApp
}.to_app
end
How to inject a testing middleware using the spec_helper?
Given a Rack app that is not Rails,
builder.rb:
def app
Rack::Builder.new{
use Rack::Static, urls:static_paths, root:'public'
run ThaApp
}.to_app
end
How to inject a testing middleware using the spec_helper?
If you're using Builder (with use, run, etc.) it does not look like you can easily inject or remove middleware at runtime. Here's the code: https://github.com/rack/rack/blob/master/lib/rack/builder.rb
Notice that it builds the stack of middleware, and when you call run it instantiates the stack (called "@use") in a tree of middleware objects which each have a reference to the next one - see methods "use" and "to_app".
So: don't think Builder is designed to allow dynamically adding and subtracting middleware in the stack.
You could rebuild a new dynamic stack, or use multiple Rack apps with and without testing middleware, or do some backflips like Rails does to dynamically reconfigure the stack.
You could also add a testing middleware only in test mode, or one that can be deactivated easily so it becomes a pass-through middleware. Then your spec_helper would just set and clear the variable telling it to pass through.
Since I want to prepend to the middleware stack, solving for this particular use case was easy.
Given a app defined as above named "app", add the new middleware: use ...
def new_app
Rack::Builder.new do
use ...
use ...
run app
end.to_app
end