@@ -22,41 +22,22 @@ public class SocketIOTest
2222 {
2323 [ TestMethod ]
2424 [ DynamicData ( nameof ( ConnectCases ) ) ]
25- public async Task Should_be_able_to_connect ( SocketIOOptions options , string [ ] res )
25+ public async Task Should_be_able_to_connect ( SocketIOOptions options , string [ ] messages )
2626 {
2727 int i = - 1 ;
2828 var mockHttp = new Mock < IHttpClient > ( ) ;
2929 mockHttp
3030 . Setup ( m => m . SendAsync ( It . IsAny < HttpRequestMessage > ( ) , It . IsAny < CancellationToken > ( ) ) )
31- . ReturnsAsync ( ( ) =>
32- {
33- var text = GetResponseText ( ref i , res ) ;
34- return new HttpResponseMessage
35- {
36- StatusCode = HttpStatusCode . OK ,
37- Content = new StringContent ( text , new MediaTypeHeaderValue ( "text/pain" ) ) ,
38- } ;
39- } ) ;
31+ . ReturnsAsync ( ( ) => GetResponseMessage ( ref i , messages ) ) ;
4032 var mockWs = new Mock < IClientWebSocket > ( ) ;
4133 mockWs
4234 . Setup ( w => w . ConnectAsync ( It . IsAny < Uri > ( ) , It . IsAny < CancellationToken > ( ) ) )
4335 . Callback ( ( ) => mockWs . SetupGet ( w => w . State ) . Returns ( WebSocketState . Open ) ) ;
4436 mockWs
4537 . Setup ( w => w . ReceiveAsync ( It . IsAny < int > ( ) , It . IsNotIn ( CancellationToken . None ) ) )
46- . ReturnsAsync ( ( ) =>
47- {
48- var text = GetResponseText ( ref i , res ) ;
49- var bytes = Encoding . UTF8 . GetBytes ( text ) ;
50- return new WebSocketReceiveResult
51- {
52- MessageType = TransportMessageType . Text ,
53- Buffer = bytes ,
54- EndOfMessage = true ,
55- Count = bytes . Length ,
56- } ;
57- } ) ;
38+ . ReturnsAsync ( ( ) => GetWebSocketResult ( ref i , messages ) ) ;
5839 using var io = new SocketIO ( "http://localhost:11002" , options ) ;
59- io . HttpClientProvider = ( ) => mockHttp . Object ;
40+ io . HttpClientProvider = ( ) => mockHttp . Object ;
6041 io . ClientWebSocketProvider = ( ) => mockWs . Object ;
6142
6243 await io . ConnectAsync ( ) ;
@@ -125,6 +106,29 @@ private static string GetResponseText(ref int i, string[] messages)
125106 return string . Empty ;
126107 }
127108
109+ private static HttpResponseMessage GetResponseMessage ( ref int i , string [ ] messages )
110+ {
111+ string text = GetResponseText ( ref i , messages ) ;
112+ return new HttpResponseMessage
113+ {
114+ StatusCode = HttpStatusCode . OK ,
115+ Content = new StringContent ( text , new MediaTypeHeaderValue ( "text/pain" ) ) ,
116+ } ;
117+ }
118+
119+ private static WebSocketReceiveResult GetWebSocketResult ( ref int i , string [ ] messages )
120+ {
121+ var text = GetResponseText ( ref i , messages ) ;
122+ var bytes = Encoding . UTF8 . GetBytes ( text ) ;
123+ return new WebSocketReceiveResult
124+ {
125+ MessageType = TransportMessageType . Text ,
126+ Buffer = bytes ,
127+ EndOfMessage = true ,
128+ Count = bytes . Length ,
129+ } ;
130+ }
131+
128132 [ TestMethod ]
129133 [ DynamicData ( nameof ( UpgradeToWebSocketCases ) ) ]
130134 public async Task Should_upgrade_transport ( EngineIO eio , string handshake , string [ ] messages )
@@ -140,25 +144,14 @@ public async Task Should_upgrade_transport(EngineIO eio, string handshake, strin
140144 . Callback ( ( ) => mockWs . SetupGet ( w => w . State ) . Returns ( WebSocketState . Open ) ) ;
141145 mockWs
142146 . Setup ( w => w . ReceiveAsync ( It . IsAny < int > ( ) , It . IsNotIn ( CancellationToken . None ) ) )
143- . ReturnsAsync ( ( ) =>
144- {
145- var text = GetResponseText ( ref i , messages ) ;
146- var bytes = Encoding . UTF8 . GetBytes ( text ) ;
147- return new WebSocketReceiveResult
148- {
149- MessageType = TransportMessageType . Text ,
150- Buffer = bytes ,
151- EndOfMessage = true ,
152- Count = bytes . Length ,
153- } ;
154- } ) ;
147+ . ReturnsAsync ( ( ) => GetWebSocketResult ( ref i , messages ) ) ;
155148 using var io = new SocketIO ( "http://localhost:11002" , new SocketIOOptions
156149 {
157150 AutoUpgrade = true ,
158151 Transport = TransportProtocol . Polling ,
159152 EIO = eio ,
160153 } ) ;
161- io . HttpClientProvider = ( ) => mockHttp . Object ;
154+ io . HttpClientProvider = ( ) => mockHttp . Object ;
162155 io . ClientWebSocketProvider = ( ) => mockWs . Object ;
163156
164157 await io . ConnectAsync ( ) ;
@@ -169,7 +162,7 @@ public async Task Should_upgrade_transport(EngineIO eio, string handshake, strin
169162
170163 private static IEnumerable < object [ ] > UpgradeToWebSocketCases =>
171164 UpgradeToWebSocketTupleCases . Select ( x => new object [ ] { x . eio , x . handshake , x . messages } ) ;
172-
165+
173166 private static IEnumerable < ( EngineIO eio , string handshake , string [ ] messages ) > UpgradeToWebSocketTupleCases
174167 {
175168 get
@@ -223,28 +216,21 @@ public async Task Should_not_upgrade_transport(SocketIOOptions options, string h
223216 . ReturnsAsync ( handshake ) ;
224217 mockHttp
225218 . Setup ( m => m . SendAsync ( It . IsAny < HttpRequestMessage > ( ) , It . IsAny < CancellationToken > ( ) ) )
226- . ReturnsAsync ( ( ) =>
227- {
228- var text = GetResponseText ( ref i , messages ) ;
229- return new HttpResponseMessage
230- {
231- StatusCode = HttpStatusCode . OK ,
232- Content = new StringContent ( text , new MediaTypeHeaderValue ( "text/pain" ) ) ,
233- } ;
234- } ) ;
219+ . ReturnsAsync ( ( ) => GetResponseMessage ( ref i , messages ) ) ;
235220 using var io = new SocketIO ( "http://localhost:11002" , options ) ;
236- io . HttpClientProvider = ( ) => mockHttp . Object ;
221+ io . HttpClientProvider = ( ) => mockHttp . Object ;
237222
238223 await io . ConnectAsync ( ) ;
239224
240225 io . Connected . Should ( ) . BeTrue ( ) ;
241226 io . Options . Transport . Should ( ) . Be ( TransportProtocol . Polling ) ;
242227 }
243-
244- private static IEnumerable < object [ ] > NotUpgradeToWebSocketCases =>
228+
229+ private static IEnumerable < object [ ] > NotUpgradeToWebSocketCases =>
245230 NotUpgradeToWebSocketTupleCases . Select ( x => new object [ ] { x . options , x . handshake , x . messages } ) ;
246-
247- private static IEnumerable < ( SocketIOOptions options , string handshake , string [ ] messages ) > NotUpgradeToWebSocketTupleCases
231+
232+ private static IEnumerable < ( SocketIOOptions options , string handshake , string [ ] messages ) >
233+ NotUpgradeToWebSocketTupleCases
248234 {
249235 get
250236 {
@@ -323,11 +309,11 @@ public async Task Should_throw_an_exception_when_server_is_unavailable_and_recon
323309 Transport = TransportProtocol . Polling ,
324310 Reconnection = false
325311 } ) ;
326- io . HttpClientProvider = ( ) => mockHttp . Object ;
312+ io . HttpClientProvider = ( ) => mockHttp . Object ;
327313 io . ClientWebSocketProvider = ( ) => mockWs . Object ;
328314
329315 await io
330- . Invoking ( async x=> await x . ConnectAsync ( ) )
316+ . Invoking ( async x => await x . ConnectAsync ( ) )
331317 . Should ( )
332318 . ThrowAsync < ConnectionException > ( )
333319 . WithMessage ( "Cannot connect to server '*'" ) ;
0 commit comments