Created
June 7, 2010 03:28
-
-
Save deepfryed/428193 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'gems/environment' | |
require 'rack' | |
require 'thin' | |
require 'em-http' | |
module Rack | |
module TestServer | |
def self.run app, middlewares=[], &block | |
app = app.kind_of?(Class) ? Class.new(app) : app | |
EM.run { | |
server = Thread.new do | |
Thin::Logging.silent = true | |
Thin::Server.start('127.0.0.1', 12345) do | |
middlewares.each {|m| use m } | |
use Rack::TestServer::Logger | |
run app | |
end | |
end | |
sleep 0.1 | |
client = Thread.new { block.call(app) } | |
server.join | |
client.join | |
} | |
end | |
def self.logs | |
@@logs ||= [] | |
end | |
class Logger | |
def initialize app | |
@app = app | |
end | |
def call env | |
res = @app.call env | |
TestServer.logs << [ env, res ] | |
res | |
end | |
end | |
end | |
end | |
Rack::TestServer.run lambda {|env| [ 200, {}, 'ok' ]} do |app| | |
http = EventMachine::HttpRequest.new('http://127.0.0.1:12345/').get | |
http.callback { p http.response; EM.stop } | |
http.errback { puts "failed"; EM.stop } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment