Last active
March 14, 2019 12:38
-
-
Save madis/05b77c1a133826ef1eab53a4481e1920 to your computer and use it in GitHub Desktop.
Simple Webrick server for testing callbacks in ruby 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
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