Use rb_thread_select instead of rb_thread_schedule
[ebb.git] / test / helper.rb
blobeecb37aeb2b5543d6ebaf82c6ab4b747c827337f
1 require 'rubygems'
2 require File.dirname(__FILE__) + '/../ruby_lib/ebb'
3 require 'test/unit'
4 require 'net/http'
5 require 'socket'
6 require 'rubygems'
7 require 'json'
9 include Ebb
11 TEST_PORT = 4044
13 def get(path)
14   Net::HTTP.get_response(URI.parse("http://0.0.0.0:#{TEST_PORT}#{path}"))
15 end
17 def post(path, data)
18   Net::HTTP.post_form(URI.parse("http://0.0.0.0:#{TEST_PORT}#{path}"), data)
19 end
21 class HelperApp
22   def call(env)
23     commands = env['PATH_INFO'].split('/')
24     
25     if commands.include?('bytes')
26       n = commands.last.to_i
27       raise "bytes called with n <= 0" if n <= 0
28       body = "C"*n
29       status = 200
30       
31     elsif commands.include?('test_post_length')
32       input_body = env['rack.input'].read
33       
34       content_length_header = env['HTTP_CONTENT_LENGTH'].to_i
35       
36       if content_length_header == input_body.length
37         body = "Content-Length matches input length"
38         status = 200
39       else
40         body = "Content-Length header is #{content_length_header} but body length is #{input_body.length}"
41         status = 500
42       end
43     
44     else
45       status = 404
46       body = "Undefined url"
47     end
48     
49     [status, {'Content-Type' => 'text/plain'}, body]
50   end
51 end
53 class ServerTest < Test::Unit::TestCase
54   def setup
55     Thread.new { Ebb.start_server(HelperApp.new, :port => TEST_PORT) }
56     sleep 0.1 until Ebb.running?
57   end
58   
59   def teardown
60     Ebb.stop_server
61     sleep 0.1 while Ebb.running?
62   end
63 end