Sha256: 77d91351b8c6a1ac0570204a7f383ab2d28f47eec16d27735227de331b4d5fea

Contents?: true

Size: 891 Bytes

Versions: 2

Compression:

Stored size: 891 Bytes

Contents

class StubServer
  module Server
    def receive_data(data)
      if echo?
        send_data("HTTP/1.0 200 OK\r\nContent-Length: #{data.bytesize}\r\nContent-Type: text/plain\r\n\r\n")
        send_data(data)
      else
        send_data @response
      end

      close_connection_after_writing
    end

    def echo= flag
      @echo = flag
    end

    def echo?
      !!@echo
    end

    def response=(response)
      @response = response
    end
  end

  def initialize options = {}
    options = {:response => options} if options.kind_of?(String)
    options = {:port     => 8081, :host => '127.0.0.1'}.merge(options)

    host = options[:host]
    port = options[:port]
    @sig = ZMachine::start_server(host, port, Server) do |server|
      server.response = options[:response]
      server.echo     = options[:echo]
    end
  end

  def stop
    ZMachine.stop_server @sig
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
z-http-request-0.2.0 spec/stub_server.rb
z-http-request-0.1.0 spec/stub_server.rb