-
Notifications
You must be signed in to change notification settings - Fork 42
/
test_async.rb
336 lines (274 loc) · 6.75 KB
/
test_async.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
require 'minitest/autorun'
require 'eventmachine'
require "sinatra/async/test"
class CallbackCounter
@@count = 0
def self.count
@@count
end
def initialize(app)
@app = app
end
def call(env)
callback = env['async.callback']
env['async.callback'] = lambda do |response|
@@count += 1
callback.call(response)
end
@app.call(env)
end
end
class TestSinatraAsync < MiniTest::Unit::TestCase
include Sinatra::Async::Test::Methods
class TestApp < Sinatra::Base
set :environment, :test
set :call_count, 0
register Sinatra::Async
# Hack for storing some global data accessible in tests (normally you
# shouldn't need to do this!)
def self.singletons
@singletons ||= []
end
error 401 do
'401'
end
aget '/hello' do
body { 'hello async' }
end
aget '/world' do
body 'world'
end
aget '/em' do
EM.add_timer(0.001) { body { 'em' }; EM.stop }
end
aget '/em_timeout' do
# never send a response
end
aget '/404' do
not_found
end
aget '/302' do
ahalt 302
end
aget '/em_halt' do
em_async_schedule { ahalt 404 }
end
aget '/s401' do
halt 401
end
aget '/a401' do
ahalt 401
end
aget '/async_close' do
# don't call body here, the 'user' is going to 'disconnect' before we do
env['async.close'].callback { self.class.singletons << 'async_closed' }
end
aget '/on_close' do
# sugared version of the above
on_close do
self.class.singletons << 'async_close_cleaned_up'
end
end
aget '/redirect' do
redirect '/'
end
aget '/aredirect' do
async_schedule { redirect '/' }
end
aget '/emredirect' do
em_async_schedule { redirect '/' }
end
aget '/agents', :agent => /chrome/ do
body { 'chrome' }
end
aget '/agents', :agent => /firefox/ do
body { 'firefox' }
end
aget '/agents' do
body { 'other' }
end
aget '/double_body_bug/:subdomain' do |subdomain|
body { settings.call_count += 1; '' }
end
aget '/param/:a' do |ma|
body { ma }
end
aget '/params/:a' do
body { params[:a] }
end
aget '/params/deferred/:a' do
async_schedule { body { aparams[:a] } }
end
aget '/halt-with-body' do
async_schedule { ahalt 404, 'halted' }
end
apatch '/patch' do
body { "apatch #{request.body.read}" }
end
# Defeat the test environment semantics, ensuring we actually follow the
# non-test branch of async_schedule. You would normally just call
# async_schedule in user apps, and use test helpers appropriately.
def em_async_schedule
o = self.class.environment
self.class.set :environment, :normal
async_schedule { yield }
ensure
self.class.set :environment, o
end
end
def app
Rack::Builder.new do
use CallbackCounter
run TestApp.new
end.to_app
end
def assert_redirect(path)
r = last_request.env
uri = r['rack.url_scheme'] + '://' + r['SERVER_NAME'] + path
assert_equal uri, last_response.location
end
def test_basic_async_get
get '/hello'
assert_async
async_continue
assert last_response.ok?
assert_equal 'hello async', last_response.body
end
def test_em_get
get '/em'
assert_async
em_async_continue
assert last_response.ok?
assert_equal 'em', last_response.body
end
def test_em_async_continue_timeout
get '/em_timeout'
assert_async
assert_raises(MiniTest::Assertion) do
em_async_continue(0.001)
end
end
def test_404
get '/404'
assert_async
async_continue
assert_equal 404, last_response.status
end
def test_302
get '/302'
assert_async
async_continue
assert_equal 302, last_response.status
end
def test_em_halt
get '/em_halt'
assert_async
em_async_continue
assert_equal 404, last_response.status
end
def test_error_blocks_sync
get '/s401'
assert_async
async_continue
assert_equal 401, last_response.status
assert_equal '401', last_response.body
end
def test_error_blocks_async
get '/a401'
assert_async
async_continue
assert_equal 401, last_response.status
assert_equal '401', last_response.body
end
def test_async_close
aget '/async_close'
async_close
assert_equal 'async_closed', TestApp.singletons.shift
end
def test_on_close
aget '/on_close'
async_close
assert_equal 'async_close_cleaned_up', TestApp.singletons.shift
end
def test_redirect
aget '/redirect'
assert last_response.redirect?
assert_equal 302, last_response.status
assert_redirect '/'
end
def test_aredirect
aget '/aredirect'
assert last_response.redirect?
assert_equal 302, last_response.status
assert_redirect '/'
end
def test_emredirect
aget '/emredirect'
em_async_continue
assert last_response.redirect?
assert_equal 302, last_response.status
assert_redirect '/'
end
def test_route_conditions_no_match
aget '/agents'
assert_equal 'other', last_response.body
end
def test_route_conditions_first
header "User-Agent", "chrome"
aget '/agents'
assert_equal 'chrome', last_response.body
end
def test_route_conditions_second
header "User-Agent", "firefox"
aget '/agents'
assert_equal 'firefox', last_response.body
end
def test_basic_async_head
head '/hello'
assert_async
async_continue
assert last_response.ok?
assert_equal '', last_response.body
assert_equal "11", last_response.headers['Content-Length']
end
def test_string_body
get '/world'
assert_async
async_continue
assert last_response.ok?
assert_equal 'world', last_response.body
end
def test_double_body_bug
aget '/double_body_bug/example'
assert_equal 1, settings.call_count
end
def test_block_with_no_args
aget '/params/test'
assert_equal 'test', last_response.body
end
def test_block_with_an_arg
aget '/param/test'
assert_equal 'test', last_response.body
end
def test_params_deferred
aget '/params/deferred/test'
assert_equal 'test', last_response.body
end
def test_two_requests
aget '/hello'
assert last_response.ok?
aget '/302'
assert_equal 302, last_response.status
end
def test_bug_42_halt_with_body_should_only_call_callback_once
start = CallbackCounter.count
aget '/halt-with-body'
assert_equal 'halted', last_response.body
assert_equal start + 1, CallbackCounter.count
end
def test_apatch
apatch "/patch", "body for apatch", { "CONTENT_TYPE" => "text/plain" }
assert_equal 200, last_response.status
assert_equal "apatch body for apatch", last_response.body
end
end