3
3
using ReactNative . Bridge ;
4
4
using ReactNative . Modules . Core ;
5
5
using ReactNative . Modules . WebSocket ;
6
+ using System ;
7
+ using System . Text ;
6
8
using System . Threading ;
7
9
8
10
namespace ReactNative . Tests . Modules . WebSocket
@@ -110,16 +112,104 @@ public void WebSocketModule_DataEvent()
110
112
json = ( JObject ) args [ 1 ] ;
111
113
waitHandle . Set ( ) ;
112
114
break ;
113
- default :
115
+ }
116
+ } ) ) ;
117
+
118
+ var module = new WebSocketModule ( context ) ;
119
+ try
120
+ {
121
+ module . connect ( "ws://echo.websocket.org" , null , null , 1 ) ;
122
+ Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
123
+ module . send ( "FooBarBaz" , 1 ) ;
124
+ Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
125
+ }
126
+ finally
127
+ {
128
+ module . close ( 1000 , "None" , 1 ) ;
129
+ Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
130
+
131
+ waitHandle . Dispose ( ) ;
132
+ }
133
+
134
+ Assert . AreEqual ( 1 , json [ "id" ] . Value < int > ( ) ) ;
135
+ Assert . AreEqual ( "text" , json [ "type" ] . Value < string > ( ) ) ;
136
+ Assert . AreEqual ( "FooBarBaz" , json [ "data" ] . Value < string > ( ) ) ;
137
+ }
138
+
139
+ [ Test ]
140
+ [ Category ( "Network" ) ]
141
+ public void WebSocketModule_DataEvent_Binary ( )
142
+ {
143
+ var waitHandle = new AutoResetEvent ( false ) ;
144
+ var json = default ( JObject ) ;
145
+ var context = CreateReactContext ( new MockInvocationHandler ( ( name , args ) =>
146
+ {
147
+ var eventName = ( string ) args [ 0 ] ;
148
+ switch ( eventName )
149
+ {
150
+ case "websocketOpen" :
151
+ case "websocketClosed" :
152
+ waitHandle . Set ( ) ;
153
+ break ;
154
+ case "websocketMessage" :
155
+ json = ( JObject ) args [ 1 ] ;
156
+ waitHandle . Set ( ) ;
114
157
break ;
115
158
}
116
159
} ) ) ;
117
160
118
161
var module = new WebSocketModule ( context ) ;
162
+ var encodedMessage = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( "Hello World" ) ) ;
119
163
try
120
164
{
121
165
module . connect ( "ws://echo.websocket.org" , null , null , 1 ) ;
122
166
Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
167
+ module . sendBinary ( encodedMessage , 1 ) ;
168
+ Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
169
+ }
170
+ finally
171
+ {
172
+ module . close ( 1000 , "None" , 1 ) ;
173
+ Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
174
+
175
+ waitHandle . Dispose ( ) ;
176
+ }
177
+
178
+ Assert . AreEqual ( 1 , json [ "id" ] . Value < int > ( ) ) ;
179
+ Assert . AreEqual ( "binary" , json [ "type" ] . Value < string > ( ) ) ;
180
+ Assert . AreEqual ( encodedMessage , json [ "data" ] . Value < string > ( ) ) ;
181
+ }
182
+
183
+ [ Test ]
184
+ [ Category ( "Network" ) ]
185
+ public void WebSocketModule_DataEvent_Binary_ThenText ( )
186
+ {
187
+ var waitHandle = new AutoResetEvent ( false ) ;
188
+ var json = default ( JObject ) ;
189
+ var context = CreateReactContext ( new MockInvocationHandler ( ( name , args ) =>
190
+ {
191
+ var eventName = ( string ) args [ 0 ] ;
192
+ switch ( eventName )
193
+ {
194
+ case "websocketOpen" :
195
+ case "websocketClosed" :
196
+ waitHandle . Set ( ) ;
197
+ break ;
198
+ case "websocketMessage" :
199
+ json = ( JObject ) args [ 1 ] ;
200
+ waitHandle . Set ( ) ;
201
+ break ;
202
+ }
203
+ } ) ) ;
204
+
205
+ var module = new WebSocketModule ( context ) ;
206
+ var encodedMessage = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( "Hello World" ) ) ;
207
+ try
208
+ {
209
+ module . connect ( "ws://echo.websocket.org" , null , null , 1 ) ;
210
+ Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
211
+ module . sendBinary ( encodedMessage , 1 ) ;
212
+ Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
123
213
module . send ( "FooBarBaz" , 1 ) ;
124
214
Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
125
215
}
@@ -132,9 +222,56 @@ public void WebSocketModule_DataEvent()
132
222
}
133
223
134
224
Assert . AreEqual ( 1 , json [ "id" ] . Value < int > ( ) ) ;
225
+ Assert . AreEqual ( "text" , json [ "type" ] . Value < string > ( ) ) ;
135
226
Assert . AreEqual ( "FooBarBaz" , json [ "data" ] . Value < string > ( ) ) ;
136
227
}
137
228
229
+ [ Test ]
230
+ [ Category ( "Network" ) ]
231
+ public void WebSocketModule_DataEvent_Text_ThenBinary ( )
232
+ {
233
+ var waitHandle = new AutoResetEvent ( false ) ;
234
+ var json = default ( JObject ) ;
235
+ var context = CreateReactContext ( new MockInvocationHandler ( ( name , args ) =>
236
+ {
237
+ var eventName = ( string ) args [ 0 ] ;
238
+ switch ( eventName )
239
+ {
240
+ case "websocketOpen" :
241
+ case "websocketClosed" :
242
+ waitHandle . Set ( ) ;
243
+ break ;
244
+ case "websocketMessage" :
245
+ json = ( JObject ) args [ 1 ] ;
246
+ waitHandle . Set ( ) ;
247
+ break ;
248
+ }
249
+ } ) ) ;
250
+
251
+ var module = new WebSocketModule ( context ) ;
252
+ var encodedMessage = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( "Hello World" ) ) ;
253
+ try
254
+ {
255
+ module . connect ( "ws://echo.websocket.org" , null , null , 1 ) ;
256
+ Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
257
+ module . send ( "FooBarBaz" , 1 ) ;
258
+ Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
259
+ module . sendBinary ( encodedMessage , 1 ) ;
260
+ Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
261
+ }
262
+ finally
263
+ {
264
+ module . close ( 1000 , "None" , 1 ) ;
265
+ Assert . IsTrue ( waitHandle . WaitOne ( WaitTimeoutInMs ) ) ;
266
+
267
+ waitHandle . Dispose ( ) ;
268
+ }
269
+
270
+ Assert . AreEqual ( 1 , json [ "id" ] . Value < int > ( ) ) ;
271
+ Assert . AreEqual ( "binary" , json [ "type" ] . Value < string > ( ) ) ;
272
+ Assert . AreEqual ( encodedMessage , json [ "data" ] . Value < string > ( ) ) ;
273
+ }
274
+
138
275
private ReactContext CreateReactContext ( IInvocationHandler handler )
139
276
{
140
277
var eventEmitter = new RCTDeviceEventEmitter ( ) ;
0 commit comments