Sha256: 41f78678a42f3c14408345284e0d6d43f3a3bea0f1d8622e5d098f7e412fbfbe

Contents?: true

Size: 991 Bytes

Versions: 14

Compression:

Stored size: 991 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
      alias do_DELETE 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

14 entries across 14 versions & 1 rubygems

Version Path
routemaster-drain-3.7.1 spec/support/server.rb
routemaster-drain-3.7.0 spec/support/server.rb
routemaster-drain-3.6.8 spec/support/server.rb
routemaster-drain-3.6.7 spec/support/server.rb
routemaster-drain-3.6.6 spec/support/server.rb
routemaster-drain-3.6.5 spec/support/server.rb
routemaster-drain-3.6.4 spec/support/server.rb
routemaster-drain-3.6.3 spec/support/server.rb
routemaster-drain-3.6.2 spec/support/server.rb
routemaster-drain-3.6.1 spec/support/server.rb
routemaster-drain-3.6.0 spec/support/server.rb
routemaster-drain-3.5.1 spec/support/server.rb
routemaster-drain-3.5.0 spec/support/server.rb
routemaster-drain-3.4.0 spec/support/server.rb