Merge /Users/ry/gnosek_ebb
[ebb.git] / test / helper.rb
blob05998c26bbcbe2a5a6c801c6216ad130fc78e3fe
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'
10 Ebb.log = File.open('/dev/null','w')
12 TEST_PORT = 4044
15 class HelperApp
16   def call(env)
17     commands = env['PATH_INFO'].split('/')
18     
19     if commands.include?('bytes')
20       n = commands.last.to_i
21       raise "bytes called with n <= 0" if n <= 0
22       body = "C"*n
23       status = 200
24       
25     elsif commands.include?('test_post_length')
26       input_body = env['rack.input'].read
27       
28       content_length_header = env['CONTENT_LENGTH'].to_i
29       
30       if content_length_header == input_body.length
31         body = "Content-Length matches input length"
32         status = 200
33       else
34         body = "Content-Length header is #{content_length_header} but body length is #{input_body.length}"
35         status = 500
36       end
37       
38     else
39       status = 404
40       body = "Undefined url"
41     end
42     
43     env['rack.input'] = env['rack.input'].read
44     env.delete('rack.errors')
45     env['output'] = body
46     env['status'] = status
47     
48     [status, {'Content-Type' => 'text/json'}, env.to_json]
49   end
50 end
52 class Test::Unit::TestCase
53   def get(path)
54     response = Net::HTTP.get_response(URI.parse("http://0.0.0.0:#{TEST_PORT}#{path}"))
55   end
56   
57   def post(path, data)
58     response = Net::HTTP.post_form(URI.parse("http://0.0.0.0:#{TEST_PORT}#{path}"), data)
59   end
60 end
62 class ServerTest < Test::Unit::TestCase
63   def get(path)
64     response = Net::HTTP.get_response(URI.parse("http://0.0.0.0:#{TEST_PORT}#{path}"))
65     env = JSON.parse(response.body)
66   end
68   def post(path, data)
69     response = Net::HTTP.post_form(URI.parse("http://0.0.0.0:#{TEST_PORT}#{path}"), data)
70     env = JSON.parse(response.body)
71   end
72   
73   def setup
74     Thread.new { Ebb.start_server(HelperApp.new, :port => TEST_PORT) }
75     sleep 0.1 until Ebb.running?
76   end
77   
78   def teardown
79     Ebb.stop_server
80     sleep 0.1 while Ebb.running?
81   end
82   
83   def default_test
84     assert true
85   end
86 end
88 class ServerTestFD < ServerTest
89   def setup
90     @tcp_server = TCPServer.new('0.0.0.0', TEST_PORT);
91     Thread.new { Ebb.start_server(HelperApp.new, :fileno => @tcp_server.fileno) }
92     sleep 0.1 until Ebb.running?
93   end
95   def teardown
96     super
97     @tcp_server = nil
98   end
99 end