Пример #1
0
        public void TestSimpleGetHttps()
        {
            ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); });

            HttpEchoServer server = new HttpEchoServer(pool);

            try
            {
                server.Start(true);

                BlockingCollection <HttpResponse> responses = new BlockingCollection <HttpResponse>();

                ClientSockNetChannel client = (ClientSockNetChannel)SockNetClient.Create(server.Endpoint, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool)
                                              .AddModule(new HttpSockNetChannelModule(HttpSockNetChannelModule.ParsingMode.Client));
                client.Pipe.AddIncomingLast <HttpResponse>((ISockNetChannel channel, ref HttpResponse data) => { responses.Add(data); });
                client.ConnectWithTLS((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); })
                .WaitForValue(TimeSpan.FromSeconds(5));

                HttpRequest request = new HttpRequest(client.BufferPool)
                {
                    Action  = "GET",
                    Path    = "/",
                    Version = "HTTP/1.1"
                };
                request.Header["Host"]       = "localhost";
                request.Header["Connection"] = "Close";

                client.Send(request);

                HttpResponse response = null;
                responses.TryTake(out response, 5000);

                Assert.IsNotNull(response);

                Assert.IsNotNull(response.Version);
                Assert.IsNotNull(response.Code);
                Assert.IsNotNull(response.Reason);

                MemoryStream stream = new MemoryStream();
                response.Write(stream, false);
                stream.Position = 0;

                using (StreamReader reader = new StreamReader(stream))
                {
                    Console.WriteLine("Got response: " + reader.ReadToEnd());
                }
            }
            finally
            {
                server.Stop();
            }
        }
        public void TestSimpleSslContent()
        {
            ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); });

            GdsEchoServer server = new GdsEchoServer(pool);

            try
            {
                server.Start(true);

                BlockingCollection <object> blockingCollection = new BlockingCollection <object>();

                ClientSockNetChannel client = (ClientSockNetChannel)SockNetClient.Create(server.Endpoint, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool)
                                              .AddModule(new GdsSockNetChannelModule(true));

                client.ConnectWithTLS((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); })
                .WaitForValue(TimeSpan.FromSeconds(5));

                object currentObject;

                client.Pipe.AddIncomingLast <GdsFrame>((ISockNetChannel sockNetClient, ref GdsFrame data) => { blockingCollection.Add(data); });

                ChunkedBuffer body = new ChunkedBuffer(pool);
                body.OfferRaw(Encoding.UTF8.GetBytes("some test"), 0, Encoding.UTF8.GetByteCount("some test"));

                client.Send(GdsFrame.NewContentFrame(1, null, false, body, true));

                Assert.IsTrue(blockingCollection.TryTake(out currentObject, 5000));
                Assert.IsTrue(currentObject is GdsFrame);

                Assert.AreEqual("some test", ((GdsFrame)currentObject).Body.ToString(Encoding.UTF8));

                Console.WriteLine("Got response: \n" + ((GdsFrame)currentObject).Body);

                client.Disconnect().WaitForValue(TimeSpan.FromSeconds(5));
            }
            finally
            {
                server.Stop();
            }
        }
        public void TestEchoWithMaskWithSsl()
        {
            ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); });

            WebSocketEchoServer server = new WebSocketEchoServer(pool);

            try
            {
                server.Start(true);

                BlockingCollection <object> blockingCollection = new BlockingCollection <object>();

                ClientSockNetChannel client = (ClientSockNetChannel)SockNetClient.Create(server.Endpoint, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool)
                                              .AddModule(new WebSocketClientSockNetChannelModule("/", "localhost", (ISockNetChannel sockNetClient) => { blockingCollection.Add(true); }));

                client.ConnectWithTLS((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); })
                .WaitForValue(TimeSpan.FromSeconds(5));

                object currentObject;

                Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT));
                Assert.IsTrue((bool)currentObject);

                client.Pipe.AddIncomingLast <WebSocketFrame>((ISockNetChannel sockNetClient, ref WebSocketFrame data) => { blockingCollection.Add(data); });

                client.Send(WebSocketFrame.CreateTextFrame("some test", true));

                Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT));
                Assert.IsTrue(currentObject is WebSocketFrame);

                Assert.AreEqual("some test", ((WebSocketFrame)currentObject).DataAsString);

                Console.WriteLine("Got response: \n" + ((WebSocketFrame)currentObject).DataAsString);

                client.Disconnect().WaitForValue(TimeSpan.FromSeconds(5));
            }
            finally
            {
                server.Stop();
            }
        }