Skip to content

Instantly share code, notes, and snippets.

@fallwith
Created February 8, 2024 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fallwith/159ae97362ff355da6a612dc824dea66 to your computer and use it in GitHub Desktop.
Save fallwith/159ae97362ff355da6a612dc824dea66 to your computer and use it in GitHub Desktop.
A "fake" HTTP server for local development or testing of HTTP clients
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'webrick'
class FakeServlet < WEBrick::HTTPServlet::AbstractServlet
RESPONSE_BODY = "Success\n"
TEXT_PLAIN = 'text/plain'
def do_GET(_request, response)
# query hash is accessible via request.query
# request path is accessible via request.path
response.status = 200
response.content_type = TEXT_PLAIN
response.body = RESPONSE_BODY
end
end
class FakeServer
DEFAULT_PORT = 80
def initialize(port = nil)
port ||= DEFAULT_PORT
@server = WEBrick::HTTPServer.new(Port: port)
@server.mount '/', FakeServlet
@server.start
end
end
port = ARGV[0].to_i unless ARGV.empty?
FakeServer.new(port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment