Skip to content

Instantly share code, notes, and snippets.

@madis
Last active Mar 14, 2019
Embed
What would you like to do?
Simple Webrick server for testing callbacks in ruby tests
require 'webrick'
require 'rack'
require 'rack/query_parser'
class CallbackServer
ENDPOINT = '/callbacks'
attr_reader :requests
def initialize
@requests = []
end
def start
Thread.abort_on_exception = true
@server_thread = Thread.new { server.start }
end
def stop
@server.shutdown
end
def callback_url
"http://localhost:#{port}#{ENDPOINT}"
end
def port
@server[:Port]
end
def server
logger = WEBrick::Log::new("/dev/null", WEBrick::Log::DEBUG) # ERROR
@server ||= WEBrick::HTTPServer.new(Logger: logger, :Port => 0)
@server.mount_proc(ENDPOINT) do |req, res|
@requests << Rack::QueryParser.make_default(100, 100).parse_query(req.body)
end
@server
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment