ry / ebb fork watch download tarball
public
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
Search Repo:
ryah (author)
Tue Apr 08 07:50:50 -0700 2008
commit  8f16a4f26257e8063b8aa82e6e2c0a11b7eb3f8a
tree    4c60de9a19fdfb349ef60bef7364ae1a483b55b0
parent  330364ac2933ad98948bd1a9089fa06174a13033
ebb / test / helper.rb
100644 137 lines (112 sloc) 3.214 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
 
class ServerTestSocket < ServerTest
  def get(path)
    socket = UNIXSocket.open(@socketfile)
    socket.write("GET #{path} HTTP/1.0\r\n\r\n")
    response = ""
    while chunk = socket.read(100)
      response << chunk
    end
    body = response.split("\r\n\r\n")[1]
    env = JSON.parse(body)
  ensure
    socket.close if socket
  end
  
  def post(path, data)
    socket = UNIXSocket.open(@socketfile)
    socket.write("POST #{path} HTTP/1.0\r\nContent-Length: #{data.length}\r\n\r\n#{data}")
    response = ""
    while chunk = socket.read(100)
      response << chunk
    end
    body = response.split("\r\n\r\n")[1]
    env = JSON.parse(body)
  ensure
    socket.close if socket
  end
  
  def setup
    @socketfile = '/tmp/ebb_unittest.sock'
    Thread.new { Ebb.start_server(HelperApp.new, :unix_socket => @socketfile) }
    sleep 0.1 until Ebb.running?
  end
 
  def teardown
    super
  end
end