Sha256: af7508c056d763ab968989f4e36b2dbca100efb79285779d2aa2ad0569d86cdb

Contents?: true

Size: 1.27 KB

Versions: 57

Compression:

Stored size: 1.27 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.read(1)
        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
        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

57 entries across 57 versions & 4 rubygems

Version Path
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/webmock-3.18.1/spec/support/webmock_server.rb
webmock-3.18.1 spec/support/webmock_server.rb
webmock-3.18.0 spec/support/webmock_server.rb
webmock-3.15.2 spec/support/webmock_server.rb
webmock-3.16.2 spec/support/webmock_server.rb
webmock-3.16.1 spec/support/webmock_server.rb
webmock-3.15.1 spec/support/webmock_server.rb
webmock-3.17.1 spec/support/webmock_server.rb
webmock-3.17.0 spec/support/webmock_server.rb
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/support/webmock_server.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/support/webmock_server.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/support/webmock_server.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/support/webmock_server.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/support/webmock_server.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/support/webmock_server.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/support/webmock_server.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/support/webmock_server.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/support/webmock_server.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/support/webmock_server.rb
webmock-3.14.0 spec/support/webmock_server.rb