Skip to content

Instantly share code, notes, and snippets.

@mjstrasser
Last active August 29, 2015 14:05
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 mjstrasser/8c79e8961e1d0223150b to your computer and use it in GitHub Desktop.
Save mjstrasser/8c79e8961e1d0223150b to your computer and use it in GitHub Desktop.
Web proxy that injects HTTP headers for test purposes
# Logging class that logs nothing. The default is INFO logging to $stderr.
class NullLog < WEBrick::BasicLog
def log(level, data)
# Do nothing.
end
end
# Subclass of WEBrick::HTTPProxyServer that injects HTTP headers into the request.
#
# Specifying a RequestCallback proc does not work because the WEBrick::HTTPRequest object
# passed to the proc does not allow headers to be added to it.
class HeadersProxy < WEBrick::HTTPProxyServer
def initialize(port, headers)
opts = {
Port: port,
AccessLog: [], # We don't need a proxy server to log access.
Logger: NullLog.new # Not interested in other logging either.
}
super(opts)
@extra_headers = Hash(headers)
end
# Call the original method to assemble the header hash, then add the extras.
def setup_proxy_header(req, res)
super(req, res).merge(@extra_headers)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment