Inheritance: ClientTransportBase
        public void ExceptionCaughtAndReportedIfThrownWhenGettingDataReader()
        {
            var exception = new Exception();

            var fakeWebSocketResponse = new FakeWebSocketResponse();
            fakeWebSocketResponse.Setup<IDataReader>("GetDataReader", () => { throw exception; });

            var transport = new WebSocketTransport();
            transport.Start(
                new FakeConnection
                {
                    TransportConnectTimeout = new TimeSpan(0, 0, 0, 0, 100)
                },
                string.Empty, CancellationToken.None);

            var fakeConnection = new FakeConnection();
            transport.MessageReceived(fakeWebSocketResponse, fakeConnection);

            fakeConnection.Verify("OnError", new List<object[]> { new object[] { exception } });
        }
Ejemplo n.º 2
0
        public void WebSocketSendReturnsAFaultedTaskWhenNotConnected(WebSocketState state)
        {
            var mockConnection = new Mock<Client.IConnection>(MockBehavior.Strict);
            var mockWebSocket = new Mock<WebSocket>(MockBehavior.Strict);
            var mockWebSocketHandler = new Mock<ClientWebSocketHandler>();

            mockWebSocket.SetupGet(ws => ws.State).Returns(state);
            mockConnection.Setup(c => c.OnError(It.IsAny<InvalidOperationException>()));
            mockWebSocketHandler.Object.WebSocket = mockWebSocket.Object;
            
            var wsTransport = new WebSocketTransport(mockWebSocketHandler.Object);

            var task = wsTransport.Send(mockConnection.Object, "", "");

            Assert.True(task.IsFaulted);
            Assert.IsType(typeof(InvalidOperationException), task.Exception.InnerException);

            mockConnection.VerifyAll();
            mockWebSocket.VerifyAll();
        }
        public void MessageReceivedReadsAndProcessesMessages()
        {
            var fakeDataReader = new FakeDataReader
            {
                UnicodeEncoding = (UnicodeEncoding)(-1),
                UnconsumedBufferLength = 42
            };
            fakeDataReader.Setup("ReadString", () => "MessageBody");

            var fakeWebSocketResponse = new FakeWebSocketResponse();
            fakeWebSocketResponse.Setup("GetDataReader", () => fakeDataReader);

            var transport = new WebSocketTransport();
            transport.Start(new FakeConnection(), string.Empty, CancellationToken.None);

            var fakeConnection = new FakeConnection();
            transport.MessageReceived(fakeWebSocketResponse, fakeConnection);

            Assert.Equal(UnicodeEncoding.Utf8, fakeDataReader.UnicodeEncoding);
            fakeDataReader.Verify("ReadString", new List<object[]> {new object[] { 42u}});

            // invoked by ProcessResponse
            Assert.Equal(1, fakeConnection.GetInvocations("MarkLastMessage").Count());
            Assert.Equal(1, fakeConnection.GetInvocations("Trace").Count());
        }