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