1

I have a rack application (specifically a Sinatra one, but I don't think that matters) which, when running normally, happily outputs a bunch of information about the requests which are being made of it to STDOUT eg:

127.0.0.1 - - [25/Jul/2013 10:05:39] "GET /oath2/token?password=ohnoes HTTP/1.1" 404 507 0.0013

I'm trying write an extension for Rack::CommonLogger that will remove passwords from the logfile, so of course my first task is to write a test.

I have rack/test set up with rspec like so, but I can't figure out how to capture the outgoing logs so I can scan what's in them! Any ideas?

require 'my_webapp'

describe "My Webapp" do
  include Rack::Test::Methods

  def app
    @app ||= MyWebapp.new
  end

  it 'should not log the text of any GET password parameter' do
    get '/oauth2/token?password=ohnoes'

    # Not sure about this!

    log_output.should_not =~ /ohnoes/
  end
end
4

1 回答 1

1

您需要对自定义记录器调用以写入其输出的任何方法进行存根并设置期望。

因此,例如,如果您的自定义记录器STDERR使用单个参数直接写入,您将使用:

STDERR.stub(:write) {|arg| expect(arg).to_not match(/password/)}

如果您的自定义记录器写入默认记录器(即env['rack.errors'])而不是 STDERR,那么您将write改为存根该对象的方法。我会展示这个例子,但我不知道如何在 RSpec 测试中掌握 Rack 环境。

关于这个实现的几点说明:

  • 不赞成使用any_number_of_times, 如 in STDERR.should_receive(:write).any_number_of_timesstub
  • 没有合理的正则表达式来匹配包含特定字符串的字符串,因此没有合理的方式来使用表单STDERR.stub(:write).with(/regex/)
于 2013-07-28T16:21:24.300 回答