From 06af507acd044be39f9f43dfaeb2486e76d83cc8 Mon Sep 17 00:00:00 2001 From: Oleg Kovalenko Date: Sun, 18 Jan 2015 16:38:17 +0200 Subject: [PATCH 1/4] Use at_exit instead of overriding trap for interrup to shutdown ProxyServer, AuthenticatedProxyServer. Call to trap replaces handler for specified signal, so ProxyServer.shutdown would have never been called. at_exit registers handler for execution when the programm exits, handlers are executed in revers order of registration. --- spec/support/proxy_server.rb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/spec/support/proxy_server.rb b/spec/support/proxy_server.rb index 8700acf9..95214141 100644 --- a/spec/support/proxy_server.rb +++ b/spec/support/proxy_server.rb @@ -18,14 +18,8 @@ :RequestCallback => handler ) -Thread.new { ProxyServer.start } -trap("INT") do - ProxyServer.shutdown - exit -end +Thread.new { ProxyServer.start } +at_exit { ProxyServer.shutdown } -Thread.new { AuthenticatedProxyServer.start } -trap("INT") do - AuthenticatedProxyServer.shutdown - exit -end +Thread.new { AuthenticatedProxyServer.start } +at_exit { AuthenticatedProxyServer.shutdown } From abb207de5ff9c8a1a729741ea46f906683c8838a Mon Sep 17 00:00:00 2001 From: Oleg Kovalenko Date: Mon, 19 Jan 2015 01:46:59 +0200 Subject: [PATCH 2/4] Move testing servers startup/shutdown into rspec before/after suite blocks. --- spec/spec_helper.rb | 18 ++++++++++++++++++ spec/support/example_server.rb | 9 --------- spec/support/proxy_server.rb | 6 ------ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8b5db6fe..9e9fca49 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,6 +21,24 @@ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| + servers = [ + ProxyServer, + AuthenticatedProxyServer, + ExampleServer.instance + ] + threads = [] + + config.before :suite do + threads.push(*servers.map { |server| Thread.new { server.start } }) + # wait until servers fully boot up + Thread.pass while threads.any? { |t| t.status && t.status != "sleep" } + end + + config.after :suite do + servers.each { |server| server.shutdown } + threads.each(&:join).clear + end + 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 diff --git a/spec/support/example_server.rb b/spec/support/example_server.rb index b770bfcb..4f9b6ffd 100644 --- a/spec/support/example_server.rb +++ b/spec/support/example_server.rb @@ -19,12 +19,3 @@ def initialize delegate [:start, :shutdown] => :@server end - -t = Thread.new { ExampleServer.instance.start } - -trap("INT") do - ExampleServer.instance.shutdown - exit -end - -Thread.pass while t.status && t.status != "sleep" diff --git a/spec/support/proxy_server.rb b/spec/support/proxy_server.rb index 95214141..2e5c0b51 100644 --- a/spec/support/proxy_server.rb +++ b/spec/support/proxy_server.rb @@ -17,9 +17,3 @@ end, :RequestCallback => handler ) - -Thread.new { ProxyServer.start } -at_exit { ProxyServer.shutdown } - -Thread.new { AuthenticatedProxyServer.start } -at_exit { AuthenticatedProxyServer.shutdown } From 1e822d50fd0a5f2aa040613867790a3862b83d0e Mon Sep 17 00:00:00 2001 From: Oleg Kovalenko Date: Mon, 19 Jan 2015 18:41:14 +0200 Subject: [PATCH 3/4] Configure rspec to start/shutdown proxies in before/after suite hooks. --- spec/spec_helper.rb | 17 ----------------- spec/support/proxy_server.rb | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3d455cc2..c6c26a0c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,23 +19,6 @@ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| - servers = [ - ProxyServer, - AuthenticatedProxyServer, - ExampleServer.instance - ] - threads = [] - - config.before :suite do - threads.push(*servers.map { |server| Thread.new { server.start } }) - # wait until servers fully boot up - Thread.pass while threads.any? { |t| t.status && t.status != "sleep" } - end - - config.after :suite do - servers.each { |server| server.shutdown } - threads.each(&:join).clear - end config.expect_with :rspec do |expectations| # This option will default to `true` in RSpec 4. It makes the `description` diff --git a/spec/support/proxy_server.rb b/spec/support/proxy_server.rb index 2e5c0b51..2d98feb6 100644 --- a/spec/support/proxy_server.rb +++ b/spec/support/proxy_server.rb @@ -17,3 +17,22 @@ end, :RequestCallback => handler ) + +RSpec.configure do |config| + servers = [ + ProxyServer, + AuthenticatedProxyServer + ] + threads = [] + + config.before :suite do + threads.push(*servers.map { |server| Thread.new { server.start } }) + # wait until servers fully boot up + Thread.pass while threads.any? { |t| t.status && t.status != "sleep" } + end + + config.after :suite do + servers.each { |server| server.shutdown } + threads.each(&:join).clear + end +end From 9aabb025b7b3b392e9e34476a5a1f0fcc99fc495 Mon Sep 17 00:00:00 2001 From: Oleg Kovalenko Date: Tue, 20 Jan 2015 16:28:06 +0200 Subject: [PATCH 4/4] Start and stop proxies inside the tests that depend on them. --- spec/lib/http_spec.rb | 58 ++++++++++++++++++++++-------------- spec/support/proxy_server.rb | 56 +++++++++++++++++----------------- 2 files changed, 64 insertions(+), 50 deletions(-) diff --git a/spec/lib/http_spec.rb b/spec/lib/http_spec.rb index 35e83441..c05a3d59 100644 --- a/spec/lib/http_spec.rb +++ b/spec/lib/http_spec.rb @@ -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(//) + 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(//) + 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 diff --git a/spec/support/proxy_server.rb b/spec/support/proxy_server.rb index 2d98feb6..3b46d1d1 100644 --- a/spec/support/proxy_server.rb +++ b/spec/support/proxy_server.rb @@ -1,38 +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 -RSpec.configure do |config| - servers = [ - ProxyServer, - AuthenticatedProxyServer - ] - threads = [] + with_proxy(port, handler, options.merge(:ProxyAuthProc => auth_proc)) { |proxy| yield proxy } +end - config.before :suite do - threads.push(*servers.map { |server| Thread.new { server.start } }) - # wait until servers fully boot up - Thread.pass while threads.any? { |t| t.status && t.status != "sleep" } - end +def oversee_webrick_server(server) + thread = Thread.new { server.start } + Thread.pass while thread.status != "sleep" - config.after :suite do - servers.each { |server| server.shutdown } - threads.each(&:join).clear + begin + yield server + ensure + server.shutdown + thread.join end end