1 # -*- encoding: binary -*-
2 # frozen_string_literal: false
6 class TestMiddleware < Test::Unit::TestCase
9 @resp_headers = { 'Content-Type' => 'text/plain', 'Content-Length' => '0' }
10 @response = [ 200, @resp_headers, [] ]
11 @app = lambda { |env| @response }
15 app = Raindrops::Middleware.new(@app)
16 response = app.call({})
17 assert_equal @response[0,2], response[0,2]
18 assert response.last.kind_of?(Raindrops::Middleware::Proxy)
19 assert response.last.object_id != app.object_id
21 response.last.each { |y| tmp << y }
26 stats = Raindrops::Middleware::Stats.new
28 if (stats.writing == 0 && stats.calling == 1)
31 [ 500, @resp_headers, [] ]
34 app = Raindrops::Middleware.new(app, :stats => stats)
35 response = app.call({})
36 assert_equal 0, stats.calling
37 assert_equal 1, stats.writing
38 assert_equal 200, response[0]
39 assert response.last.kind_of?(Raindrops::Middleware::Proxy)
41 response.last.each do |y|
42 assert_equal 1, stats.writing
48 def test_default_endpoint
49 app = Raindrops::Middleware.new(@app)
50 response = app.call("PATH_INFO" => "/_raindrops")
53 { "Content-Type" => "text/plain", "Content-Length" => "22" },
54 [ "calling: 0\nwriting: 0\n" ]
56 assert_equal expect, response
60 app = Raindrops::Middleware.new(@app, :path => "/foo")
61 response = app.call("PATH_INFO" => "/foo")
64 { "Content-Type" => "text/plain", "Content-Length" => "22" },
65 [ "calling: 0\nwriting: 0\n" ]
67 assert_equal expect, response
78 # wait until parent has run app.call for stats endpoint
82 app = Raindrops::Middleware.new(app)
84 pid = fork { app.call({}) }
87 # wait til child is running in app.call
88 assert_equal '.', rda.sysread(1)
91 response = app.call("PATH_INFO" => "/_raindrops")
94 { "Content-Type" => "text/plain", "Content-Length" => "22" },
95 [ "calling: 1\nwriting: 0\n" ]
97 assert_equal expect, response
98 wrb.close # unblock child process
99 assert Process.waitpid2(pid).last.success?
101 # we didn't call close the body in the forked child, so it'll always be
102 # marked as writing, a real server would close the body
103 response = app.call("PATH_INFO" => "/_raindrops")
106 { "Content-Type" => "text/plain", "Content-Length" => "22" },
107 [ "calling: 0\nwriting: 1\n" ]
109 assert_equal expect, response
112 def test_middleware_proxy_to_path_missing
113 app = Raindrops::Middleware.new(@app)
114 response = app.call({})
116 assert_kind_of Raindrops::Middleware::Proxy, body
117 assert ! body.respond_to?(:to_path)
118 assert body.respond_to?(:close)
119 orig_body = @response[2]
121 def orig_body.to_path; "/dev/null"; end
122 assert body.respond_to?(:to_path)
123 assert_equal "/dev/null", body.to_path
125 def orig_body.body; "this is a body"; end
126 assert body.respond_to?(:body)
127 assert_equal "this is a body", body.body