Last active
December 21, 2015 03:58
-
-
Save marcosccm/6245538 to your computer and use it in GitHub Desktop.
WEBrick dummy server
This file contains hidden or 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
require 'webrick' | |
json = File.read("./file.json") | |
class Server < WEBrick::HTTPServlet::AbstractServlet | |
def initialize(server, json) | |
super(server) | |
@json = json | |
end | |
def do_GET(request, response) | |
response.status = 200 | |
response['Content-Type'] = 'application/text' | |
response.body = @json | |
end | |
end | |
server = WEBrick::HTTPServer.new :Port => 8000 | |
server.mount "/bla", Server, json | |
trap 'INT' do server.shutdown end | |
pid = fork { server.start } | |
puts `curl localhost:8000/bla --max-time 1` # then I can talk to the webrick | |
Process.kill('INT', pid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment