faye 0.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of faye might be problematic. Click here for more details.

@@ -0,0 +1,34 @@
1
+ require "test/unit"
2
+ require "faye"
3
+
4
+ class TestChannel < Test::Unit::TestCase
5
+ include Faye
6
+
7
+ def test_channel_storage
8
+ tree = Channel::Tree.new
9
+ tree['invalid/name'] = 1
10
+ tree['/valid/name'] = 2
11
+ tree['/va()$$lid/name'] = 3
12
+
13
+ assert_equal nil, tree['invalid/name']
14
+ assert_equal 2, tree['/valid/name']
15
+ assert_equal 3, tree['/va()$$lid/name']
16
+ end
17
+
18
+ def test_globbing
19
+ globber = Channel::Tree.new
20
+ globber['/foo/bar'] = 1
21
+ globber['/foo/boo'] = 2
22
+ globber['/foo'] = 3
23
+ globber['/foobar'] = 4
24
+ globber['/foo/bar/boo'] = 5
25
+ globber['/foobar/boo'] = 6
26
+ globber['/foo/*'] = 7
27
+ globber['/foo/**'] = 8
28
+
29
+ assert_equal [1,2,7,8], globber.glob('/foo/*').sort
30
+ assert_equal [1,7,8], globber.glob('/foo/bar').sort
31
+ assert_equal [1,2,5,7,8], globber.glob('/foo/**').sort
32
+ assert_equal [5,8], globber.glob('/foo/bar/boo').sort
33
+ end
34
+ end
@@ -0,0 +1,86 @@
1
+ require "test/unit"
2
+ require "faye"
3
+
4
+ class TestGrammar < Test::Unit::TestCase
5
+ include Faye::Grammar
6
+
7
+ def test_single_chars
8
+ assert LOWALPHA =~ 'a'
9
+ assert LOWALPHA !~ 'A'
10
+ assert LOWALPHA !~ 'aa'
11
+
12
+ assert UPALPHA !~ 'a'
13
+ assert UPALPHA =~ 'A'
14
+ assert UPALPHA !~ 'AA'
15
+
16
+ assert ALPHA =~ 'a'
17
+ assert ALPHA =~ 'A'
18
+ assert ALPHA !~ '0'
19
+ assert ALPHA !~ 'aA'
20
+
21
+ assert DIGIT =~ '0'
22
+ assert DIGIT !~ '90'
23
+ assert DIGIT !~ 'z'
24
+
25
+ assert ALPHANUM =~ '6'
26
+ assert ALPHANUM =~ 'b'
27
+ assert ALPHANUM !~ '6b'
28
+ assert ALPHANUM !~ '/'
29
+
30
+ assert MARK =~ '-'
31
+ assert MARK =~ '@'
32
+ assert MARK !~ '!-'
33
+ end
34
+
35
+ def test_strings
36
+ assert STRING =~ ''
37
+ assert STRING =~ ' 4tv4 (($ !/~ ..* /) ___'
38
+ assert STRING !~ ' 4tv4 (($ !/~ ..* /) _}_'
39
+
40
+ assert TOKEN =~ '$a9b'
41
+ assert TOKEN !~ ''
42
+
43
+ assert INTEGER =~ '9'
44
+ assert INTEGER =~ '09'
45
+ assert INTEGER !~ '9.0'
46
+ assert INTEGER !~ '9a'
47
+ end
48
+
49
+ def test_channels
50
+ assert CHANNEL_NAME =~ '/fo_o/$@()bar'
51
+ assert CHANNEL_NAME !~ '/foo/$@()bar/'
52
+ assert CHANNEL_NAME !~ 'foo/$@()bar'
53
+ assert CHANNEL_NAME !~ '/fo o/$@()bar'
54
+
55
+ assert CHANNEL_PATTERN =~ '/!!/$/*'
56
+ assert CHANNEL_PATTERN =~ '/!!/$/**'
57
+ assert CHANNEL_PATTERN !~ '/!!/$/**/'
58
+ assert CHANNEL_PATTERN !~ '!!/$/**'
59
+ assert CHANNEL_PATTERN !~ '/!!/$/**/*'
60
+ end
61
+
62
+ def test_version
63
+ assert VERSION =~ '9'
64
+ assert VERSION =~ '9.a-delta4'
65
+ assert VERSION !~ '9.a-delta4.'
66
+ assert VERSION !~ 'K.a-delta4'
67
+ end
68
+
69
+ def test_ids
70
+ assert CLIENT_ID =~ '9'
71
+ assert CLIENT_ID =~ 'j'
72
+ assert CLIENT_ID !~ ''
73
+ assert CLIENT_ID =~ 'dfghs5r'
74
+ assert CLIENT_ID !~ 'dfg_hs5r'
75
+ end
76
+
77
+ def test_errors
78
+ assert ERROR =~ '401::No client ID'
79
+ assert ERROR !~ '401:No client ID'
80
+ assert ERROR !~ '40::No client ID'
81
+ assert ERROR !~ '40k::No client ID'
82
+ assert ERROR =~ '402:xj3sjdsjdsjad:Unknown Client ID'
83
+ assert ERROR =~ '403:xj3sjdsjdsjad,/foo/bar:Subscription denied'
84
+ assert ERROR =~ '404:/foo/bar:Unknown Channel'
85
+ end
86
+ end
@@ -0,0 +1,420 @@
1
+ require "test/unit"
2
+ require "faye"
3
+
4
+ class TestServer < Test::Unit::TestCase
5
+ include Faye
6
+
7
+ def setup
8
+ @server = Server.new
9
+ end
10
+
11
+ def get_client_id
12
+ handshake( 'version' => '1.0',
13
+ 'supportedConnectionTypes' => %w[long-polling] )
14
+ @r['clientId']
15
+ end
16
+
17
+ def method_missing(*args, &block)
18
+ @r = @server.__send__(*args, &block)
19
+ end
20
+
21
+ def test_handshake
22
+ #================================================================
23
+ # MUST
24
+ handshake( 'version' => '1.0',
25
+ 'supportedConnectionTypes' => %w[long-polling] )
26
+ # MAY include minimumVersion, ext, id
27
+ # SHOULD NOT send other messages with handshake
28
+
29
+ # MUST
30
+ assert_equal '/meta/handshake', @r['channel']
31
+ assert_not_equal nil, @r['version']
32
+ assert_equal %w[long-polling callback-polling], @r['supportedConnectionTypes']
33
+ assert_match /[a-z0-9]+/, @r['clientId']
34
+ assert_equal true, @r['successful']
35
+ # MAY
36
+ assert_equal nil, @r['id']
37
+ # MAY include minimumVersion, advice, ext, authSuccessful
38
+
39
+ #================================================================
40
+ # Unique IDs
41
+ id1 = @r['clientId']
42
+ assert_equal [id1], @server.client_ids
43
+ handshake( 'version' => '1.0', 'supportedConnectionTypes' => %w[callback-polling] )
44
+ id2 = @r['clientId']
45
+ assert_not_equal id1, id2
46
+ assert_equal [id1, id2].sort, @server.client_ids.sort
47
+
48
+ #================================================================
49
+ handshake( 'version' => '1.0',
50
+ 'supportedConnectionTypes' => %w[long-polling],
51
+ 'id' => 'foo' )
52
+ # MAY
53
+ assert_equal 'foo', @r['id']
54
+
55
+ #================================================================
56
+ # missing version
57
+ handshake('supportedConnectionTypes' => %w[long-polling])
58
+ # MUST
59
+ assert_equal '/meta/handshake', @r['channel']
60
+ assert_equal false, @r['successful']
61
+ assert_equal '402:version:Missing required parameter', @r['error']
62
+ # MAY
63
+ assert_not_equal nil, @r['version']
64
+ assert_equal %w[long-polling callback-polling], @r['supportedConnectionTypes']
65
+ # MAY include advice, minimumVersion, ext, id
66
+
67
+ #================================================================
68
+ # no matching connection type
69
+ handshake('supportedConnectionTypes' => %w[iframe flash])
70
+ # MUST
71
+ assert_equal '/meta/handshake', @r['channel']
72
+ assert_equal false, @r['successful']
73
+ assert_equal '301:iframe,flash:Connection types not supported', @r['error']
74
+ # MAY
75
+ assert_equal %w[long-polling callback-polling], @r['supportedConnectionTypes']
76
+ assert_equal nil, @r['id']
77
+
78
+ #================================================================
79
+ # no given connection type
80
+ handshake('version' => '1.0', 'id' => 'foo')
81
+ # MUST
82
+ assert_equal '/meta/handshake', @r['channel']
83
+ assert_equal false, @r['successful']
84
+ assert_equal '402:supportedConnectionTypes:Missing required parameter', @r['error']
85
+ # MAY
86
+ assert_equal %w[long-polling callback-polling], @r['supportedConnectionTypes']
87
+ assert_equal 'foo', @r['id']
88
+ end
89
+
90
+ def test_connect
91
+ @id = get_client_id
92
+ #================================================================
93
+ # MUST
94
+ connect( 'clientId' => @id,
95
+ 'connectionType' => 'long-polling' )
96
+ # MUST
97
+ assert_equal '/meta/connect', @r['channel']
98
+ assert_equal true, @r['successful']
99
+ assert_equal @id, @r['clientId']
100
+ # MAY
101
+ assert_equal nil, @r['id']
102
+ # MAY include error, advice, ext, timestamp
103
+
104
+ #================================================================
105
+ connect( 'clientId' => @id,
106
+ 'connectionType' => 'long-polling',
107
+ 'id' => 'foo' )
108
+ # MUST
109
+ assert_equal '/meta/connect', @r['channel']
110
+ assert_equal true, @r['successful']
111
+ assert_equal @id, @r['clientId']
112
+ # MAY
113
+ assert_equal 'foo', @r['id']
114
+
115
+ #================================================================
116
+ # no client ID
117
+ connect( 'connectionType' => 'long-polling' )
118
+ # MUST
119
+ assert_equal '/meta/connect', @r['channel']
120
+ assert_equal false, @r['successful']
121
+ assert_equal nil, @r['clientId']
122
+ # MAY
123
+ assert_equal '402:clientId:Missing required parameter', @r['error']
124
+ assert_equal nil, @r['id']
125
+ # MAY include advice, ext, timestamp
126
+
127
+ #================================================================
128
+ # no connection type
129
+ connect( 'clientId' => @id )
130
+ # MUST
131
+ assert_equal '/meta/connect', @r['channel']
132
+ assert_equal false, @r['successful']
133
+ assert_equal nil, @r['clientId']
134
+ # MAY
135
+ assert_equal '402:connectionType:Missing required parameter', @r['error']
136
+ assert_equal nil, @r['id']
137
+ # MAY include advice, ext, timestamp
138
+ end
139
+
140
+ def test_disconnect
141
+ @id = get_client_id
142
+ assert_equal [@id], @server.client_ids
143
+
144
+ #================================================================
145
+ # MUST
146
+ disconnect( 'clientId' => @id )
147
+ # MUST
148
+ assert_equal '/meta/disconnect', @r['channel']
149
+ assert_equal @id, @r['clientId']
150
+ assert_equal true, @r['successful']
151
+ # MAY
152
+ assert_equal nil, @r['id']
153
+ # MAY include error, ext
154
+ assert_equal [], @server.client_ids
155
+
156
+ #================================================================
157
+ # missing client ID
158
+ disconnect( 'id' => 'foo' )
159
+ # MUST
160
+ assert_equal '/meta/disconnect', @r['channel']
161
+ assert_equal nil, @r['clientId']
162
+ assert_equal false, @r['successful']
163
+ # MAY
164
+ assert_equal '402:clientId:Missing required parameter', @r['error']
165
+ assert_equal 'foo', @r['id']
166
+ # MAY include ext
167
+
168
+ #================================================================
169
+ # unrecognised client ID
170
+ disconnect( 'clientId' => @id )
171
+ # MUST
172
+ assert_equal '/meta/disconnect', @r['channel']
173
+ assert_equal nil, @r['clientId']
174
+ assert_equal false, @r['successful']
175
+ # MAY
176
+ assert_equal "401:#{@id}:Unknown client", @r['error']
177
+ assert_equal nil, @r['id']
178
+ # MAY include ext
179
+ end
180
+
181
+ def test_subscribe
182
+ @id = get_client_id
183
+ #================================================================
184
+ # MUST
185
+ subscribe( 'clientId' => @id,
186
+ 'subscription' => '/foo' ) # channel name
187
+ # MAY include ext, id
188
+
189
+ # MUST
190
+ assert_equal '/meta/subscribe', @r['channel']
191
+ assert_equal true, @r['successful']
192
+ assert_equal @id, @r['clientId']
193
+ assert_equal ['/foo'], @r['subscription']
194
+ # MAY
195
+ assert_equal nil, @r['id']
196
+ # MAY include error, advice, ext, timestamp
197
+
198
+ #================================================================
199
+ # MUST
200
+ subscribe( 'clientId' => @id,
201
+ 'subscription' => '/foo/**' ) # channel pattern
202
+ # MAY include ext, id
203
+
204
+ # MUST
205
+ assert_equal '/meta/subscribe', @r['channel']
206
+ assert_equal true, @r['successful']
207
+ assert_equal @id, @r['clientId']
208
+ assert_equal ['/foo/**'], @r['subscription']
209
+ # MAY
210
+ assert_equal nil, @r['id']
211
+ # MAY include error, advice, ext, timestamp
212
+
213
+ #================================================================
214
+ # MUST
215
+ subscribe( 'clientId' => @id,
216
+ 'subscription' => ['/bar/*', '/foo'], # channel list
217
+ 'id' => 'baz' )
218
+ # MAY include ext
219
+
220
+ # MUST
221
+ assert_equal '/meta/subscribe', @r['channel']
222
+ assert_equal true, @r['successful']
223
+ assert_equal @id, @r['clientId']
224
+ assert_equal ['/bar/*', '/foo'], @r['subscription']
225
+ # MAY
226
+ assert_equal 'baz', @r['id']
227
+ # MAY include error, advice, ext, timestamp
228
+
229
+ #================================================================
230
+ # missing client ID
231
+ subscribe( 'subscription' => '/foo' )
232
+ # MAY include ext, id
233
+
234
+ # MUST
235
+ assert_equal '/meta/subscribe', @r['channel']
236
+ assert_equal false, @r['successful']
237
+ assert_equal nil, @r['clientId']
238
+ assert_equal ['/foo'], @r['subscription']
239
+ # MAY
240
+ assert_equal '402:clientId:Missing required parameter', @r['error']
241
+ assert_equal nil, @r['id']
242
+ # MAY include advice, ext, timestamp
243
+
244
+ #================================================================
245
+ # unknown client
246
+ subscribe( 'clientId' => 'nonesuch',
247
+ 'subscription' => '/j' )
248
+ # MAY include ext, id
249
+
250
+ # MUST
251
+ assert_equal '/meta/subscribe', @r['channel']
252
+ assert_equal false, @r['successful']
253
+ assert_equal 'nonesuch', @r['clientId']
254
+ assert_equal ['/j'], @r['subscription']
255
+ # MAY
256
+ assert_equal '401:nonesuch:Unknown client', @r['error']
257
+ assert_equal nil, @r['id']
258
+ # MAY include advice, ext, timestamp
259
+
260
+ #================================================================
261
+ # missing subscription
262
+ subscribe( 'clientId' => @id )
263
+ # MAY include ext, id
264
+
265
+ # MUST
266
+ assert_equal '/meta/subscribe', @r['channel']
267
+ assert_equal false, @r['successful']
268
+ assert_equal @id, @r['clientId']
269
+ assert_equal [], @r['subscription']
270
+ # MAY
271
+ assert_equal '402:subscription:Missing required parameter', @r['error']
272
+ assert_equal nil, @r['id']
273
+ # MAY include advice, ext, timestamp
274
+
275
+ #================================================================
276
+ # invalid channel
277
+ subscribe( 'clientId' => @id,
278
+ 'subscription' => '/not/**/valid' )
279
+ # MAY include ext, id
280
+
281
+ # MUST
282
+ assert_equal '/meta/subscribe', @r['channel']
283
+ assert_equal false, @r['successful']
284
+ assert_equal @id, @r['clientId']
285
+ assert_equal ['/not/**/valid'], @r['subscription']
286
+ # MAY
287
+ assert_equal "405:/not/**/valid:Invalid channel", @r['error']
288
+ assert_equal nil, @r['id']
289
+ # MAY include advice, ext, timestamp
290
+
291
+ #================================================================
292
+ # cannot subscribe to meta channels
293
+ subscribe( 'clientId' => @id,
294
+ 'subscription' => '/meta/foo' )
295
+ # MAY include ext, id
296
+
297
+ # MUST
298
+ assert_equal '/meta/subscribe', @r['channel']
299
+ assert_equal false, @r['successful']
300
+ assert_equal @id, @r['clientId']
301
+ assert_equal ['/meta/foo'], @r['subscription']
302
+ # MAY
303
+ assert_equal "403:/meta/foo:Forbidden channel", @r['error']
304
+ assert_equal nil, @r['id']
305
+ # MAY include advice, ext, timestamp
306
+
307
+ #================================================================
308
+ # cannot subscribe to service channels
309
+ subscribe( 'clientId' => @id,
310
+ 'subscription' => '/service/foo' )
311
+ # MAY include ext, id
312
+
313
+ # MUST
314
+ assert_equal '/meta/subscribe', @r['channel']
315
+ assert_equal false, @r['successful']
316
+ assert_equal @id, @r['clientId']
317
+ assert_equal ['/service/foo'], @r['subscription']
318
+ # MAY
319
+ assert_equal "403:/service/foo:Forbidden channel", @r['error']
320
+ assert_equal nil, @r['id']
321
+ # MAY include advice, ext, timestamp
322
+ end
323
+
324
+ def test_unsubscribe
325
+ @id = get_client_id
326
+ subscribe( 'clientId' => @id, 'subscription' => '/foo' )
327
+
328
+ #================================================================
329
+ # MUST
330
+ unsubscribe( 'clientId' => @id,
331
+ 'subscription' => '/foo' )
332
+ # MAY include ext, id
333
+
334
+ # MUST
335
+ assert_equal '/meta/unsubscribe', @r['channel']
336
+ assert_equal true, @r['successful']
337
+ assert_equal @id, @r['clientId']
338
+ assert_equal ['/foo'], @r['subscription']
339
+ # MAY
340
+ assert_equal nil, @r['id']
341
+ # MAY include error, advice, ext, timestamp
342
+
343
+ #================================================================
344
+ # missing client ID
345
+ unsubscribe( 'subscription' => '/foo' )
346
+ # MAY include ext, id
347
+
348
+ # MUST
349
+ assert_equal '/meta/unsubscribe', @r['channel']
350
+ assert_equal false, @r['successful']
351
+ assert_equal nil, @r['clientId']
352
+ assert_equal ['/foo'], @r['subscription']
353
+ # MAY
354
+ assert_equal '402:clientId:Missing required parameter', @r['error']
355
+ assert_equal nil, @r['id']
356
+ # MAY include advice, ext, timestamp
357
+
358
+ #================================================================
359
+ # unknown client
360
+ unsubscribe( 'clientId' => 'matz',
361
+ 'subscription' => '/foo' )
362
+ # MAY include ext, id
363
+
364
+ # MUST
365
+ assert_equal '/meta/unsubscribe', @r['channel']
366
+ assert_equal false, @r['successful']
367
+ assert_equal 'matz', @r['clientId']
368
+ assert_equal ['/foo'], @r['subscription']
369
+ # MAY
370
+ assert_equal '401:matz:Unknown client', @r['error']
371
+ assert_equal nil, @r['id']
372
+ # MAY include advice, ext, timestamp
373
+
374
+ #================================================================
375
+ # missing subscription
376
+ unsubscribe( 'clientId' => @id )
377
+ # MAY include ext, id
378
+
379
+ # MUST
380
+ assert_equal '/meta/unsubscribe', @r['channel']
381
+ assert_equal false, @r['successful']
382
+ assert_equal @id, @r['clientId']
383
+ assert_equal [], @r['subscription']
384
+ # MAY
385
+ assert_equal '402:subscription:Missing required parameter', @r['error']
386
+ assert_equal nil, @r['id']
387
+ # MAY include advice, ext, timestamp
388
+
389
+ #================================================================
390
+ # invalid channel
391
+ unsubscribe( 'clientId' => @id,
392
+ 'subscription' => '/not/**/valid' )
393
+ # MAY include ext, id
394
+
395
+ # MUST
396
+ assert_equal '/meta/unsubscribe', @r['channel']
397
+ assert_equal false, @r['successful']
398
+ assert_equal @id, @r['clientId']
399
+ assert_equal ['/not/**/valid'], @r['subscription']
400
+ # MAY
401
+ assert_equal "405:/not/**/valid:Invalid channel", @r['error']
402
+ assert_equal nil, @r['id']
403
+ # MAY include advice, ext, timestamp
404
+ end
405
+
406
+ def test_advice
407
+ handle( 'channel' => '/meta/subscribe', 'subscription' => '/foo', 'clientId' => 'fake' ) do |r|
408
+ assert_equal '401:fake:Unknown client', r['error']
409
+ assert_equal 'handshake', r['advice']['reconnect']
410
+ assert_equal 1000, r['advice']['interval']
411
+ end
412
+
413
+ id = get_client_id
414
+ handle( 'channel' => '/meta/subscribe', 'subscription' => '/foo', 'clientId' => id ) do |r|
415
+ assert_equal true, r['successful']
416
+ assert_equal 'retry', r['advice']['reconnect']
417
+ assert_equal 1000, r['advice']['interval']
418
+ end
419
+ end
420
+ end