public
Created

Create a self contained Rack server for use with unit tests

  • Download Gist
rack_mini_server.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
module Rack::MiniServer
 
def start
@server_pid = fork do
puts "start forking ..."
Rack::Handler::WEBrick.run(
Rack::ShowExceptions.new(Rack::Lint.new(self.new)), :Port => 9292) do |server|
['INT', 'TERM'].each{ |signal| trap(signal){ puts "shutting down"; server.shutdown} }
end
end
sleep 2
end
 
def stop
Process.kill('INT',@server_pid)
end
 
end
 
class TestHelloWorld
extend Rack::MiniServer
 
def initialize
super
puts "server ready"
end
 
def call(env)
[200, {"Content-Type" => "text/plain"}, ["Hello world!"]]
end
 
end
 
require 'httpclient'
 
HelloWorld.start
 
client = HTTPClient.new
assert_equal "Hello World",client.get_content("http://localhost:9292/")
 
HelloWorld.stop

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.