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