Esempio n. 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 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();
            }
        }