require 'rack' require 'webrick' require 'thread' require 'tap/test/subset_test' require 'stringio' module Tap module Test # HttpTest facilitates testing of HTTP requests by initializing a # Webrick server that echos requests, and providing methods to # validate echoed requests. # module HttpTest class MockServer def initialize(body, status=200, headers={}) @response = [status, headers, [body]] end def call(env) @response end end class EchoServer def self.call(env) body = env['rack.input'].read headers = {} env.each_pair {|key, value| headers[key] = [value] unless key =~ /^rack/ } [200, headers, [body]] end end def self.included(base) base.send(:include, Tap::Test::SubsetTest) end def default_config(log_dev=StringIO.new('')) common_logger = WEBrick::Log.new(log_dev, WEBrick::Log.const_get(:WARN) ) { :Port => 2000, :Logger => common_logger, :AccessLog => common_logger } end def web_test(app=EchoServer, config=default_config) subset_test("WEB", "w") do begin server = ::WEBrick::HTTPServer.new(config); server.mount("/", Rack::Handler::WEBrick, app); Thread.new { server.start } yield ensure server.shutdown end end end end end end