Created
February 8, 2024 22:35
-
-
Save fallwith/159ae97362ff355da6a612dc824dea66 to your computer and use it in GitHub Desktop.
A "fake" HTTP server for local development or testing of HTTP clients
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
#!/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