forked from soveran/cuba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathon.rb
157 lines (120 loc) · 2.68 KB
/
on.rb
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require File.expand_path("helper", File.dirname(__FILE__))
test "executes on true" do
Cuba.define do
on true do
res.write "+1"
end
end
_, _, resp = Cuba.call({})
assert_response resp, ["+1"]
end
test "executes on non-false" do
Cuba.define do
on "123" do
res.write "+1"
end
end
_, _, resp = Cuba.call({ "PATH_INFO" => "/123", "SCRIPT_NAME" => "/" })
assert_response resp, ["+1"]
end
test "ensures SCRIPT_NAME and PATH_INFO are reverted" do
Cuba.define do
on lambda { env["SCRIPT_NAME"] = "/hello"; false } do
res.write "Unreachable"
end
end
env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/hello" }
_, _, resp = Cuba.call(env)
assert_equal "/", env["SCRIPT_NAME"]
assert_equal "/hello", env["PATH_INFO"]
assert_response resp, []
end
test "skips consecutive matches" do
Cuba.define do
on true do
env["foo"] = "foo"
res.write "foo"
end
on true do
env["bar"] = "bar"
res.write "bar"
end
end
env = {}
_, _, resp = Cuba.call(env)
assert_equal "foo", env["foo"]
assert_response resp, ["foo"]
assert ! env["bar"]
end
test "finds first match available" do
Cuba.define do
on false do
res.write "foo"
end
on true do
res.write "bar"
end
end
_, _, resp = Cuba.call({})
assert_response resp, ["bar"]
end
test "reverts a half-met matcher" do
Cuba.define do
on "post", false do
res.write "Should be unmet"
end
end
env = { "PATH_INFO" => "/post", "SCRIPT_NAME" => "/" }
_, _, resp = Cuba.call(env)
assert_response resp, []
assert_equal "/post", env["PATH_INFO"]
assert_equal "/", env["SCRIPT_NAME"]
end
test "responds 404 if conditions are not met" do
Cuba.define do
on root do
res.write("Should be unmet")
end
end
env = { "PATH_INFO" => "/notexists", "SCRIPT_NAME" => "/" }
status, _, body = Cuba.call(env)
assert_equal 404, status
assert body.empty?
end
test "responds 404 if nested conditions are not met" do
Cuba.define do
on get do
on root do
res.write("Should be unmet")
end
end
on default do
res.write("Should be unmet")
end
end
env = {
"REQUEST_METHOD" => "GET",
"PATH_INFO" => "/notexists",
"SCRIPT_NAME" => "/"
}
status, _, body = Cuba.call(env)
assert_equal 404, status
assert body.empty?
end
test "responds 200 even with an empty body if status is set" do
Cuba.define do
on get do
on root do
res.status = 200
end
end
end
env = {
"REQUEST_METHOD" => "GET",
"PATH_INFO" => "/",
"SCRIPT_NAME" => "/"
}
status, _, body = Cuba.call(env)
assert_equal 200, status
assert body.empty?
end