How do I check if any particular rack middleware is being used?
To be more specific, I'm talking about sentry-raven
and sinatra
here. I've seen examples of testing applications sinatra
or middlewares. But I haven't seen them test if any particular middleware is present. Or should I be testing the behavior, not the configuration (or what should I call it)?
With the help ruby-raven
guys , we have this:
ENV['RACK_ENV'] = 'test'
# the app: start
require 'sinatra'
require 'sentry-raven'
Raven.configure(true) do |config|
config.dsn = '...'
end
use Raven::Rack
get '/' do
'Hello, world!'
end
# the app: end
require 'rspec'
require 'rack/test'
Raven.configure do |config|
logger = ::Logger.new(STDOUT)
logger.level = ::Logger::WARN
config.logger = logger
end
describe 'app' do
include Rack::Test::Methods
def app
@app || Sinatra::Application
end
class TestRavenError < StandardError; end
it 'sends errors to sentry' do
@app = Class.new Sinatra::Application do
get '/' do
raise TestRavenError
end
end
allow(Raven.client).to receive(:send).and_return(true)
begin
get '/'
rescue TestRavenError
end
expect(Raven.client).to have_received(:send)
end
end
Or if it raven
sends requests to the path (when tests fail because of the request being sent raven
, and not because of an underlying error), you can disable them:
Raven.configure(true) do |config|
config.should_send = Proc.new { false }
end
And instead of mock Raven.send_or_skip
:
...
allow(Raven).to receive(:send_or_skip).and_return(true)
begin
get '/'
rescue TestRavenError
end
expect(Raven).to have_received(:send_or_skip)
...
The important thing (I would say) is the behavior, but if you want to check for the presence of middleware, there are two ways I suggest you after diving into the Sinatra source (probably much easier / better):
env
There is a method in the Sinatra source that uses env to check for the presence of middleware :
# Behaves exactly like Rack::CommonLogger with the notable exception that it does nothing,
# if another CommonLogger is already in the middleware chain.
class CommonLogger < Rack::CommonLogger
def call(env)
env['sinatra.commonlogger'] ? @app.call(env) : super
end
You can do the same in a route for example.
get "/env-keys" do
env.keys.inspect
end
It will only show you middleware if it inserts something into the env hash for example.
class MyBad
def initialize app, options={}
@app = app
@options = options
end
def call env
@app.call env.merge("mybad" => "I'm sorry!")
end
end
output:
["SERVER_SOFTWARE", "SERVER_NAME", "rack.input", "rack.version", "rack.errors", "rack.multithread", "rack.multiprocess", "rack.run_once", "REQUEST_METHOD", "REQUEST_PATH", "PATH_INFO", "REQUEST_URI", "HTTP_VERSION", "HTTP_HOST", "HTTP_CONNECTION", "HTTP_CACHE_CONTROL", "HTTP_ACCEPT", "HTTP_USER_AGENT", "HTTP_DNT", "HTTP_ACCEPT_ENCODING", "HTTP_ACCEPT_LANGUAGE", "GATEWAY_INTERFACE", "SERVER_PORT", "QUERY_STRING", "SERVER_PROTOCOL", "rack.url_scheme", "SCRIPT_NAME", "REMOTE_ADDR", "async.callback", "async.close", "rack.logger", "mybad", "rack.request.query_string", "rack.request.query_hash", "sinatra.route"]
He is near the end of this list.
Middle layer method
There is also a method called middleware
in Sinatra :: Base :
# Middleware used in this class and all superclasses.
def middleware
if superclass.respond_to?(:middleware)
superclass.middleware + @middleware
else
@middleware
end
end
Call it in your modular app class definition and you can get middlewares in an array:
require 'sinatra/base'
class AnExample < Sinatra::Base
use MyBad
warn "self.middleware = #{self.middleware}"
output:
self.middleware = [[MyBad, [], nil]]
There might be a way to get it from Sinatra :: Application, but I haven't looked.