Take the 2008 Git User's Survey and help out! [ hide ]

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)
Mon Apr 07 14:10:15 -0700 2008
commit  8d410a265216bc390fe079ef5655ec33d7970d56
tree    10da0f7b45be6880ea45fce348830b3dbac1d789
parent  590255007398ef5bc9bbaa0e5697dde73239f245
ebb / test / helper.rb
100644 123 lines (99 sloc) 2.817 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
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")
    response = ""
    while chunk = socket.recv(100)
      response << chunk
    end
    env = JSON.parse(response)
  ensure
    socket.close if socket
  end
  
  def setup
    @socketfile = '/tmp/ebb_socket.sock'
    Thread.new { Ebb.start_server(HelperApp.new, :unix_socket => @socketfile) }
    sleep 0.1 until Ebb.running?
  end
 
  def teardown
    super
  end
end