public void ReadAsyncThrowsWhenSocketNotOpen()
        {
            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(TransportAsyncCallbackArgs callbackArgs) { };
            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.ReadAsync(args));
        }
        public void ReadAsyncThrowsWhenBufferLimits()
        {
            var webSocket = new Mock <WebSocket>();

            webSocket.Setup(ws => ws.State)
            .Returns(WebSocketState.Open);

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

            var args = new TransportAsyncCallbackArgs();

            args.SetBuffer(new byte[4], -1, 4);
            args.CompletedCallback += delegate { };
            Assert.Throws <ArgumentOutOfRangeException>(() => serverTransport.ReadAsync(args));

            args.SetBuffer(new byte[4], 5, 9);
            Assert.Throws <ArgumentOutOfRangeException>(() => serverTransport.ReadAsync(args));

            args.SetBuffer(new byte[4], 2, -1);
            Assert.Throws <ArgumentOutOfRangeException>(() => serverTransport.ReadAsync(args));

            args.SetBuffer(new byte[4], 2, 3);
            Assert.Throws <ArgumentOutOfRangeException>(() => serverTransport.ReadAsync(args));
        }
        public void ReadAsyncThrowsWhenBufferIsNull()
        {
            var webSocket       = new Mock <WebSocket>();
            var serverTransport = new ServerWebSocketTransport(webSocket.Object, "local", "remote", Guid.NewGuid().ToString());

            Assert.Throws <ArgumentNullException>(() => serverTransport.ReadAsync(new TransportAsyncCallbackArgs()));
        }
        public void ReadAsyncThrowsWhenCompletedCallbackIsNull()
        {
            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.ReadAsync(args));
        }
        public void ReadAsyncSuccess()
        {
            var webSocket = new Mock <WebSocket>();

            webSocket.Setup(ws => ws.State)
            .Returns(WebSocketState.Open);
            webSocket.Setup(ws => ws.ReceiveAsync(It.IsAny <ArraySegment <byte> >(), 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 byte[4], 0, 4);
            args.CompletedCallback += delegate { };

            Assert.False(serverTransport.ReadAsync(args));
            webSocket.VerifyAll();
        }