-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor initializing of proxy servers in specs
Closes #176
- Loading branch information
Showing
7 changed files
with
87 additions
and
76 deletions.
There are no files selected for viewing
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
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,37 @@ | ||
require "webrick/httpproxy" | ||
|
||
handler = proc { |_, res| res["X-PROXIED"] = true } | ||
|
||
ProxyServer = WEBrick::HTTPProxyServer.new( | ||
:Port => 8080, | ||
:AccessLog => [], | ||
:RequestCallback => handler | ||
) | ||
|
||
AuthenticatedProxyServer = WEBrick::HTTPProxyServer.new( | ||
:Port => 8081, | ||
:ProxyAuthProc => proc do | req, res | | ||
WEBrick::HTTPAuth.proxy_basic_auth(req, res, "proxy") do | user, pass | | ||
require "support/black_hole" | ||
require "support/servers/config" | ||
require "support/servers/runner" | ||
|
||
class ProxyServer < WEBrick::HTTPProxyServer | ||
include ServerConfig | ||
|
||
CONFIG = { | ||
:BindAddress => "127.0.0.1", | ||
:Port => 0, | ||
:AccessLog => BlackHole, | ||
:Logger => BlackHole, | ||
:RequestCallback => proc { |_, res| res["X-PROXIED"] = true } | ||
}.freeze | ||
|
||
def initialize | ||
super CONFIG | ||
end | ||
end | ||
|
||
class AuthProxyServer < WEBrick::HTTPProxyServer | ||
include ServerConfig | ||
|
||
AUTHENTICATOR = proc do |req, res| | ||
WEBrick::HTTPAuth.proxy_basic_auth(req, res, "proxy") do |user, pass| | ||
user == "username" && pass == "password" | ||
end | ||
end, | ||
:RequestCallback => handler | ||
) | ||
|
||
Thread.new { ProxyServer.start } | ||
trap("INT") do | ||
ProxyServer.shutdown | ||
exit | ||
end | ||
end | ||
|
||
CONFIG = ProxyServer::CONFIG.merge :ProxyAuthProc => AUTHENTICATOR | ||
|
||
Thread.new { AuthenticatedProxyServer.start } | ||
trap("INT") do | ||
AuthenticatedProxyServer.shutdown | ||
exit | ||
def initialize | ||
super CONFIG | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module ServerConfig | ||
def addr | ||
config[:BindAddress] | ||
end | ||
|
||
def port | ||
config[:Port] | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module ServerRunner | ||
def run_server(name) | ||
let! name do | ||
server = yield | ||
Thread.new { server.start } | ||
server | ||
end | ||
|
||
after { send(name).shutdown } | ||
end | ||
end | ||
|
||
RSpec.configure { |c| c.extend ServerRunner } |