Sha256: 685ec5036602b0a54422c66cf4c1078713072124739fe5607a45c693b6b72118

Contents?: true

Size: 1.65 KB

Versions: 36

Compression:

Stored size: 1.65 KB

Contents

require 'multi_json'
require 'socket'

class FakeWireServer
  def initialize(port, protocol_table)
    @port, @protocol_table = port, protocol_table
    @delays = {}
  end

  def run
    @server = TCPServer.open(@port)
    loop { handle_connections }
  end

  def delay_response(message, delay)
    @delays[message] = delay
  end

  private

  def handle_connections
    Thread.start(@server.accept) { |socket| open_session_on socket }
  end

  def open_session_on(socket)
    begin
      SocketSession.new(socket, @protocol_table, @delays).start
    rescue Exception => e
      raise e
    ensure
      socket.close
    end
  end

  class SocketSession
    def initialize(socket, protocol, delays)
      @socket = socket
      @protocol = protocol
      @delays = delays
    end

    def start
      while message = @socket.gets
        handle(message)
      end
    end

    private

    def handle(data)
      if protocol_entry = response_to(data.strip)
        sleep delay(data)
        send_response(protocol_entry['response'])
      else
        serialized_exception = { :message => "Not understood: #{data}", :backtrace => [] }
        send_response(['fail', serialized_exception ].to_json)
      end
    rescue => e
      send_response(['fail', { :message => e.message, :backtrace => e.backtrace, :exception => e.class } ].to_json)
    end

    def response_to(data)
      @protocol.detect do |entry|
        MultiJson.load(entry['request']) == MultiJson.load(data)
      end
    end

    def send_response(response)
      @socket.puts response + "\n"
    end

    def delay(data)
      message = MultiJson.load(data.strip)[0]
      @delays[message.to_sym] || 0
    end
  end
end

Version data entries

36 entries across 34 versions & 2 rubygems

Version Path
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/cucumber-1.3.16/legacy_features/support/fake_wire_server.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/cucumber-1.3.18/legacy_features/support/fake_wire_server.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/cucumber-1.3.18/legacy_features/support/fake_wire_server.rb
cucumber-1.3.20 legacy_features/support/fake_wire_server.rb
cucumber-1.3.19 legacy_features/support/fake_wire_server.rb
cucumber-2.0.0.rc.4 features/lib/support/fake_wire_server.rb
cucumber-2.0.0.rc.3 features/lib/support/fake_wire_server.rb
cucumber-2.0.0.rc.2 features/lib/support/fake_wire_server.rb
cucumber-2.0.0.rc.1 features/lib/support/fake_wire_server.rb
cucumber-1.3.18 legacy_features/support/fake_wire_server.rb
cucumber-2.0.0.beta.5 features/lib/support/fake_wire_server.rb
cucumber-2.0.0.beta.4 features/lib/support/fake_wire_server.rb
cucumber-2.0.0.beta.3 features/lib/support/fake_wire_server.rb
cucumber-1.3.17 legacy_features/support/fake_wire_server.rb
cucumber-2.0.0.beta.2 features/lib/support/fake_wire_server.rb
cucumber-2.0.0.beta.1 features/lib/support/fake_wire_server.rb
cucumber-1.3.16 legacy_features/support/fake_wire_server.rb
cucumber-1.3.15 legacy_features/support/fake_wire_server.rb
cucumber-1.3.14 legacy_features/support/fake_wire_server.rb
cucumber-1.3.13 legacy_features/support/fake_wire_server.rb