2012-11-12 02:01:49 -05:00
|
|
|
require 'socket'
|
2012-11-16 05:34:10 -05:00
|
|
|
require 'net/http'
|
2012-11-12 02:01:49 -05:00
|
|
|
|
|
|
|
# Adapted from code by Jon Leighton
|
|
|
|
# https://github.com/jonleighton/focused_controller/blob/ec7ccf1/test/acceptance/app_test.rb
|
|
|
|
|
|
|
|
class DummyApp
|
|
|
|
|
|
|
|
def initialize(environment)
|
2015-08-26 22:24:34 -04:00
|
|
|
raise ArgumentError, 'Environment must be development or production' unless ['development', 'production', 'test'].include?(environment.to_s)
|
2012-11-12 02:01:49 -05:00
|
|
|
@environment = environment
|
|
|
|
end
|
|
|
|
|
2012-11-16 05:34:10 -05:00
|
|
|
attr_reader :environment
|
|
|
|
|
2012-11-12 02:01:49 -05:00
|
|
|
def url
|
|
|
|
"http://#{localhost}:#{port}"
|
|
|
|
end
|
|
|
|
|
2012-11-16 05:34:10 -05:00
|
|
|
def get(path)
|
|
|
|
Net::HTTP.get(URI(url + path))
|
|
|
|
end
|
|
|
|
|
2012-11-12 02:01:49 -05:00
|
|
|
def within_app(&block)
|
|
|
|
Dir.chdir(root, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_server
|
|
|
|
within_app do
|
|
|
|
IO.popen("bundle exec rails s -e #{@environment} -p #{port} 2>&1") do |out|
|
|
|
|
start = Time.now
|
|
|
|
started = false
|
|
|
|
output = ""
|
|
|
|
timeout = 60.0
|
|
|
|
|
|
|
|
while !started && !out.eof? && Time.now - start <= timeout
|
|
|
|
output << read_output(out)
|
|
|
|
sleep 0.1
|
|
|
|
|
|
|
|
begin
|
|
|
|
TCPSocket.new(localhost, port)
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
else
|
|
|
|
started = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
raise "Server failed to start:\n#{output}" unless started
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
2013-01-07 07:26:51 -05:00
|
|
|
Process.kill("KILL", out.pid)
|
|
|
|
File.delete("tmp/pids/server.pid")
|
2012-11-12 02:01:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def root
|
2013-01-07 07:26:51 -05:00
|
|
|
File.expand_path("../../dummy", __FILE__)
|
2012-11-12 02:01:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def localhost
|
2013-01-07 07:26:51 -05:00
|
|
|
"127.0.0.1"
|
2012-11-12 02:01:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def port
|
|
|
|
@port ||= begin
|
|
|
|
server = TCPServer.new(localhost, 0)
|
2012-11-16 05:34:10 -05:00
|
|
|
server.addr[1]
|
2012-11-12 02:01:49 -05:00
|
|
|
ensure
|
|
|
|
server.close if server
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_output(stream)
|
|
|
|
read = IO.select([stream], [], [stream], 0.1)
|
|
|
|
output = ""
|
|
|
|
loop { output << stream.read_nonblock(1024) } if read
|
|
|
|
output
|
|
|
|
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, EOFError
|
|
|
|
output
|
|
|
|
end
|
|
|
|
end
|