ry / ebb fork watch download tarball
public
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
ryah (author)
Mon Apr 07 09:52:20 -0700 2008
commit  f16270d317fca555c68a294db72a0db1d4767dee
tree    8f42dfae9f83549ad87b236c4d0df1d83f974ef7
parent  7393e3658c5b1bc422047df6bf40e0f08568af37
ebb / test / helper.rb
100644 101 lines (78 sloc) 2.308 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require 'rubygems'
require File.dirname(__FILE__) + '/../ruby_lib/ebb'
require 'test/unit'
require 'net/http'
require 'socket'
require 'rubygems'
require 'json'
 
 
Ebb.log = File.open('/dev/null','w')
 
TEST_PORT = 4044
 
 
class HelperApp
  def call(env)
    commands = env['PATH_INFO'].split('/')
    
    if 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?('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
    
    env['rack.input'] = env['rack.input'].read
    env.delete('rack.errors')
    env['output'] = body
    env['status'] = status
    
    [status, {'Content-Type' => 'text/json'}, env.to_json]
  end
end
 
class Test::Unit::TestCase
  def get(path)
    response = Net::HTTP.get_response(URI.parse("http://0.0.0.0:#{TEST_PORT}#{path}"))
  end
  
  def post(path, data)
    response = Net::HTTP.post_form(URI.parse("http://0.0.0.0:#{TEST_PORT}#{path}"), data)
  end
end
 
class ServerTest < Test::Unit::TestCase
  def get(path)
    response = Net::HTTP.get_response(URI.parse("http://0.0.0.0:#{TEST_PORT}#{path}"))
    env = JSON.parse(response.body)
  end
 
  def post(path, data)
    response = Net::HTTP.post_form(URI.parse("http://0.0.0.0:#{TEST_PORT}#{path}"), data)
    env = JSON.parse(response.body)
  end
  
  def setup
    Thread.new { Ebb.start_server(HelperApp.new, :port => TEST_PORT) }
    sleep 0.1 until Ebb.running?
  end
  
  def teardown
    Ebb.stop_server
    sleep 0.1 while Ebb.running?
  end
  
  def default_test
    assert true
  end
end
 
class ServerTestFD < ServerTest
  def setup
    @tcp_server = TCPServer.new('0.0.0.0', TEST_PORT);
    Thread.new { Ebb.start_server(HelperApp.new, :fileno => @tcp_server.fileno) }
    sleep 0.1 until Ebb.running?
  end
 
  def teardown
    super
    @tcp_server = nil
  end
end