Наследование: Microsoft.AspNet.Server.Kestrel.ServiceContext
        public async Task CanReadAndWriteWithHttpsConnectionFilter()
        {
            RemoteCertificateValidationCallback validationCallback =
                    (sender, cert, chain, sslPolicyErrors) => true;

            try
            {
                ServicePointManager.ServerCertificateValidationCallback += validationCallback;

                var sereverAddress = "https://*****:*****@"TestResources/testCert.pfx", "testPassword"),
                        new NoOpConnectionFilter())
                };

                using (var server = new TestServer(App, serviceContext, sereverAddress))
                {
                    using (var client = new HttpClient())
                    {
                        var result = await client.PostAsync(sereverAddress, new FormUrlEncodedContent(new[] {
                            new KeyValuePair<string, string>("content", "Hello World?")
                        }));

                        Assert.Equal("content=Hello+World%3F", await result.Content.ReadAsStringAsync());
                    }
                }
            }
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback -= validationCallback;
            }
        }
        public async Task CanReadAndWriteWithAsyncConnectionFilter()
        {
            var serviceContext = new TestServiceContext()
            {
                ConnectionFilter = new AsyncConnectionFilter()
            };

            using (var server = new TestServer(App, serviceContext))
            {
                using (var connection = new TestConnection())
                {
                    await connection.SendEnd(
                        "POST / HTTP/1.0",
                        "",
                        "Hello World?");
                    await connection.ReceiveEnd(
                        "HTTP/1.0 200 OK",
                        "",
                        "Hello World!");
                }
            }
       }
        public async Task CanReadAndWriteWithRewritingConnectionFilter()
        {
            var filter = new RewritingConnectionFilter();
            var serviceContext = new TestServiceContext()
            {
                ConnectionFilter = filter
            };
            var sendString = "POST / HTTP/1.0\r\n\r\nHello World?";

            using (var server = new TestServer(App, serviceContext))
            {
                using (var connection = new TestConnection())
                {
                    // "?" changes to "!"
                    await connection.SendEnd(sendString);
                    await connection.ReceiveEnd(
                        "HTTP/1.0 200 OK",
                        "",
                        "Hello World!");
                }
            }

            Assert.Equal(sendString.Length, filter.BytesRead);
        }
        public async Task RequireCertificateFailsWhenNoCertificate()
        {
            RemoteCertificateValidationCallback validationCallback =
                    (sender, cert, chain, sslPolicyErrors) => true;

            try
            {
#if DNX451
                var handler = new HttpClientHandler();
                ServicePointManager.ServerCertificateValidationCallback += validationCallback;
#else
                var handler = new WinHttpHandler();
                handler.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
#endif

                var serverAddress = "https://*****:*****@"TestResources/testCert.pfx", "testPassword"),
                            ClientCertificateMode = ClientCertificateMode.RequireCertificate
                        },
                        new NoOpConnectionFilter())
                };

                using (var server = new TestServer(App, serviceContext, serverAddress))
                {
                    using (var client = new HttpClient(handler))
                    {
                        await Assert.ThrowsAnyAsync<Exception>(
                            () => client.GetAsync(serverAddress));
                    }
                }
            }
            finally
            {
#if DNX451
                ServicePointManager.ServerCertificateValidationCallback -= validationCallback;
#endif
            }
        }
        public async Task HttpsSchemePassedToRequestFeature()
        {
            RemoteCertificateValidationCallback validationCallback =
                    (sender, cert, chain, sslPolicyErrors) => true;

            try
            {
#if DNX451
                var handler = new HttpClientHandler();
                ServicePointManager.ServerCertificateValidationCallback += validationCallback;
#else
                var handler = new WinHttpHandler();
                handler.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
#endif

                var serverAddress = "https://*****:*****@"TestResources/testCert.pfx", "testPassword")
                        },
                        new NoOpConnectionFilter())
                };

                RequestDelegate app = context => context.Response.WriteAsync(context.Request.Scheme);

                using (var server = new TestServer(app, serviceContext, serverAddress))
                {
                    using (var client = new HttpClient(handler))
                    {
                        var result = await client.GetAsync(serverAddress);

                        Assert.Equal("https", await result.Content.ReadAsStringAsync());
                    }
                }
            }
            finally
            {
#if DNX451
                ServicePointManager.ServerCertificateValidationCallback -= validationCallback;
#endif
            }
        }
        public async Task CertificatePassedToHttpContext()
        {
            RemoteCertificateValidationCallback validationCallback =
                    (sender, cert, chain, sslPolicyErrors) => true;

            try
            {
#if DNX451
                ServicePointManager.ServerCertificateValidationCallback += validationCallback;
#endif

                var serverAddress = "https://*****:*****@"TestResources/testCert.pfx", "testPassword"),
                            ClientCertificateMode = ClientCertificateMode.RequireCertificate,
                            ClientCertificateValidation = (certificate, chain, sslPolicyErrors) => true
                        },
                        new NoOpConnectionFilter())
                };

                RequestDelegate app = context =>
                {
                    var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
                    Assert.NotNull(tlsFeature);
                    Assert.NotNull(tlsFeature.ClientCertificate);
                    Assert.NotNull(context.Connection.ClientCertificate);
                    return context.Response.WriteAsync("hello world");
                };

                using (var server = new TestServer(app, serviceContext, serverAddress))
                {
                    // SslStream is used to ensure the certificate is actually passed to the server
                    // HttpClient might not send the certificate because it is invalid or it doesn't match any
                    // of the certificate authorities sent by the server in the SSL handshake.
                    using (var client = new TcpClient())
                    {
                        await client.ConnectAsync("127.0.0.1", 54321);

                        SslStream stream = new SslStream(client.GetStream(), false, (sender, certificate, chain, errors) => true,
                            (sender, host, certificates, certificate, issuers) => new X509Certificate2(@"TestResources/testCert.pfx", "testPassword"));
                        await stream.AuthenticateAsClientAsync("localhost");

                        var request = Encoding.UTF8.GetBytes("GET / HTTP/1.0\r\n\r\n");
                        await stream.WriteAsync(request, 0, request.Length);

                        var reader = new StreamReader(stream);
                        var line = await reader.ReadLineAsync();
                        Assert.Equal("HTTP/1.0 200 OK", line);
                    }
                }
            }
            finally
            {
#if DNX451
                ServicePointManager.ServerCertificateValidationCallback -= validationCallback;
#endif
            }
        }
Пример #7
0
 public void ListenerCanCreateAndDispose(TestServiceContext testContext)
 {
     testContext.App = App;
     var engine = new KestrelEngine(testContext);
     engine.Start(1);
     var address = ServerAddress.FromUrl("http://localhost:54321/");
     var started = engine.CreateServer(address);
     started.Dispose();
     engine.Dispose();
 }
Пример #8
0
        public void ConnectionCanReadAndWrite(TestServiceContext testContext)
        {
            testContext.App = App;
            var engine = new KestrelEngine(testContext);
            engine.Start(1);
            var address = ServerAddress.FromUrl("http://localhost:54321/");
            var started = engine.CreateServer(address);

            Console.WriteLine("Started");
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(new IPEndPoint(IPAddress.Loopback, 54321));
            socket.Send(Encoding.ASCII.GetBytes("POST / HTTP/1.0\r\n\r\nHello World"));
            socket.Shutdown(SocketShutdown.Send);
            var buffer = new byte[8192];
            while (true)
            {
                var length = socket.Receive(buffer);
                if (length == 0) { break; }
                var text = Encoding.ASCII.GetString(buffer, 0, length);
            }
            started.Dispose();
            engine.Dispose();
        }
        public async Task ThrowingSynchronousConnectionFilterDoesNotCrashServer()
        {
            var serviceContext = new TestServiceContext()
            {
                ConnectionFilter = new ThrowingConnectionFilter()
            };

            using (var server = new TestServer(App, serviceContext))
            {
                using (var connection = new TestConnection())
                {
                    try
                    {
                        await connection.SendEnd(
                            "POST / HTTP/1.0",
                            "",
                            "Hello World?");
                    }
                    catch (IOException)
                    {
                        // Will throw because the exception in the connection filter will close the connection.
                        Assert.True(true);
                    }
                }
            }
        }