@@ -2866,113 +2866,6 @@ describe('WebSocket', () => {
2866
2866
} ) ;
2867
2867
} ) ;
2868
2868
2869
- it ( 'can send and receive text data' , ( done ) => {
2870
- const wss = new WebSocket . Server (
2871
- {
2872
- perMessageDeflate : { threshold : 0 } ,
2873
- port : 0
2874
- } ,
2875
- ( ) => {
2876
- const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` , {
2877
- perMessageDeflate : { threshold : 0 }
2878
- } ) ;
2879
-
2880
- ws . on ( 'open' , ( ) => {
2881
- ws . send ( 'hi' , { compress : true } ) ;
2882
- ws . close ( ) ;
2883
- } ) ;
2884
-
2885
- ws . on ( 'message' , ( message , isBinary ) => {
2886
- assert . deepStrictEqual ( message , Buffer . from ( 'hi' ) ) ;
2887
- assert . ok ( ! isBinary ) ;
2888
- wss . close ( done ) ;
2889
- } ) ;
2890
- }
2891
- ) ;
2892
-
2893
- wss . on ( 'connection' , ( ws ) => {
2894
- ws . on ( 'message' , ( message , isBinary ) => {
2895
- ws . send ( message , { binary : isBinary , compress : true } ) ;
2896
- } ) ;
2897
- } ) ;
2898
- } ) ;
2899
-
2900
- it ( 'can send and receive a `TypedArray`' , ( done ) => {
2901
- const array = new Float32Array ( 5 ) ;
2902
-
2903
- for ( let i = 0 ; i < array . length ; i ++ ) {
2904
- array [ i ] = i / 2 ;
2905
- }
2906
-
2907
- const wss = new WebSocket . Server (
2908
- {
2909
- perMessageDeflate : { threshold : 0 } ,
2910
- port : 0
2911
- } ,
2912
- ( ) => {
2913
- const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` , {
2914
- perMessageDeflate : { threshold : 0 }
2915
- } ) ;
2916
-
2917
- ws . on ( 'open' , ( ) => {
2918
- ws . send ( array , { compress : true } ) ;
2919
- ws . close ( ) ;
2920
- } ) ;
2921
-
2922
- ws . on ( 'message' , ( message , isBinary ) => {
2923
- assert . deepStrictEqual ( message , Buffer . from ( array . buffer ) ) ;
2924
- assert . ok ( isBinary ) ;
2925
- wss . close ( done ) ;
2926
- } ) ;
2927
- }
2928
- ) ;
2929
-
2930
- wss . on ( 'connection' , ( ws ) => {
2931
- ws . on ( 'message' , ( message , isBinary ) => {
2932
- assert . ok ( isBinary ) ;
2933
- ws . send ( message , { compress : true } ) ;
2934
- } ) ;
2935
- } ) ;
2936
- } ) ;
2937
-
2938
- it ( 'can send and receive an `ArrayBuffer`' , ( done ) => {
2939
- const array = new Float32Array ( 5 ) ;
2940
-
2941
- for ( let i = 0 ; i < array . length ; i ++ ) {
2942
- array [ i ] = i / 2 ;
2943
- }
2944
-
2945
- const wss = new WebSocket . Server (
2946
- {
2947
- perMessageDeflate : { threshold : 0 } ,
2948
- port : 0
2949
- } ,
2950
- ( ) => {
2951
- const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` , {
2952
- perMessageDeflate : { threshold : 0 }
2953
- } ) ;
2954
-
2955
- ws . on ( 'open' , ( ) => {
2956
- ws . send ( array . buffer , { compress : true } ) ;
2957
- ws . close ( ) ;
2958
- } ) ;
2959
-
2960
- ws . on ( 'message' , ( message , isBinary ) => {
2961
- assert . deepStrictEqual ( message , Buffer . from ( array . buffer ) ) ;
2962
- assert . ok ( isBinary ) ;
2963
- wss . close ( done ) ;
2964
- } ) ;
2965
- }
2966
- ) ;
2967
-
2968
- wss . on ( 'connection' , ( ws ) => {
2969
- ws . on ( 'message' , ( message , isBinary ) => {
2970
- assert . ok ( isBinary ) ;
2971
- ws . send ( message , { compress : true } ) ;
2972
- } ) ;
2973
- } ) ;
2974
- } ) ;
2975
-
2976
2869
it ( 'consumes all received data when connection is closed (1/2)' , ( done ) => {
2977
2870
const wss = new WebSocket . Server (
2978
2871
{
@@ -3167,6 +3060,113 @@ describe('WebSocket', () => {
3167
3060
} ) ;
3168
3061
3169
3062
describe ( '#send' , ( ) => {
3063
+ it ( 'can send text data' , ( done ) => {
3064
+ const wss = new WebSocket . Server (
3065
+ {
3066
+ perMessageDeflate : { threshold : 0 } ,
3067
+ port : 0
3068
+ } ,
3069
+ ( ) => {
3070
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` , {
3071
+ perMessageDeflate : { threshold : 0 }
3072
+ } ) ;
3073
+
3074
+ ws . on ( 'open' , ( ) => {
3075
+ ws . send ( 'hi' , { compress : true } ) ;
3076
+ ws . close ( ) ;
3077
+ } ) ;
3078
+
3079
+ ws . on ( 'message' , ( message , isBinary ) => {
3080
+ assert . deepStrictEqual ( message , Buffer . from ( 'hi' ) ) ;
3081
+ assert . ok ( ! isBinary ) ;
3082
+ wss . close ( done ) ;
3083
+ } ) ;
3084
+ }
3085
+ ) ;
3086
+
3087
+ wss . on ( 'connection' , ( ws ) => {
3088
+ ws . on ( 'message' , ( message , isBinary ) => {
3089
+ ws . send ( message , { binary : isBinary , compress : true } ) ;
3090
+ } ) ;
3091
+ } ) ;
3092
+ } ) ;
3093
+
3094
+ it ( 'can send a `TypedArray`' , ( done ) => {
3095
+ const array = new Float32Array ( 5 ) ;
3096
+
3097
+ for ( let i = 0 ; i < array . length ; i ++ ) {
3098
+ array [ i ] = i / 2 ;
3099
+ }
3100
+
3101
+ const wss = new WebSocket . Server (
3102
+ {
3103
+ perMessageDeflate : { threshold : 0 } ,
3104
+ port : 0
3105
+ } ,
3106
+ ( ) => {
3107
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` , {
3108
+ perMessageDeflate : { threshold : 0 }
3109
+ } ) ;
3110
+
3111
+ ws . on ( 'open' , ( ) => {
3112
+ ws . send ( array , { compress : true } ) ;
3113
+ ws . close ( ) ;
3114
+ } ) ;
3115
+
3116
+ ws . on ( 'message' , ( message , isBinary ) => {
3117
+ assert . deepStrictEqual ( message , Buffer . from ( array . buffer ) ) ;
3118
+ assert . ok ( isBinary ) ;
3119
+ wss . close ( done ) ;
3120
+ } ) ;
3121
+ }
3122
+ ) ;
3123
+
3124
+ wss . on ( 'connection' , ( ws ) => {
3125
+ ws . on ( 'message' , ( message , isBinary ) => {
3126
+ assert . ok ( isBinary ) ;
3127
+ ws . send ( message , { compress : true } ) ;
3128
+ } ) ;
3129
+ } ) ;
3130
+ } ) ;
3131
+
3132
+ it ( 'can send an `ArrayBuffer`' , ( done ) => {
3133
+ const array = new Float32Array ( 5 ) ;
3134
+
3135
+ for ( let i = 0 ; i < array . length ; i ++ ) {
3136
+ array [ i ] = i / 2 ;
3137
+ }
3138
+
3139
+ const wss = new WebSocket . Server (
3140
+ {
3141
+ perMessageDeflate : { threshold : 0 } ,
3142
+ port : 0
3143
+ } ,
3144
+ ( ) => {
3145
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` , {
3146
+ perMessageDeflate : { threshold : 0 }
3147
+ } ) ;
3148
+
3149
+ ws . on ( 'open' , ( ) => {
3150
+ ws . send ( array . buffer , { compress : true } ) ;
3151
+ ws . close ( ) ;
3152
+ } ) ;
3153
+
3154
+ ws . on ( 'message' , ( message , isBinary ) => {
3155
+ assert . deepStrictEqual ( message , Buffer . from ( array . buffer ) ) ;
3156
+ assert . ok ( isBinary ) ;
3157
+ wss . close ( done ) ;
3158
+ } ) ;
3159
+ }
3160
+ ) ;
3161
+
3162
+ wss . on ( 'connection' , ( ws ) => {
3163
+ ws . on ( 'message' , ( message , isBinary ) => {
3164
+ assert . ok ( isBinary ) ;
3165
+ ws . send ( message , { compress : true } ) ;
3166
+ } ) ;
3167
+ } ) ;
3168
+ } ) ;
3169
+
3170
3170
it ( 'ignores the `compress` option if the extension is disabled' , ( done ) => {
3171
3171
const wss = new WebSocket . Server ( { port : 0 } , ( ) => {
3172
3172
const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` , {
0 commit comments