Example #1
0
        public void ReadTwoBufferedSmallFrames()
        {
            var handshake = this.GenerateSimpleHandshake();

            using (var connectionInput = new MemoryStream())
                using (var connectionOutput = new MemoryStream())
                    using (var connection = new DummyNetworkConnection(connectionInput, connectionOutput))
                        using (var ws = new WebSocketRfc6455(connection, this.options, handshake.Request, handshake.Response, handshake.NegotiatedMessageExtensions))
                        {
                            connectionInput.Write(new byte[] { 129, 130, 75, 91, 80, 26, 3, 50 }, 0, 8);
                            connectionInput.Write(new byte[] { 129, 130, 75, 91, 80, 26, 3, 50 }, 0, 8);
                            connectionInput.Flush();
                            connectionInput.Seek(0, SeekOrigin.Begin);

                            var reader = ws.ReadMessageAsync(CancellationToken.None).Result;
                            Assert.NotNull(reader);
                            using (var sr = new StreamReader(reader, Encoding.UTF8, true, 1024, true))
                            {
                                var s = sr.ReadToEnd();
                                Assert.Equal("Hi", s);
                            }

                            reader = ws.ReadMessageAsync(CancellationToken.None).Result;
                            Assert.NotNull(reader);
                            using (var sr = new StreamReader(reader, Encoding.UTF8, true, 1024, true))
                            {
                                var s = sr.ReadToEndAsync().Result;
                                Assert.Equal("Hi", s);
                            }

                            reader = ws.ReadMessageAsync(CancellationToken.None).Result;
                            Assert.Null(reader);
                        }
        }
Example #2
0
        public void With_WebSocket_CanReadTwoBufferedSmallFrames()
        {
            var handshake = GenerateSimpleHandshake();

            using (var ms = new MemoryStream())
                using (WebSocket ws = new WebSocketRfc6455(ms, new WebSocketListenerOptions()
                {
                    PingTimeout = Timeout.InfiniteTimeSpan
                }, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2), handshake.Request, handshake.Response, handshake.NegotiatedMessageExtensions))
                {
                    ms.Write(new Byte[] { 129, 130, 75, 91, 80, 26, 3, 50 }, 0, 8);
                    ms.Write(new Byte[] { 129, 130, 75, 91, 80, 26, 3, 50 }, 0, 8);
                    ms.Flush();
                    ms.Seek(0, SeekOrigin.Begin);

                    var reader = ws.ReadMessageAsync(CancellationToken.None).Result;
                    Assert.IsNotNull(reader);
                    using (var sr = new StreamReader(reader, Encoding.UTF8, true, 1024, true))
                    {
                        String s = sr.ReadToEnd();
                        Assert.AreEqual("Hi", s);
                    }

                    reader = ws.ReadMessageAsync(CancellationToken.None).Result;
                    Assert.IsNotNull(reader);
                    using (var sr = new StreamReader(reader, Encoding.UTF8, true, 1024, true))
                    {
                        String s = sr.ReadToEndAsync().Result;
                        Assert.AreEqual("Hi", s);
                    }

                    reader = ws.ReadMessageAsync(CancellationToken.None).Result;
                    Assert.IsNull(reader);
                }
        }
Example #3
0
        public void ReadEmptyMessage()
        {
            var handshake = this.GenerateSimpleHandshake();

            using (var connectionInput = new MemoryStream())
                using (var connectionOutput = new MemoryStream())
                    using (var connection = new DummyNetworkConnection(connectionInput, connectionOutput))
                        using (var ws = new WebSocketRfc6455(connection, this.options, handshake.Request, handshake.Response, handshake.NegotiatedMessageExtensions))
                        {
                            connectionInput.Write(new byte[]
                            {
                                129, 128, 166, 124, 106, 65
                            }, 0, 6);
                            connectionInput.Flush();
                            connectionInput.Seek(0, SeekOrigin.Begin);

                            var reader = ws.ReadMessageAsync(CancellationToken.None).Result;
                            Assert.NotNull(reader);
                            using (var sr = new StreamReader(reader, Encoding.UTF8, true, 1024, true))
                            {
                                var s = sr.ReadToEnd();
                                Assert.Equal(string.Empty, s);
                            }
                        }
        }
Example #4
0
        public async Task With_WebSocket_FailsWithDoubleMessageAwait()
        {
            var handshake = GenerateSimpleHandshake();

            using (var ms = new BufferedStream(new MemoryStream()))
                using (WebSocket ws = new WebSocketRfc6455(ms, new WebSocketListenerOptions()
                {
                    PingTimeout = Timeout.InfiniteTimeSpan
                }, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2), handshake.Request, handshake.Response, handshake.NegotiatedMessageExtensions))
                {
                    ms.Write(new Byte[] { 129, 130, 75, 91, 80, 26, 3, 50 }, 0, 8);
                    ms.Write(new Byte[] { 129, 130, 75, 91, 80, 26, 3, 50 }, 0, 8);
                    ms.Flush();
                    ms.Seek(0, SeekOrigin.Begin);

                    await ws.ReadMessageAsync(CancellationToken.None);

                    await ws.ReadMessageAsync(CancellationToken.None);
                }
        }
Example #5
0
        public async Task FailDoubleMessageAwait()
        {
            var handshake = this.GenerateSimpleHandshake();
            await Assert.ThrowsAsync <WebSocketException>(async() =>
            {
                using (var connectionInput = new BufferedStream(new MemoryStream()))
                    using (var connectionOutput = new MemoryStream())
                        using (var connection = new DummyNetworkConnection(connectionInput, connectionOutput))
                            using (var ws = new WebSocketRfc6455(connection, this.options, handshake.Request, handshake.Response, handshake.NegotiatedMessageExtensions))
                            {
                                connectionInput.Write(new byte[] { 129, 130, 75, 91, 80, 26, 3, 50 }, 0, 8);
                                connectionInput.Write(new byte[] { 129, 130, 75, 91, 80, 26, 3, 50 }, 0, 8);
                                connectionInput.Flush();
                                connectionInput.Seek(0, SeekOrigin.Begin);

                                await ws.ReadMessageAsync(CancellationToken.None).ConfigureAwait(false);
                                await ws.ReadMessageAsync(CancellationToken.None).ConfigureAwait(false);
                            }
            }).ConfigureAwait(false);
        }
Example #6
0
        public void With_WebSocket_CanDetectHalfOpenConnection()
        {
            var handshake = GenerateSimpleHandshake();

            using (var ms = new MemoryStream())
                using (WebSocket ws = new WebSocketRfc6455(ms, new WebSocketListenerOptions()
                {
                    PingTimeout = TimeSpan.FromMilliseconds(100)
                }, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2), handshake.Request, handshake.Response, handshake.NegotiatedMessageExtensions))
                {
                    ws.ReadMessageAsync(CancellationToken.None);
                    // DateTime has no millisecond precission.
                    Thread.Sleep(500);
                    Assert.IsFalse(ws.IsConnected);
                }
        }
Example #7
0
        public void DetectHalfOpenConnection()
        {
            var handshake = this.GenerateSimpleHandshake();
            var options   = this.options.Clone();

            options.PingTimeout = TimeSpan.FromMilliseconds(100);

            using (var connectionInput = new MemoryStream())
                using (var connectionOutput = new MemoryStream())
                    using (var connection = new DummyNetworkConnection(connectionInput, connectionOutput))
                        using (var ws = new WebSocketRfc6455(connection, options, handshake.Request,
                                                             handshake.Response, handshake.NegotiatedMessageExtensions))
                        {
                            ws.ReadMessageAsync(CancellationToken.None);

                            // DateTime has no millisecond precision.
                            Thread.Sleep(500);
                            Assert.False(ws.IsConnected);
                        }
        }