-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathclient.js
897 lines (778 loc) · 25.6 KB
/
client.js
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
function setupTransport() {
// Dummy transport
portal.transports.dummy = function() {
return {
open: function() {},
send: function() {},
close: function() {}
};
};
// The server-side view of the socket handling
// when using this, the user must use the server.fire and not use the client.fire
portal.transports.mock = function(client, options) {
var server;
// Hack for the server-side socket's transport
if (options.client) {
server = client;
client = options.client;
// The server does
return {
open: function() {},
send: function(data) {
if (client.state() === "opened") {
client._fire(data);
}
},
close: function() {
server.fire("close", "aborted");
}
};
}
// The client does
return {
feedback: true,
open: function() {
// Forwards control to the user
client.server = function(opts) {
if (server) {
return server;
}
// A server-side socket
opts = opts || {};
server = portal.open("/server~" + client.option("url"), {
reconnect: false,
sharing: false,
transports: ["mock"],
client: client
});
return server.on({
open: function() {
client.fire("open");
},
close: function(reason) {
// Safe though this is done by the client
client.fire("close", reason);
},
// Incomplete implementation but fine
heartbeat: function() {
if (!opts.noHeartbeat) {
// Only heartbeat event should be sent with some delay
setTimeout(function() {
server.send("heartbeat");
}, 1);
}
}
});
};
},
send: function(data) {
if (server && server.state() === "opened") {
server._fire(data);
}
},
close: function() {
if (server) {
server.fire("close", "aborted");
}
}
};
};
}
QUnit.module("portal", {
setup: function() {
helper.setup();
setupTransport();
portal.defaults.transports = ["dummy"];
},
teardown: helper.teardown
});
test("portal.open(url) should opens and returns a socket", function() {
ok(portal.open("dummy"));
});
test("portal.open(url, options) should opens and returns a socket", function() {
ok(portal.open("dummy", {}));
});
test("portal.find() should return the first socket", function() {
var first = portal.open("dummy1"), second = portal.open("dummy2");
strictEqual(first, portal.find());
notStrictEqual(second, portal.find());
});
test("portal.find(url) should return the socket which is mapped to the given url", function() {
var socket = portal.open("dummy");
strictEqual(socket, portal.find("dummy"));
strictEqual(portal.find("absent"), null);
});
test("portal.find(url) should work with both absolute url and relative url", function() {
var socket = portal.open("dummy");
strictEqual(socket, portal.find("./dummy"));
strictEqual(socket, portal.find(portal.support.getAbsoluteURL("dummy")));
});
QUnit.module("socket", {
setup: function() {
helper.setup();
setupTransport();
portal.defaults.transports = ["dummy"];
},
teardown: helper.teardown
});
// Event managing
test("socket.on(event, handler) should add a given event handler for a given event", 2, function() {
portal.open("dummy").on("event", helper.okTrue).fire("event").fire("event");
});
test("The 'this' of any event handler should refer the socket where the event occurs", 1, function() {
var socket = portal.open("dummy");
socket.on("event", function() {
strictEqual(this, socket);
})
.fire("event");
});
test("socket.on(handlers) should add a given event handlers from a given map of event type and event handler", 2, function() {
portal.open("dummy").on({event: helper.okTrue}).fire("event").fire("event");
});
test("socket.off(event, handler) should remove a given event handler for a given event", function() {
portal.open("dummy").on("event", helper.okFalse).off("event", helper.okFalse).on("event", helper.okTrue).fire("event");
});
test("socket.one(event, handler) should add a given one time event handler for a given event", 1, function() {
portal.open("dummy").one("event", helper.okTrue).fire("event").fire("event");
});
// Life cycle and event
test("socket.state() should determine the current state of the socket", 1, function() {
strictEqual(typeof portal.open("dummy").state(), "string");
});
test("socket.state() should be 'preparing' as an initial state", 1, function() {
strictEqual(portal.open("dummy", {prepare: function() {}}).state(), "preparing");
});
test("connecting event should be fired only once when a connection is tried", 1, function() {
var output = "",
out = function(str) {
return function() {
output += str;
};
};
portal.open("dummy").on("connecting", out("A")).on("connecting", out("B")).fire("connecting");
strictEqual(output, "AB");
});
test("socket.connecting(fn) should add the given handler for the connecting event", 1, function() {
var output = "",
out = function(str) {
return function() {
output += str;
};
};
portal.open("dummy").connecting(out("A")).connecting(out("B")).fire("connecting");
strictEqual(output, "AB");
});
test("socket.state() should be 'connecting' on the connecting event", 1, function() {
portal.open("dummy").connecting(function() {
strictEqual(this.state(), "connecting");
});
});
test("connecting event handler should receive no arguments", 1, function() {
portal.open("dummy").on("connecting", function() {
strictEqual(arguments.length, 0);
});
});
test("waiting event should be fired only once when a reconnection has scheduled", 1, function() {
var output = "",
out = function(str) {
return function() {
output += str;
};
};
portal.open("dummy").on("waiting", out("A")).fire("close").on("waiting", out("B")).fire("waiting");
strictEqual(output, "AB");
});
test("socket.waiting(fn) should add the given handler for the waiting event", 1, function() {
var output = "",
out = function(str) {
return function() {
output += str;
};
};
portal.open("dummy").waiting(out("A")).fire("close").waiting(out("B")).fire("waiting");
strictEqual(output, "AB");
});
test("socket.state() should be 'waiting' on the waitingevent", 1, function() {
portal.open("dummy").waiting(function() {
strictEqual(this.state(), "waiting");
})
.fire("waiting");
});
test("waiting event handler should receive the reconnection delay and the total number of reconnection attempts", 2, function() {
portal.open("dummy").waiting(function(delay, attempts) {
strictEqual(delay, 500);
strictEqual(attempts, 1);
})
.fire("close");
});
test("open event should be fired only once when a connection is established", 1, function() {
var output = "",
out = function(str) {
return function() {
output += str;
};
};
portal.open("dummy").on("open", out("A")).fire("open").on("open", out("B")).fire("open");
strictEqual(output, "AB");
});
test("socket.open(fn) should add the given handler for the open event", 1, function() {
var output = "",
out = function(str) {
return function() {
output += str;
};
};
portal.open("dummy").open(out("A")).fire("open").open(out("B")).fire("open");
strictEqual(output, "AB");
});
test("socket.state() should be 'opened' on the open event", 1, function() {
portal.open("dummy").open(function() {
strictEqual(this.state(), "opened");
})
.fire("open");
});
test("open event handler should receive no arguments", 1, function() {
portal.open("dummy").fire("open").on("open", function() {
strictEqual(arguments.length, 0);
});
});
test("connecting event should become locked on the open event", 2, function() {
portal.open("dummy").on("connecting", helper.okTrue).open(helper.okTrue).fire("open").on("connecting", helper.okFalse);
});
test("Accumulated events which are sent before the open event should be sent", function() {
var received = [],
client = portal.open("mock", {transports: ["mock"]}).send("event", "v0").send("event", "v1"),
server = client.server().on("event", function(value) {
received.push(value);
});
strictEqual(received.length, 0);
strictEqual(client.state(), "connecting");
server.fire("open");
strictEqual(received.length, 2);
strictEqual(client.state(), "opened");
client.send("event", "v2");
strictEqual(received.length, 3);
});
test("message event should be fired multiple times when a message event has been received", 1, function() {
var output = "",
out = function(str1) {
return function() {
output += str1;
};
};
portal.open("dummy").on("message", out("A")).fire("message").on("message", out("B")).fire("message");
strictEqual(output, "AAB");
});
test("socket.message(fn) should add the given handler for the message event", 1, function() {
var output = "",
out = function(str1) {
return function() {
output += str1;
};
};
portal.open("dummy").message(out("A")).fire("message").message(out("B")).fire("message");
strictEqual(output, "AAB");
});
test("message event should receive the data", 1, function() {
var output = "",
out = function(str1) {
return function(str2) {
output += str1 + str2;
};
};
portal.open("dummy").message(out("A")).fire("message", "0").message(out("B")).fire("message", "1");
strictEqual(output, "A0A1B1");
});
test("message event should receive the data and the optional callback for replying", 2, function() {
portal.open("mock", {transports: ["mock"]}).message(function(data, reply) {
strictEqual(data, "data");
ok(portal.support.isFunction(reply));
})
.server().fire("open").send("message", "data", function() {});
});
test("callback which is a second argument of message event handler should be able to send a reply to the server", 1, function() {
portal.open("mock", {transports: ["mock"]}).message(function(data, reply) {
reply(data);
})
.server().fire("open").send("message", "data", function(data) {
strictEqual(data, "data");
});
});
test("close event should be fired once when a connection has been closed", 1, function() {
var output = "",
out = function(str) {
return function() {
output += str;
};
};
portal.open("dummy").on("close", out("A")).fire("close").on("close", out("B")).fire("close");
strictEqual(output, "AB");
});
test("socket.close(fn) should add the given handler for the close event", 1, function() {
var output = "",
out = function(str) {
return function() {
output += str;
};
};
portal.open("dummy").close(out("A")).fire("close").close(out("B")).fire("close");
strictEqual(output, "AB");
});
test("socket.state() should be 'closed' on the close event", 1, function() {
portal.open("dummy").close(function() {
strictEqual(this.state(), "closed");
})
.fire("close");
});
test("close event handler should receive the connection close reason", 1, function() {
portal.open("dummy").fire("close", "").on("close", function(reason) {
strictEqual(typeof reason, "string");
});
});
test("close reason should be 'canceled' when preparation fails", 1, function() {
portal.open("dummy", {
prepare: function(connect, cancel) {
cancel();
}
})
.close(function(reason) {
strictEqual(reason, "canceled");
});
});
test("close reason should be 'notransport' when there is no available transport", 1, function() {
portal.transports.spaceship = function() {};
portal.open("dummy", {transports: ["spaceship"]}).close(function(reason) {
strictEqual(reason, "notransport");
});
});
test("close reason should be 'done' when the connection has been closed normally", 1, function() {
portal.open("dummy").close(function(reason) {
strictEqual(reason, "done");
})
.fire("close", "done");
});
test("close reason should be 'aborted' when the connection has been closed by its close()", 1, function() {
portal.open("dummy").close(function(reason) {
strictEqual(reason, "aborted");
})
.close();
});
asyncTest("close reason should be 'timeout' when the connection has been timed out", 1, function() {
portal.open("dummy", {timeout: 10}).close(function(reason) {
strictEqual(reason, "timeout");
start();
});
});
test("close reason should be 'error' when the connection has been closed due to a server error or could not be opened", 1, function() {
portal.open("dummy").close(function(reason) {
strictEqual(reason, "error");
})
.fire("close", "error");
});
helper.each("connecting open message event".split(" "), function(i, event) {
test(event + " event should become locked on the close event", 2, function() {
portal.open("dummy").on(event, helper.okTrue).fire(event).close(helper.okTrue).fire("close").on(event, helper.okFalse).fire(event);
});
test(event + " event propagation should be stopped on the close event", 1, function() {
portal.open("dummy2").on(event, helper.okTrue)
.on(event, function() {
this.close();
})
.on(event, helper.okFalse).fire(event);
});
});
// Misc methods
test("socket.option(key) should find the value of an option from the options merged with the default options and the given options", 3, function() {
var socket;
portal.defaults.tobeoverride = "A";
portal.defaults.onlyindefaults = "B";
socket = portal.open("dummy", {tobeoverride: "C", onlyinthis: "D"});
strictEqual(socket.option("onlyindefaults"), "B");
strictEqual(socket.option("tobeoverride"), "C");
strictEqual(socket.option("onlyinthis"), "D");
});
test("socket.option('id') should return the socket id", 1, function() {
strictEqual(typeof portal.open("dummy").option("id"), "string");
});
test("socket.option('url') should return an absolute url of the original url", 1, function() {
strictEqual(portal.open("dummy").option("url"), portal.support.getAbsoluteURL("dummy"));
});
test("socket.data(key) and socket.data(key, value) should return and store the connection-scoped data with the specified key", 1, function() {
strictEqual(portal.open("dummy").data("var", "variable").data("var"), "variable");
});
asyncTest("The connection scope should reset every time the socket opens", 2, function() {
var latch;
portal.open("dummy").data("var", "variable").connecting(function() {
if (latch) {
ok(this.data("var") == null);
start();
} else {
latch = true;
strictEqual(this.data("var"), "variable");
}
})
.fire("close");
});
// Communication
test("socket.send(event) should send an event whose type is a given event to the server", 1, function() {
portal.open("mock", {transports: ["mock"]}).send("event")
.server().on("event", helper.okTrue).fire("open");
});
test("socket.send(event, data) should send an event whose type is a given event and data is a given data to the server", 6, function() {
var client = portal.open("mock", {transports: ["mock"]}),
server = client.server();
helper.each({string: "string", number: 1, object: {a: 0}, array: [0], "true": true, "false": false}, function(k, v) {
client.send("event-" + k, v);
server.on("event-" + k, function(data) {
if (typeof data === "object") {
deepEqual(data, v);
} else {
strictEqual(data, v);
}
});
});
server.fire("open");
});
test("socket.send(event, data, done, fail) should send the event and register done and fail callback", 8, function() {
function assertData(data) {
strictEqual(data, "data");
}
portal.open("mock", {transports: ["mock"]})
.send("event-donewithoutdata", "data", helper.okTrue, helper.okFalse)
.send("event-donewithdata", "data", assertData, helper.okFalse)
.send("event-failwithoutdata", "data", helper.okFalse, helper.okTrue)
.send("event-failwithdata", "data", helper.okFalse, assertData)
.server().on("event-donewithoutdata", function(data, reply) {
strictEqual(data, "data");
reply();
})
.on("event-donewithdata", function(data, reply) {
strictEqual(data, "data");
reply(data);
})
.on("event-failwithoutdata", function(data, reply) {
strictEqual(data, "data");
this.send("reply", {id: this.option("lastEventId"), exception: true});
})
.on("event-failwithdata", function(data, reply) {
strictEqual(data, "data");
this.send("reply", {id: this.option("lastEventId"), exception: true, data: data});
})
.fire("open");
});
test("socket.send(event, data, done, fail) should send the event and register done and fail callback event name", 8, function() {
portal.open("mock", {transports: ["mock"]})
.on("okTrue", helper.okTrue)
.on("okFalse", helper.okFalse)
.on("assert-data", function(data) {
strictEqual(data, "data");
})
.send("event-donewithoutdata", "data", "okTrue", "okFalse")
.send("event-donewithdata", "data", "assert-data", "okFalse")
.send("event-failwithoutdata", "data", "okFalse", "okTrue")
.send("event-failwithdata", "data", "okFalse", "assert-data")
.server().on("event-donewithoutdata", function(data, reply) {
strictEqual(data, "data");
reply();
})
.on("event-donewithdata", function(data, reply) {
strictEqual(data, "data");
reply(data);
})
.on("event-failwithoutdata", function(data, reply) {
strictEqual(data, "data");
this.send("reply", {id: this.option("lastEventId"), exception: true});
})
.on("event-failwithdata", function(data, reply) {
strictEqual(data, "data");
this.send("reply", {id: this.option("lastEventId"), exception: true, data: data});
})
.fire("open");
});
test("socket.close() should close the socket", 1, function() {
portal.open("dummyk").close(helper.okTrue).close();
});
test("socket.close() should prevent reconnection", 1, function() {
portal.open("dummyk").close(helper.okTrue).waiting(helper.okFalse);
});
// Options
test("transports option should be an array of the transport candidate id to be used, in order of index", 1, function() {
var output = "",
transports = ["bus", "subway", "bicycle"];
helper.each(transports, function(i, name) {
portal.transports[name] = function() {
output += name;
};
});
portal.open("dummy", {transports: transports});
strictEqual(output, transports.join(""));
});
asyncTest("timeout option should start timeout timer on the connecting event", 2, function() {
portal.open("dummy", {timeout: 10}).connecting(helper.okTrue).open(helper.okFalse).close(function() {
ok(true);
start();
});
});
asyncTest("timeout option should cancel timeout timer on the open event", 2, function() {
var socket = portal.open("dummy", {timeout: 10}).connecting(helper.okTrue).open(helper.okTrue).fire("open").close(helper.okFalse);
setTimeout(function() {
socket.off("close", helper.okFalse);
start();
}, 10);
});
asyncTest("heartbeat option should send a heartbeat event each time the value has elapsed", 6, function() {
var i = 0;
portal.open("mock", {transports: ["mock"], heartbeat: 60, _heartbeat: 30})
.on("heartbeat", function() {
ok(true);
i++;
if (i > 2) {
this.close();
start();
}
})
.server().fire("open").on("heartbeat", helper.okTrue);
});
asyncTest("heartbeat option should fire the close event if the server makes no response to a heartbeat", 1, function() {
portal.open("mock", {transports: ["mock"], heartbeat: 10, _heartbeat: 5})
.on("heartbeat", helper.okFalse)
.close(function() {
start();
})
.server({noHeartbeat: true}).fire("open").on("heartbeat", helper.okTrue);
});
test("lastEventId option should be the last event id", 5, function() {
var i = 0;
function assertEventId() {
strictEqual(this.option("lastEventId"), ++i);
}
portal.open("mock", {transports: ["mock"]}).message(assertEventId).on("another", assertEventId)
.server().fire("open").send("message").send("message").send("message").send("another").send("another");
});
test("lastEventId option should start from the assigned value if it is set", 2, function() {
portal.open("mock", {transports: ["mock"], lastEventId: 25})
.open(function() {
strictEqual(this.option("lastEventId"), 25);
})
.message(function() {
strictEqual(this.option("lastEventId"), 1);
})
.server().fire("open").send("message");
});
test("prepare option should be called before the socket tries to connect", 1, function() {
portal.open("dummy", {prepare: helper.okTrue}).connecting(helper.okFalse);
});
test("prepare option should receive a callback to connect, a callback to cancel and merged modifiable options", 4, function() {
portal.open("dummy1", {
vassline: "black silence",
prepare: function(connect, cancel, options) {
strictEqual(options.vassline, "black silence");
connect();
}
})
.connecting(helper.okTrue);
portal.open("dummy2", {
prepare: function(connect, cancel, options) {
strictEqual(options.heartbeat, false);
options.heartbeat = 5000;
cancel();
}
})
.connecting(helper.okFalse)
.close(function() {
strictEqual(this.option("heartbeat"), 5000);
});
});
test("reconnect option should be executed to schedule reconnection on the close event", 1, function() {
var output = "",
out = function(str) {
return function() {
output += str;
};
};
portal.open("dummy", {reconnect: out("B")}).close(out("A")).waiting(out("C")).fire("close");
strictEqual(output, "ABC");
});
test("reconnect option should prevent the socket from reconnecting when it is or returns false", 3, function() {
portal.open("dummy1", {reconnect: false}).fire("close").close(helper.okTrue).waiting(helper.okFalse);
portal.open("dummy2", {
reconnect: function() {
ok(true);
return false;
}
})
.fire("close").close(helper.okTrue).waiting(helper.okFalse);
});
asyncTest("reconnect option should receive the last delay and the total number of reconnection attempts and return a delay to reconnect", 16, function() {
var i = 0, lastDelay = -1;
portal.open("dummy2", {
reconnect: function(delay, attempts) {
delay = delay || 0;
strictEqual(attempts, i);
strictEqual(delay, lastDelay + 1);
lastDelay = delay;
return delay + 1;
}
})
.connecting(function() {
i++;
ok(true);
this.fire("close");
})
.waiting(function(delay, attempts) {
strictEqual(i, attempts);
if (attempts > 3) {
this.close();
start();
}
});
});
test("idGenerator option should generate a socket id", 1, function() {
strictEqual(portal.open("dummy", {
idGenerator: function() {
return "envy";
}
})
.option("id"), "envy");
});
test("urlBuilder option should build an url receiving the absolute form of url, a params object and the when", 17, function() {
var params,
when,
socket = portal.open("dummy", {
urlBuilder: function(url, parameters, on) {
strictEqual(url, portal.support.getAbsoluteURL("dummy"), "A");
params = parameters;
ok(delete params._);
when = on;
return "/wow-" + on;
}
});
strictEqual(socket.buildURL("open"), "/wow-open");
deepEqual(params, {id: socket.option("id"), transport: "dummy", heartbeat: false, lastEventId: 0});
strictEqual(when, "open");
strictEqual(socket.buildURL("poll"), "/wow-poll");
deepEqual(params, {id: socket.option("id"), transport: "dummy", lastEventId: 0, lastEventIds: undefined /* default value is not specified */});
strictEqual(when, "poll");
strictEqual(socket.buildURL("abort"), "/wow-abort");
deepEqual(params, {id: socket.option("id")});
strictEqual(when, "abort");
});
test("params option should be merged with the default params and passed to the urlBuilder option", 8, function() {
var params,
when,
noop = function() {},
socket = portal.open("dummy", {
params: {
open: {val: "x", fn: noop},
poll: {val: "y", fn: noop},
abort: {val: "z", fn: noop}
},
urlBuilder: function(url, params, on) {
switch (on) {
case "open":
strictEqual(params.val, "x");
strictEqual(params.fn, noop);
break;
case "poll":
strictEqual(params.val, "y");
strictEqual(params.fn, noop);
break;
case "abort":
strictEqual(params.val, "z");
strictEqual(params.fn, noop);
break;
default:
ok(false);
break;
}
return url;
}
});
socket.buildURL("open");
socket.buildURL("poll");
socket.buildURL("abort");
});
test("inbound option should convert data by the server into an event object", 6, function() {
var inbound, socket;
socket = portal.open("dummy", {
inbound: function() {
return inbound.apply(this, arguments);
}
})
.fire("open");
inbound = function(data) {
return {type: "message"};
};
socket.one("message", function(data, reply) {
ok(!data);
ok(!reply);
})
._fire("a");
inbound = function(data) {
return {type: "message", data: data};
};
socket.one("message", function(data, reply) {
strictEqual(data, "a");
ok(!reply);
})
._fire("a");
inbound = function(data) {
return {type: "message", data: data, reply: true};
};
socket.one("message", function(data, reply) {
strictEqual(data, "a");
ok(reply);
})
._fire("a");
});
test("outbound option should convert an event object into data to be sent to the server", 24, function() {
var outboundInternal, socket, i = 0;
portal.transports.dummy = function() {
return {
open: function() {},
send: function(payload) {
strictEqual(payload, "payload");
},
close: function() {}
};
};
socket = portal.open("dummy", {
outbound: function() {
outboundInternal.apply(this, arguments);
return "payload";
}
})
.fire("open");
outboundInternal = function(event) {
strictEqual(event.socket, socket.option("id"));
strictEqual(event.id, ++i);
strictEqual(event.type, "event");
ok(!event.data);
ok(!event.reply);
};
socket.send("event");
outboundInternal = function(event) {
strictEqual(event.socket, socket.option("id"));
strictEqual(event.id, ++i);
strictEqual(event.type, "event");
strictEqual(event.data, "data");
ok(!event.reply);
};
socket.send("event", "data");
outboundInternal = function(event) {
strictEqual(event.socket, socket.option("id"));
strictEqual(event.id, ++i);
strictEqual(event.type, "event");
strictEqual(event.data, "data");
ok(event.reply, "data");
};
socket.send("event", "data", function() {});
outboundInternal = function(event) {
strictEqual(event.socket, socket.option("id"));
strictEqual(event.id, ++i);
strictEqual(event.type, "event");
strictEqual(event.data, "data");
ok(event.reply, "data");
};
socket.send("event", "data", null, function() {});
});