Sha256: c46b70f26909a060959a244a28bb5e95254d25be4d2259726a3dd99fce67eb2a

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

require 'rubygems'
require 'rack'

TEST_PORT = 4044

$req_count = 0

class TestApp
  
  def initialize
    instance_eval do
      if $daemonized
        def log(msg) ; end
      else
        def log(msg) ; puts msg ; end
      end
    end
    log "Test server '#{__FILE__}', started"
  end

  def call(env)
    commands = env['PATH_INFO'].split('/')
    extras = {}
    $req_count += 1
    log "#{$req_count} requests"

    if commands.include?('delay')
      n = commands.last.to_f
      raise "delay called with n <= 0" if n < 0.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
      log body
      status = 302
    elsif commands.include?('test_post_length')
      input_body = env['rack.input'].read
      
      content_length_header = env['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
if $0 == __FILE__
  # ebb's much better at handling concurrent requests use it if it's installed
  begin
    require 'ebb'
    if ARGV[0] == '-d'
      require 'daemons'
      $daemonized = true
      Daemons.daemonize
    end
    Ebb.start_server(TestApp.new, :port => TEST_PORT)
  rescue LoadError => e
    Rack::Handler::Mongrel.run(TestApp.new, :Port => TEST_PORT )
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
evdispatch-0.4.2 ext/revdispatch/server.rb