Skip to content

Instantly share code, notes, and snippets.

@iain
Created February 2, 2012 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iain/1725656 to your computer and use it in GitHub Desktop.
Save iain/1725656 to your computer and use it in GitHub Desktop.
# spec/support/soap_client.rb
class SoapClient
def self.call(env)
new(env).response
end
def initialize(env)
@env = env
@request = Rack::Request.new(env)
end
def body
@request.body.string
end
def response
sleepable || errorable || normal_response
end
def normal_response
Rack::Response.new([body])
end
def sleepable
if body =~ /sleep>(\d+)</
#puts "sleeping #{$1} seconds"
sleep $1.to_i
end
false
end
def errorable
if body =~ /error>true</
Rack::Response.new(["not xml, but a .Net stacktrace, #{body}"], 500)
else
false
end
end
end
# spec/support/soap_server.rb
require 'rack'
require 'net/http'
class SoapServer
HOST = "localhost"
PORT = 13010
def self.instance
@instance ||= new
end
def self.run
instance.run
end
def self.url(extra)
File.join("http://#{HOST}:#{PORT}", extra)
end
def self.stop
instance.stop
end
def run
start unless running?
wait until running?
end
def thread
@thread
end
def start
@thread = Thread.new do
Rack::Handler::Thin.run(SoapClient, :Port => PORT, :AccessLog => []) do |server|
server.threaded = true
end
end
end
def stop
begin
thread.exit
wait while running?
rescue EOFError => error
STDERR.puts "\e[31mWarning, EOFError! #{error.inspect}\e[0m"
STDERR.puts *error.backtrace
end
end
def running?
Timeout::timeout(1) do
begin
s = TCPSocket.new(HOST, PORT)
s.close
return true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
return false
end
end
rescue Timeout::Error
return false
end
def wait
sleep 0.01
end
end
# spec/spec_helper.rb
Dir["spec/support/**/*.rb"].each do |file|
require File.expand_path(file)
end
RSpec.configure do |config|
config.before :suite do
MyLibrary.client.wsdl.endpoint = SoapServer.url("/foo.xml")
SoapServer.run
end
config.after :suite do
SoapServer.stop
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment