@@ -1544,6 +1544,10 @@ describe('WebSocket', function() {
1544
1544
ws . onclose = listener ;
1545
1545
ws . onopen = listener ;
1546
1546
1547
+ assert . ok ( ws . binaryType === 'nodebuffer' ) ;
1548
+ ws . binaryType = 'arraybuffer' ;
1549
+ assert . ok ( ws . binaryType === 'arraybuffer' ) ;
1550
+
1547
1551
assert . ok ( ws . onopen === listener ) ;
1548
1552
assert . ok ( ws . onmessage === listener ) ;
1549
1553
assert . ok ( ws . onclose === listener ) ;
@@ -1694,6 +1698,63 @@ describe('WebSocket', function() {
1694
1698
client . send ( 'hi' )
1695
1699
} ) ;
1696
1700
} ) ;
1701
+
1702
+ it ( 'should pass binary data as a node.js Buffer by default' , function ( done ) {
1703
+ server . createServer ( ++ port , function ( srv ) {
1704
+ var ws = new WebSocket ( 'ws://localhost:' + port ) ;
1705
+ var array = new Uint8Array ( 4096 ) ;
1706
+
1707
+ ws . onopen = function ( ) {
1708
+ ws . send ( array , { binary : true } ) ;
1709
+ } ;
1710
+ ws . onmessage = function ( messageEvent ) {
1711
+ assert . ok ( messageEvent . binary ) ;
1712
+ assert . ok ( ws . binaryType === 'nodebuffer' ) ;
1713
+ assert . ok ( messageEvent . data instanceof Buffer ) ;
1714
+ ws . terminate ( ) ;
1715
+ srv . close ( ) ;
1716
+ done ( ) ;
1717
+ } ;
1718
+ } ) ;
1719
+ } ) ;
1720
+
1721
+ it ( 'should pass an ArrayBuffer for event.data if binaryType = arraybuffer' , function ( done ) {
1722
+ server . createServer ( ++ port , function ( srv ) {
1723
+ var ws = new WebSocket ( 'ws://localhost:' + port ) ;
1724
+ ws . binaryType = 'arraybuffer' ;
1725
+ var array = new Uint8Array ( 4096 ) ;
1726
+
1727
+ ws . onopen = function ( ) {
1728
+ ws . send ( array , { binary : true } ) ;
1729
+ } ;
1730
+ ws . onmessage = function ( messageEvent ) {
1731
+ assert . ok ( messageEvent . binary ) ;
1732
+ assert . ok ( messageEvent . data instanceof ArrayBuffer ) ;
1733
+ ws . terminate ( ) ;
1734
+ srv . close ( ) ;
1735
+ done ( ) ;
1736
+ } ;
1737
+ } ) ;
1738
+ } ) ;
1739
+
1740
+ it ( 'should ignore binaryType for text messages' , function ( done ) {
1741
+ server . createServer ( ++ port , function ( srv ) {
1742
+ var ws = new WebSocket ( 'ws://localhost:' + port ) ;
1743
+ ws . binaryType = 'arraybuffer' ;
1744
+
1745
+ ws . onopen = function ( ) {
1746
+ ws . send ( 'foobar' ) ;
1747
+ } ;
1748
+ ws . onmessage = function ( messageEvent ) {
1749
+ assert . ok ( ! messageEvent . binary ) ;
1750
+ assert . ok ( typeof messageEvent . data === 'string' ) ;
1751
+ ws . terminate ( ) ;
1752
+ srv . close ( ) ;
1753
+ done ( ) ;
1754
+ } ;
1755
+ } ) ;
1756
+ } ) ;
1757
+
1697
1758
} ) ;
1698
1759
1699
1760
describe ( 'ssl' , function ( ) {
0 commit comments