README: update with extra disclaimer
[raindrops.git] / test / test_middleware.rb
blob5694cd4de955d476af3c5667a483d5fe685c9b92
1 # -*- encoding: binary -*-
2 # frozen_string_literal: false
3 require 'test/unit'
4 require 'raindrops'
6 class TestMiddleware < Test::Unit::TestCase
8   def setup
9     @resp_headers = { 'Content-Type' => 'text/plain', 'Content-Length' => '0' }
10     @response = [ 200, @resp_headers, [] ]
11     @app = lambda { |env| @response }
12   end
14   def test_setup
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
20     tmp = []
21     response.last.each { |y| tmp << y }
22     assert tmp.empty?
23   end
25   def test_alt_stats
26     stats = Raindrops::Middleware::Stats.new
27     app = lambda { |env|
28       if (stats.writing == 0 && stats.calling == 1)
29         @app.call(env)
30       else
31         [ 500, @resp_headers, [] ]
32       end
33     }
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)
40     tmp = []
41     response.last.each do |y|
42       assert_equal 1, stats.writing
43       tmp << y
44     end
45     assert tmp.empty?
46   end
48   def test_default_endpoint
49     app = Raindrops::Middleware.new(@app)
50     response = app.call("PATH_INFO" => "/_raindrops")
51     expect = [
52       200,
53       { "Content-Type" => "text/plain", "Content-Length" => "22" },
54       [ "calling: 0\nwriting: 0\n" ]
55     ]
56     assert_equal expect, response
57   end
59   def test_alt_endpoint
60     app = Raindrops::Middleware.new(@app, :path => "/foo")
61     response = app.call("PATH_INFO" => "/foo")
62     expect = [
63       200,
64       { "Content-Type" => "text/plain", "Content-Length" => "22" },
65       [ "calling: 0\nwriting: 0\n" ]
66     ]
67     assert_equal expect, response
68   end
70   def test_concurrent
71     rda, wra = IO.pipe
72     rdb, wrb = IO.pipe
73     app = lambda do |env|
74       wrb.close
75       wra.syswrite('.')
76       wra.close
78       # wait until parent has run app.call for stats endpoint
79       rdb.read
80       @app.call(env)
81     end
82     app = Raindrops::Middleware.new(app)
84     pid = fork { app.call({}) }
85     rdb.close
87     # wait til child is running in app.call
88     assert_equal '.', rda.sysread(1)
89     rda.close
91     response = app.call("PATH_INFO" => "/_raindrops")
92     expect = [
93       200,
94       { "Content-Type" => "text/plain", "Content-Length" => "22" },
95       [ "calling: 1\nwriting: 0\n" ]
96     ]
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")
104     expect = [
105       200,
106       { "Content-Type" => "text/plain", "Content-Length" => "22" },
107       [ "calling: 0\nwriting: 1\n" ]
108     ]
109     assert_equal expect, response
110   end
112   def test_middleware_proxy_to_path_missing
113     app = Raindrops::Middleware.new(@app)
114     response = app.call({})
115     body = response[2]
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
128   end