Last active
August 29, 2015 14:05
-
-
Save mjstrasser/8c79e8961e1d0223150b to your computer and use it in GitHub Desktop.
Web proxy that injects HTTP headers for test purposes
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
# 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