如何测试是否正在使用某些特定的机架中间件?

时间:2015-01-12 21:06:22

标签: ruby testing sinatra rack sentry

更具体地说,我在这里谈论sentry-ravensinatra。我看到了测试sinatra应用程序或中间件的示例。但是我没有看到测试是否存在某些特定的中间件。或者我应该测试行为,而不是配置(或者我应该如何调用它)?

2 个答案:

答案 0 :(得分:1)

重要的是(我会说)是行为,但是如果你想检查中间件,我会在深入研究Sinatra source后提出两种方法(可能更容易/更好的方法):

env

在Sinatra来源there's a method that uses the env to check if a middleware is already present

# 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

你可以在路线中做同样的事情,例如

get "/env-keys" do
  env.keys.inspect
end

它只会向您显示中间件,如果它是在env哈希中插入的东西,例如

class MyBad
  def initialize app, options={}
    @app = app
    @options = options
  end

  def call env
    @app.call env.merge("mybad" => "I'm sorry!")
  end
end

输出:

["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"]

它接近该列表的末尾。

中间件方法

There's 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

在模块化应用程序的类定义中调用它,您可以在数组中获取中间件:

require 'sinatra/base'

class AnExample < Sinatra::Base
  use MyBad
  warn "self.middleware = #{self.middleware}"

输出:

self.middleware = [[MyBad, [], nil]]

可能有办法从Sinatra :: Application获得它,但我没看过。

答案 1 :(得分:0)

ruby-raven guys的帮助下,我们得到了这个:

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

或者如果raven发送请求的方式(由于raven发送请求导致测试失败,而不是因为潜在的错误),可以禁用它们:

Raven.configure(true) do |config|
  config.should_send = Proc.new { false }
end

然后模仿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)
...