Sha256: ae6980382e46c1c596597a7c53fdb392f2189eb3ad5a36703f4e10c5748f3c6d

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

require 'rubygems'
require 'ebb'

Ebb.log = File.open('/dev/null','w')

TEST_PORT = 4044


class HelperApp
  def call(env)
    commands = env['PATH_INFO'].split('/')
    extras = {}

    if commands.include?('delay')
      n = commands.last.to_i
      raise "delay called with n <= 0" if n < 0
      sleep n
      body = "delayed #{n} seconds"
      status = 200

    elsif commands.include?('bytes')
      n = commands.last.to_i
      raise "bytes called with n <= 0" if n <= 0
      body = "C"*n
      status = 200
    elsif commands.include?('redir')
      n = commands.last.to_i
      if n <= 0
        extras = {'Location' => '/bytes/10'}
        body = "redirect to bytes"
      else
        extras = {'Location' => "/redir/#{n-1}"}
        body = "redirect to redir #{n-1}"
      end
      puts body
      status = 302
    elsif commands.include?('test_post_length')
      input_body = env['rack.input'].read
      
      content_length_header = env['HTTP_CONTENT_LENGTH'].to_i
      
      if content_length_header == input_body.length
        body = "Content-Length matches input length"
        status = 200
      else
        body = "Content-Length header is #{content_length_header} but body length is #{input_body.length}"
        status = 500
      end
      
    else
      status = 404
      body = "Undefined url"
    end

    [status, {'Content-Type' => 'text/json'}.merge(extras), body]
  end
end

def start_test_server
  puts "starting up ebb";
  Thread.start{ Ebb.start_server(HelperApp.new, :port => TEST_PORT) }
  sleep 0.1 until Ebb.running?

  Signal.trap( "HUP" ){
    Ebb.stop_server
    sleep 0.1 while Ebb.running?
  }

  # keep process alive
  sleep 0.5 while Ebb.running?
end
if( ARGV[0] == "start" )
  start_test_server
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
evdispatch-0.2.0 ext/revdispatch/server.rb
evdispatch-0.2.2 ext/revdispatch/server.rb
evdispatch-0.2.1 ext/revdispatch/server.rb