Sha256: d75bf45fa16d1208e991c308898d46d4eb4a9cfd26a73e6b714e24590fb2b3a7

Contents?: true

Size: 1.63 KB

Versions: 6

Compression:

Stored size: 1.63 KB

Contents

require 'socket'

class FakeServer

  def initialize(port, options = nil)
    options ||= {}
    @port = port
    @handlers = {}

    @closing_server = !!options[:closing_server]
    @slow = !!options[:slow]
  end

  def add_handler(name, &block)
    @handlers[name] = block
  end

  def run
    server = TCPServer.new("localhost", @port)
    socket = server.accept

    if @closing_server
      sleep 0.1 # ensure the connection isn't closed before a client can run
                # IO.select
      socket.close
    elsif @slow
      sleep 0.5
    else
      serve(socket)
    end

    server.close rescue false
  end

  protected

  def serve(socket)
    connection = Sanford::Protocol::Connection.new(socket)
    request = Sanford::Protocol::Request.parse(connection.read)
    status, result = route(request)
    response = Sanford::Protocol::Response.new(status, result)
    connection.write(response.to_hash)
    connection.close_write
  end

  def route(request)
    handler = @handlers[request.name]
    returned = handler.call(request.params)
  end

  module Helper

    def run_fake_server(server, &block)
      begin
        thread = Thread.new{ server.run }
        yield
      ensure
        begin
          TCPSocket.open("localhost", server.instance_variable_get("@port"))
        rescue Exception
        end
        thread.join
      end
    end

    def start_closing_server(port, &block)
      server = FakeServer.new(port, :closing_server => true)
      run_fake_server(server, &block)
    end

    def start_slow_server(port, &block)
      server = FakeServer.new(port, :slow => true)
      run_fake_server(server, &block)
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
and-son-0.7.0 test/support/fake_server.rb
and-son-0.6.1 test/support/fake_server.rb
and-son-0.6.0 test/support/fake_server.rb
and-son-0.5.0 test/support/fake_server.rb
and-son-0.4.1 test/support/fake_server.rb
and-son-0.4.0 test/support/fake_server.rb