1 # -*- encoding: binary -*-
5 class TestMiddleware < Test::Unit::TestCase
8 @resp_headers = { 'Content-Type' => 'text/plain', 'Content-Length' => '0' }
9 @response = [ 200, @resp_headers, [] ]
10 @app = lambda { |env| @response }
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
20 response.last.each { |y| tmp << y }
25 stats = Raindrops::Middleware::Stats.new
27 if (stats.writing == 0 && stats.calling == 1)
30 [ 500, @resp_headers, [] ]
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)
40 response.last.each do |y|
41 assert_equal 1, stats.writing
47 def test_default_endpoint
48 app = Raindrops::Middleware.new(@app)
49 response = app.call("PATH_INFO" => "/_raindrops")
52 { "Content-Type" => "text/plain", "Content-Length" => "22" },
53 [ "calling: 0\nwriting: 0\n" ]
55 assert_equal expect, response
59 app = Raindrops::Middleware.new(@app, :path => "/foo")
60 response = app.call("PATH_INFO" => "/foo")
63 { "Content-Type" => "text/plain", "Content-Length" => "22" },
64 [ "calling: 0\nwriting: 0\n" ]
66 assert_equal expect, response
77 # wait until parent has run app.call for stats endpoint
81 app = Raindrops::Middleware.new(app)
83 pid = fork { app.call({}) }
86 # wait til child is running in app.call
87 assert_equal '.', rda.sysread(1)
90 response = app.call("PATH_INFO" => "/_raindrops")
93 { "Content-Type" => "text/plain", "Content-Length" => "22" },
94 [ "calling: 1\nwriting: 0\n" ]
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")
105 { "Content-Type" => "text/plain", "Content-Length" => "22" },
106 [ "calling: 0\nwriting: 1\n" ]
108 assert_equal expect, response
111 def test_middleware_proxy_to_path_missing
112 app = Raindrops::Middleware.new(@app)
113 response = app.call({})
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