Sha256: 7c430951179c812b90da7a4a7dcb615888edc2ca91fcd12f0ce219e65e894d76

Contents?: true

Size: 964 Bytes

Versions: 4

Compression:

Stored size: 964 Bytes

Contents

require 'spec_helper'
require 'timeout'
require 'webrick'

# Patch WEBrick as it doesn't support PATCH (?)
module WEBrick
  module HTTPServlet
    class ProcHandler
      alias do_PATCH do_GET
    end
  end
end

class TestServer
  def initialize(port, &block)
    @port = port
    @setup = block
  end

  def start
    return if @pid
    @pid = fork do
      WEBrick::HTTPServer.new(
        Port: @port,
        DocumentRoot: Dir.pwd,
        Logger: WEBrick::Log.new('/dev/null'),
        AccessLog: [nil, nil]
      ).tap do |server|  
        @setup.call(server)
        server.start
      end
    end

    # wait until the child server is up
    Timeout.timeout(5) do
      loop do
        begin
          TCPSocket.new('127.0.0.1', @port)
        rescue Errno::ECONNREFUSED
          sleep 0.1
          next
        end
        break
      end
    end
  end

  def stop
    return unless @pid
    Process.kill('KILL', @pid)
    Process.wait(@pid)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
routemaster-drain-3.3.0 spec/support/server.rb
routemaster-drain-3.2.0 spec/support/server.rb
routemaster-drain-3.1.0 spec/support/server.rb
routemaster-drain-3.0.3 spec/support/server.rb