Created
October 11, 2011 19:49
-
-
Save fabrizioc1/1279190 to your computer and use it in GitHub Desktop.
Create a self contained Rack server for use with unit tests
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
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