Skip to content

Commit

Permalink
Refactor initializing of proxy servers in specs
Browse files Browse the repository at this point in the history
Closes #176
  • Loading branch information
ixti committed Jan 20, 2015
1 parent 7a8525a commit 0c8f268
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 76 deletions.
50 changes: 26 additions & 24 deletions spec/lib/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,38 @@
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"
describe ".via" do
run_server(:proxy) { ProxyServer.new }
run_server(:auth_proxy) { AuthProxyServer.new }

context "with http proxy address and port" do
it "proxies the request" do
response = HTTP.via(proxy.addr, proxy.port).get dummy.endpoint
expect(response.headers["X-Proxied"]).to eq "true"
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"
context "without proxy port" do
it "raises an argument error" do
expect { HTTP.via(proxy.addr) }.to raise_error HTTP::RequestError
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>/)
end
end
context "with http proxy address, port username and password" do
it "proxies the request" do
response = HTTP.via(auth_proxy.addr, auth_proxy.port, "username", "password").get dummy.endpoint
expect(response.headers["X-Proxied"]).to eq "true"
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)
end
end
it "responds with the endpoint's body" do
response = HTTP.via(auth_proxy.addr, auth_proxy.port, "username", "password").get dummy.endpoint
expect(response.to_s).to match(/<!doctype html>/)
end

context "without proxy port" do
it "raises an argument error" do
expect { HTTP.via("127.0.0.1") }.to raise_error HTTP::RequestError
it "responds with 407 when wrong proxy credentials given" do
response = HTTP.via(auth_proxy.addr, auth_proxy.port, "user", "pass").get dummy.endpoint
expect(response.status).to eq(407)
end
end
end

Expand Down Expand Up @@ -97,7 +100,6 @@
response = HTTP.with_follow(true).get("#{dummy.endpoint}/redirect-302")
expect(response.to_s).to match(/<!doctype html>/)
end

end

context "head requests" do
Expand Down
16 changes: 7 additions & 9 deletions spec/support/dummy_server.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require "webrick"
require "forwardable"

require "support/black_hole"
require "support/dummy_server/servlet"
require "support/helpers/server_runner"
require "support/servers/config"
require "support/servers/runner"

class DummyServer
extend Forwardable
class DummyServer < WEBrick::HTTPServer
include ServerConfig

CONFIG = {
:BindAddress => "127.0.0.1",
Expand All @@ -16,13 +16,11 @@ class DummyServer
}.freeze

def initialize
@server = WEBrick::HTTPServer.new CONFIG
@server.mount("/", Servlet)
super CONFIG
mount("/", Servlet)
end

def endpoint
"http://#{@server.config[:BindAddress]}:#{@server.config[:Port]}"
"http://#{addr}:#{port}"
end

def_delegators :@server, :start, :shutdown
end
2 changes: 1 addition & 1 deletion spec/support/dummy_server/servlet.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class DummyServer
class DummyServer < WEBrick::HTTPServer
class Servlet < WEBrick::HTTPServlet::AbstractServlet
def not_found(_req, res)
res.status = 404
Expand Down
17 changes: 0 additions & 17 deletions spec/support/helpers/server_runner.rb

This file was deleted.

56 changes: 31 additions & 25 deletions spec/support/proxy_server.rb
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
9 changes: 9 additions & 0 deletions spec/support/servers/config.rb
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
13 changes: 13 additions & 0 deletions spec/support/servers/runner.rb
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 }

0 comments on commit 0c8f268

Please sign in to comment.