Skip to content

Instantly share code, notes, and snippets.

@fabrizioc1
Created October 11, 2011 19:49
Show Gist options
  • Save fabrizioc1/1279190 to your computer and use it in GitHub Desktop.
Save fabrizioc1/1279190 to your computer and use it in GitHub Desktop.
Create a self contained Rack server for use with unit tests
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment