Skip to content

Instantly share code, notes, and snippets.

@tatat
Last active December 20, 2015 11:09
Show Gist options
  • Save tatat/6120811 to your computer and use it in GitHub Desktop.
Save tatat/6120811 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rack'
require 'net/http'
require 'uri'
unused_port = -> do
s = TCPServer.open(0)
port = s.addr[1]
s.close
port
end.()
pid = fork do
Rack::Handler::WEBrick.run \
->(env) { [200, {'Content-Type' => 'text/plain'}, ['Nyan!']] },
Port: unused_port,
Logger: WEBrick::Log.new("/dev/null"),
AccessLog: []
end
sleep 1
begin
puts Net::HTTP.get(URI.parse("http://localhost:#{unused_port}/"))
ensure
Process.kill 'TERM', pid
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment