Sha256: c6fa349e9023476928030971bd7f1bf9aa04d2276eebe8b2e3bd27eebe9f1003

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# encoding: utf-8

class SpecHandler
  def serve(request, served)
    served.fulfill(response_for(request))
  end

  private

  def response_for(request)
    case request.uri
    when '/sleep'     then sleep_response
    when '/close'     then close_response
    when '/streaming' then streaming_response
    else                   echo_response(request)
    end
  end

  def echo_response(request)
    Hatetepe::Response.new(200, { 'Content-Type' => 'text/plain' },
                           request.uri)
  end

  def sleep_response
    SpecHelper.async_sleep(0.01)
    Hatetepe::Response.new(200, { 'Content-Type' => 'text/plain' }, '/sleep')
  end

  def close_response
    Hatetepe::Response.new(200,
                           { 'Content-Type' => 'text/plain',
                             'Connection' => 'close' },
                           '/close')
  end

  def streaming_response
    response = Hatetepe::Response.new(200, { 'Content-Type' => 'text/plain' },
                                      Hatetepe::Body.new)
    SpecHelper.defer do
      %w[foo bar baz].each do |chunk|
        SpecHelper.tick(2)
        response.body.closed.progress(chunk)
      end
      response.close
    end
    response
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hatetepe-0.6.0.pre.2 spec/support/handler.rb