Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use at_exit instead of overriding trap for interrupt to shutdown ProxyServer, AuthenticatedProxyServer #176

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions spec/lib/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,49 @@
end
end

context "with http proxy address and port" do
it "proxies the request" do
response = HTTP.via("127.0.0.1", 8080).get dummy.endpoint
expect(response.headers["X-Proxied"]).to eq "true"
context "with http proxy" do
let(:handler) { proc { |_, res| res["X-PROXIED"] = true } }

context "address and port" do
it "proxies the request" do
with_proxy(8080, handler) do
response = HTTP.via("127.0.0.1", 8080).get dummy.endpoint
expect(response.headers["X-Proxied"]).to eq "true"
end
end
end
end

context "with http proxy address, port username and password" do
it "proxies the request" do
response = HTTP.via("127.0.0.1", 8081, "username", "password").get dummy.endpoint
expect(response.headers["X-Proxied"]).to eq "true"
end
context "address, port username and password" do
it "proxies the request" do
with_auth_proxy(8081, handler, :user => "username", :password => "password") do
response = HTTP.via("127.0.0.1", 8081, "username", "password").get dummy.endpoint
expect(response.headers["X-Proxied"]).to eq "true"
end
end

it 'responds with the endpoint\'s body' do
response = HTTP.via("127.0.0.1", 8081, "username", "password").get dummy.endpoint
expect(response.to_s).to match(/<!doctype html>/)
it "responds with the endpoint\'s body" do
with_auth_proxy(8081, handler, :user => "username", :password => "password") do
response = HTTP.via("127.0.0.1", 8081, "username", "password").get dummy.endpoint
expect(response.to_s).to match(/<!doctype html>/)
end
end
end
end

context "with http proxy address, port, with wrong username and password" do
it "responds with 407" do
response = HTTP.via("127.0.0.1", 8081, "user", "pass").get dummy.endpoint
expect(response.status).to eq(407)
context "address, port, with wrong username and password" do
it "responds with 407" do
with_auth_proxy(8081, handler, :user => "username", :password => "password") do
response = HTTP.via("127.0.0.1", 8081, "user", "pass").get dummy.endpoint
expect(response.status).to eq(407)
end
end
end
end

context "without proxy port" do
it "raises an argument error" do
expect { HTTP.via("127.0.0.1") }.to raise_error HTTP::RequestError
context "without proxy port" do
it "raises an argument error" do
with_proxy(8080, handler) do
expect { HTTP.via("127.0.0.1") }.to raise_error HTTP::RequestError
end
end
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|

config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
Expand Down
21 changes: 21 additions & 0 deletions spec/support/example_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "webrick"
require "singleton"
require "forwardable"

require "support/example_server/servlet"

class ExampleServer
extend Forwardable

include Singleton

PORT = 65_432
ADDR = "127.0.0.1:#{PORT}"

def initialize
@server = WEBrick::HTTPServer.new :Port => PORT, :AccessLog => []
@server.mount "/", Servlet
end

delegate [:start, :shutdown] => :@server
end
51 changes: 29 additions & 22 deletions spec/support/proxy_server.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
require "support/black_hole"
require "webrick/httpproxy"

handler = proc { |_, res| res["X-PROXIED"] = true }
def with_proxy(port, handler, options = {})
proxy = WEBrick::HTTPProxyServer.new({
:Port => port,
:AccessLog => [],
:ProxyContentHandler => handler,
:Logger => BlackHole
}.merge(options))

ProxyServer = WEBrick::HTTPProxyServer.new(
:Port => 8080,
:AccessLog => [],
:RequestCallback => handler
)
oversee_webrick_server(proxy) { yield proxy }
end

def with_auth_proxy(port, handler, options = {})
username = options.fetch(:user)
password = options.fetch(:password)

AuthenticatedProxyServer = WEBrick::HTTPProxyServer.new(
:Port => 8081,
:ProxyAuthProc => proc do | req, res |
WEBrick::HTTPAuth.proxy_basic_auth(req, res, "proxy") do | user, pass |
user == "username" && pass == "password"
auth_proc = proc do | req, res |
WEBrick::HTTPAuth.proxy_basic_auth(req, res, "proxy") do |user, pass|
user == username && pass == password
end
end,
:RequestCallback => handler
)
end

Thread.new { ProxyServer.start }
trap("INT") do
ProxyServer.shutdown
exit
with_proxy(port, handler, options.merge(:ProxyAuthProc => auth_proc)) { |proxy| yield proxy }
end

Thread.new { AuthenticatedProxyServer.start }
trap("INT") do
AuthenticatedProxyServer.shutdown
exit
def oversee_webrick_server(server)
thread = Thread.new { server.start }
Thread.pass while thread.status != "sleep"

begin
yield server
ensure
server.shutdown
thread.join
end
end