Sha256: 20a312acb915ed9a683f54fe4b29f5929b7764dae127efb5068afd38431f1a69

Contents?: true

Size: 1.2 KB

Versions: 25

Compression:

Stored size: 1.2 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){ server.shutdown }
      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

25 entries across 25 versions & 3 rubygems

Version Path
solidus_backend-1.0.0.pre3 vendor/bundle/gems/webmock-1.8.11/spec/support/webmock_server.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/webmock-1.8.11/spec/support/webmock_server.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/webmock-1.8.11/spec/support/webmock_server.rb
webmock-1.11.0 spec/support/webmock_server.rb
webmock-1.10.2 spec/support/webmock_server.rb
webmock-1.10.1 spec/support/webmock_server.rb
webmock-1.10.0 spec/support/webmock_server.rb
webmock-1.9.3 spec/support/webmock_server.rb
webmock-1.9.2 spec/support/webmock_server.rb
webmock-1.9.1 spec/support/webmock_server.rb
simple-client-0.0.3 vendor/bundle/ruby/1.9.1/gems/webmock-1.8.8/spec/support/webmock_server.rb
webmock-1.9.0 spec/support/webmock_server.rb
webmock-1.8.11 spec/support/webmock_server.rb
webmock-1.8.10 spec/support/webmock_server.rb
webmock-1.8.9 spec/support/webmock_server.rb
simple-client-0.0.2 vendor/bundle/ruby/1.8/gems/webmock-1.8.8/spec/support/webmock_server.rb
webmock-1.8.8 spec/support/webmock_server.rb
webmock-1.8.7 spec/support/webmock_server.rb
webmock-1.8.6 spec/support/webmock_server.rb
webmock-1.8.5 spec/support/webmock_server.rb