Sha256: e18045efcf8cae82f4531fb0ce2a0877d0d10fa349b15bb0522495a7623112b4

Contents?: true

Size: 1.26 KB

Versions: 34

Compression:

Stored size: 1.26 KB

Contents

require 'webrick'
require 'logger'
require 'singleton'

class WebMockServer
  include Singleton

  attr_reader :port, :started

  def host_with_port
    "localhost:#{port}"
  end

  def concurrent
    unless RUBY_PLATFORM =~ /java/
      @pid = Process.fork do
        yield
      end
    else
      Thread.new { yield }
    end
  end

  def start
    @started = true
    server = WEBrick::GenericServer.new(:Port => 0, :Logger => Logger.new("/dev/null"))
    server.logger.level = 0
    @port = server.config[:Port]

    concurrent do
      ['TERM', 'INT'].each do |signal|
        trap(signal) do
          Thread.new do
            server.shutdown
          end
        end
      end
      server.start do |socket|
        socket.puts <<-EOT.gsub(/^\s+\|/, '')
          |HTTP/1.1 200 OK\r
          |Date: Fri, 31 Dec 1999 23:59:59 GMT\r
          |Content-Type: text/html\r
          |Content-Length: 11\r
          |Set-Cookie: bar\r
          |Set-Cookie: foo\r
          |\r
          |hello world
        EOT
      end
    end


    loop do
      begin
        s = TCPSocket.new("localhost", port)
        sleep 0.1
        break
      rescue Errno::ECONNREFUSED
        sleep 0.1
      end
    end
  end

  def stop
    if @pid
      Process.kill('INT', @pid)
    end
  end
end

Version data entries

34 entries across 32 versions & 5 rubygems

Version Path
logstash-filter-zabbix-0.1.2 vendor/bundle/jruby/1.9/gems/webmock-1.21.0/spec/support/webmock_server.rb
logstash-filter-zabbix-0.1.1 vendor/bundle/jruby/1.9/gems/webmock-1.21.0/spec/support/webmock_server.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/webmock-1.20.4/spec/support/webmock_server.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/webmock-1.20.4/spec/support/webmock_server.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/webmock-1.20.4/spec/support/webmock_server.rb
dwolla_swagger-1.0.6 vendor/bundle/ruby/2.2.0/gems/webmock-1.22.3/spec/support/webmock_server.rb
webmock-1.22.3 spec/support/webmock_server.rb
webmock-1.22.2 spec/support/webmock_server.rb
webmock-1.22.1 spec/support/webmock_server.rb
webmock-1.21.0 spec/support/webmock_server.rb
webmock-1.20.4 spec/support/webmock_server.rb
webmock-1.20.3 spec/support/webmock_server.rb
webmock-1.20.2 spec/support/webmock_server.rb
webmock-1.20.1 spec/support/webmock_server.rb
webmock-1.20.0 spec/support/webmock_server.rb
webmock-1.19.0 spec/support/webmock_server.rb
whos_dated_who-0.1.0 vendor/bundle/gems/webmock-1.18.0/spec/support/webmock_server.rb
whos_dated_who-0.0.1 vendor/bundle/gems/webmock-1.18.0/spec/support/webmock_server.rb
webmock-1.18.0 spec/support/webmock_server.rb
webmock-1.17.4 spec/support/webmock_server.rb