-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathspec_events.rb
137 lines (120 loc) · 3.58 KB
/
spec_events.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
# frozen_string_literal: true
require_relative 'helper'
separate_testing do
require_relative '../lib/rack/events'
end
module Rack
class TestEvents < Minitest::Test
class EventMiddleware
attr_reader :events
def initialize(events)
@events = events
end
def on_start(req, res)
events << [self, __method__]
end
def on_commit(req, res)
events << [self, __method__]
end
def on_send(req, res)
events << [self, __method__]
end
def on_finish(req, res)
events << [self, __method__]
end
def on_error(req, res, e)
events << [self, __method__]
end
end
def test_events_fire
events = []
ret = [200, {}, []]
app = lambda { |env| events << [app, :call]; ret }
se = EventMiddleware.new events
e = Events.new app, [se]
triple = e.call({})
response_body = []
triple[2].each { |x| response_body << x }
triple[2].close
triple[2] = response_body
assert_equal ret, triple
assert_equal [[se, :on_start],
[app, :call],
[se, :on_commit],
[se, :on_send],
[se, :on_finish],
], events
end
def test_send_and_finish_are_not_run_until_body_is_sent
events = []
ret = [200, {}, []]
app = lambda { |env| events << [app, :call]; ret }
se = EventMiddleware.new events
e = Events.new app, [se]
e.call({})
assert_equal [[se, :on_start],
[app, :call],
[se, :on_commit],
], events
end
def test_send_is_called_on_each
events = []
ret = [200, {}, []]
app = lambda { |env| events << [app, :call]; ret }
se = EventMiddleware.new events
e = Events.new app, [se]
triple = e.call({})
triple[2].each { |x| }
assert_equal [[se, :on_start],
[app, :call],
[se, :on_commit],
[se, :on_send],
], events
end
def test_finish_is_called_on_close
events = []
ret = [200, {}, []]
app = lambda { |env| events << [app, :call]; ret }
se = EventMiddleware.new events
e = Events.new app, [se]
triple = e.call({})
triple[2].each { |x| }
triple[2].close
assert_equal [[se, :on_start],
[app, :call],
[se, :on_commit],
[se, :on_send],
[se, :on_finish],
], events
end
def test_finish_is_called_in_reverse_order
events = []
ret = [200, {}, []]
app = lambda { |env| events << [app, :call]; ret }
se1 = EventMiddleware.new events
se2 = EventMiddleware.new events
se3 = EventMiddleware.new events
e = Events.new app, [se1, se2, se3]
triple = e.call({})
triple[2].each { |x| }
triple[2].close
groups = events.group_by { |x| x.last }
assert_equal groups[:on_start].map(&:first), groups[:on_finish].map(&:first).reverse
assert_equal groups[:on_commit].map(&:first), groups[:on_finish].map(&:first)
assert_equal groups[:on_send].map(&:first), groups[:on_finish].map(&:first)
end
def test_finish_is_called_if_there_is_an_exception
events = []
app = lambda { |env| raise }
se = EventMiddleware.new events
e = Events.new app, [se]
assert_raises(RuntimeError) do
e.call({})
end
assert_equal [[se, :on_start],
[se, :on_error],
[se, :on_finish],
], events
end
end
end