public void WriteAsyncThrowsWhenSocketNotOpen()
        {
            var webSocket = new Mock <WebSocket>();

            webSocket.SetupSequence(ws => ws.State)
            .Returns(WebSocketState.Aborted)
            .Returns(WebSocketState.Closed)
            .Returns(WebSocketState.CloseSent)
            .Returns(WebSocketState.CloseReceived)
            .Returns(WebSocketState.None);
            var serverTransport = new ServerWebSocketTransport(webSocket.Object,
                                                               "local",
                                                               "remote",
                                                               Guid.NewGuid().ToString());

            var args = new TransportAsyncCallbackArgs();

            args.SetBuffer(new byte[4], 0, 4);
            args.CompletedCallback += delegate { };
            Assert.Throws <ObjectDisposedException>(() => serverTransport.ReadAsync(args));
            Assert.Throws <ObjectDisposedException>(() => serverTransport.ReadAsync(args));
            Assert.Throws <ObjectDisposedException>(() => serverTransport.ReadAsync(args));
            Assert.Throws <ObjectDisposedException>(() => serverTransport.ReadAsync(args));
            Assert.Throws <AmqpException>(() => serverTransport.WriteAsync(args));
        }
        public void WriteAsyncThrowsWhenBufferIsNull()
        {
            var webSocket       = new Mock <WebSocket>();
            var serverTransport = new ServerWebSocketTransport(webSocket.Object, "local", "remote", Guid.NewGuid().ToString());

            Assert.Throws <ArgumentException>(() => serverTransport.WriteAsync(new TransportAsyncCallbackArgs()));
        }
        public void WriteBufferListAsyncSuccess()
        {
            var webSocket = new Mock <WebSocket>();

            webSocket.Setup(ws => ws.State)
            .Returns(WebSocketState.Open);
            webSocket.Setup(ws => ws.SendAsync(It.IsAny <ArraySegment <byte> >(), It.IsAny <WebSocketMessageType>(), It.IsAny <bool>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new WebSocketReceiveResult(2, WebSocketMessageType.Text, true)));

            var serverTransport = new ServerWebSocketTransport(
                webSocket.Object,
                "local",
                "remote",
                Guid.NewGuid().ToString());

            var args = new TransportAsyncCallbackArgs();

            args.SetBuffer(new List <ByteBuffer> {
                new ByteBuffer(new byte[4]), new ByteBuffer(new byte[5])
            });
            args.CompletedCallback += obj => { };

            Assert.False(serverTransport.WriteAsync(args));
            webSocket.Verify(ws => ws.SendAsync(It.IsAny <ArraySegment <byte> >(), It.IsAny <WebSocketMessageType>(), It.IsAny <bool>(), It.IsAny <CancellationToken>()), Times.Exactly(2));
        }
        public void WriteAsyncThrowsWhenCompletedCallbackIsNull()
        {
            var webSocket       = new Mock <WebSocket>();
            var serverTransport = new ServerWebSocketTransport(webSocket.Object, "local", "remote", Guid.NewGuid().ToString());

            var args = new TransportAsyncCallbackArgs();

            args.SetBuffer(new byte[4], 0, 4);
            Assert.Throws <ArgumentNullException>(() => serverTransport.WriteAsync(args));
        }