Add method_missing to Raindrops::Middleware::Proxy
[raindrops.git] / test / test_middleware.rb
blob56ce3461739d4616de5d9c86cbc9760c36683354
1 # -*- encoding: binary -*-
2 require 'test/unit'
3 require 'raindrops'
5 class TestMiddleware < Test::Unit::TestCase
7   def setup
8     @resp_headers = { 'Content-Type' => 'text/plain', 'Content-Length' => '0' }
9     @response = [ 200, @resp_headers, [] ]
10     @app = lambda { |env| @response }
11   end
13   def test_setup
14     app = Raindrops::Middleware.new(@app)
15     response = app.call({})
16     assert_equal @response[0,2], response[0,2]
17     assert response.last.kind_of?(Raindrops::Middleware::Proxy)
18     assert response.last.object_id != app.object_id
19     tmp = []
20     response.last.each { |y| tmp << y }
21     assert tmp.empty?
22   end
24   def test_alt_stats
25     stats = Raindrops::Middleware::Stats.new
26     app = lambda { |env|
27       if (stats.writing == 0 && stats.calling == 1)
28         @app.call(env)
29       else
30         [ 500, @resp_headers, [] ]
31       end
32     }
33     app = Raindrops::Middleware.new(app, :stats => stats)
34     response = app.call({})
35     assert_equal 0, stats.calling
36     assert_equal 1, stats.writing
37     assert_equal 200, response[0]
38     assert response.last.kind_of?(Raindrops::Middleware::Proxy)
39     tmp = []
40     response.last.each do |y|
41       assert_equal 1, stats.writing
42       tmp << y
43     end
44     assert tmp.empty?
45   end
47   def test_default_endpoint
48     app = Raindrops::Middleware.new(@app)
49     response = app.call("PATH_INFO" => "/_raindrops")
50     expect = [
51       200,
52       { "Content-Type" => "text/plain", "Content-Length" => "22" },
53       [ "calling: 0\nwriting: 0\n" ]
54     ]
55     assert_equal expect, response
56   end
58   def test_alt_endpoint
59     app = Raindrops::Middleware.new(@app, :path => "/foo")
60     response = app.call("PATH_INFO" => "/foo")
61     expect = [
62       200,
63       { "Content-Type" => "text/plain", "Content-Length" => "22" },
64       [ "calling: 0\nwriting: 0\n" ]
65     ]
66     assert_equal expect, response
67   end
69   def test_concurrent
70     rda, wra = IO.pipe
71     rdb, wrb = IO.pipe
72     app = lambda do |env|
73       wrb.close
74       wra.syswrite('.')
75       wra.close
77       # wait until parent has run app.call for stats endpoint
78       rdb.read
79       @app.call(env)
80     end
81     app = Raindrops::Middleware.new(app)
83     pid = fork { app.call({}) }
84     rdb.close
86     # wait til child is running in app.call
87     assert_equal '.', rda.sysread(1)
88     rda.close
90     response = app.call("PATH_INFO" => "/_raindrops")
91     expect = [
92       200,
93       { "Content-Type" => "text/plain", "Content-Length" => "22" },
94       [ "calling: 1\nwriting: 0\n" ]
95     ]
96     assert_equal expect, response
97     wrb.close # unblock child process
98     assert Process.waitpid2(pid).last.success?
100     # we didn't call close the body in the forked child, so it'll always be
101     # marked as writing, a real server would close the body
102     response = app.call("PATH_INFO" => "/_raindrops")
103     expect = [
104       200,
105       { "Content-Type" => "text/plain", "Content-Length" => "22" },
106       [ "calling: 0\nwriting: 1\n" ]
107     ]
108     assert_equal expect, response
109   end
111   def test_middleware_proxy_to_path_missing
112     app = Raindrops::Middleware.new(@app)
113     response = app.call({})
114     body = response[2]
115     assert_kind_of Raindrops::Middleware::Proxy, body
116     assert ! body.respond_to?(:to_path)
117     assert body.respond_to?(:close)
118     orig_body = @response[2]
120     def orig_body.to_path; "/dev/null"; end
121     assert body.respond_to?(:to_path)
122     assert_equal "/dev/null", body.to_path
124     def orig_body.body; "this is a body"; end
125     assert body.respond_to?(:body)
126     assert_equal "this is a body", body.body
127   end