Sha256: 3d75f28b5fa794c6938f1dcb442c38ca68860a4167f37a706fc78dbaba748706

Contents?: true

Size: 1.25 KB

Versions: 12

Compression:

Stored size: 1.25 KB

Contents

require 'socket'
require 'json'

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

  def run
    @server = TCPServer.open(@port)
    loop { handle_connections }
  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).start
    rescue Exception => e
      raise e
    ensure
      socket.close
    end
  end
  
  class SocketSession
    def initialize(socket, protocol)
      @socket = socket
      @protocol = protocol
    end

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

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

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

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

Version data entries

12 entries across 12 versions & 4 rubygems

Version Path
cucumber-0.5.1 features/support/fake_wire_server.rb
cucumber-0.5.0 features/support/fake_wire_server.rb
cucumber-0.4.5.rc2 features/support/fake_wire_server.rb
kbaum-cucumber-0.4.5.pre features/support/fake_wire_server.rb
cucumber-0.4.5.rc1 features/support/fake_wire_server.rb
middleman-0.10.17 vendor/gems/gems/cucumber-0.4.4/features/support/fake_wire_server.rb
middleman-0.10.16 vendor/gems/gems/cucumber-0.4.4/features/support/fake_wire_server.rb
middleman-0.10.15 vendor/gems/gems/cucumber-0.4.4/features/support/fake_wire_server.rb
rackjour-0.1.8 vendor/gems/gems/cucumber-0.4.4/features/support/fake_wire_server.rb
middleman-0.10.14 vendor/gems/gems/cucumber-0.4.4/features/support/fake_wire_server.rb
cucumber-0.4.4 features/support/fake_wire_server.rb
cucumber-0.4.3 features/support/fake_wire_server.rb