예제 #1
0
        public async Task UseClientCertOnHttp2_OSSupportsIt_Success()
        {
            using X509Certificate2 clientCert = Test.Common.Configuration.Certificates.GetClientCertificate();
            await Http2LoopbackServer.CreateClientAndServerAsync(
                async address =>
                {
                    var handler = new WinHttpHandler();
                    handler.ServerCertificateValidationCallback = CustomServerCertificateValidationCallback;
                    handler.ClientCertificates.Add(clientCert);
                    handler.ClientCertificateOption = ClientCertificateOption.Manual;
                    using (var client = new HttpClient(handler))
                    using (HttpResponseMessage response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, address) { Version = HttpVersion20.Value }))
                    {
                        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                        Assert.True(_validationCallbackHistory.WasCalled);
                        Assert.NotEmpty(_validationCallbackHistory.CertificateChain);
                        Assert.Equal(Test.Common.Configuration.Certificates.GetServerCertificate(), _validationCallbackHistory.CertificateChain[0]);
                    }
                },
                async s =>
                {
                    await using (Http2LoopbackConnection connection = await s.EstablishConnectionAsync().ConfigureAwait(false))
                    {
                        SslStream sslStream = connection.Stream as SslStream;
                        Assert.NotNull(sslStream);
                        Assert.True(sslStream.IsMutuallyAuthenticated);
                        Assert.Equal(clientCert, sslStream.RemoteCertificate);

                        int streamId = await connection.ReadRequestHeaderAsync();
                        await connection.SendDefaultResponseAsync(streamId);
                    }
                }, new Http2Options { ClientCertificateRequired = true });
        }
예제 #2
0
        public async Task ConnectAsync_VersionSupported_WithSsl_Success()
        {
            await Http2LoopbackServer.CreateClientAndServerAsync(async uri =>
            {
                using (var cws = new ClientWebSocket())
                    using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
                    {
                        cws.Options.HttpVersion       = HttpVersion.Version20;
                        cws.Options.HttpVersionPolicy = Http.HttpVersionPolicy.RequestVersionExact;

                        var handler = CreateSocketsHttpHandler(allowAllCertificates: true);
                        await cws.ConnectAsync(uri, new HttpMessageInvoker(handler), cts.Token);
                    }
            },
                                                                 async server =>
            {
                Http2LoopbackConnection connection = await server.EstablishConnectionAsync(new SettingsEntry {
                    SettingId = SettingId.EnableConnect, Value = 1
                });
                (int streamId, HttpRequestData requestData) = await connection.ReadAndParseRequestHeaderAsync(readBody: false);
                await connection.SendResponseHeadersAsync(streamId, endStream: false, HttpStatusCode.OK);
            }, new Http2Options()
            {
                WebSocketEndpoint = true
            }
                                                                 );
        }
예제 #3
0
        public async Task ConnectAsync_VersionNotSupported_WithSsl_Throws()
        {
            await Http2LoopbackServer.CreateClientAndServerAsync(async uri =>
            {
                using (var cws = new ClientWebSocket())
                    using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
                    {
                        cws.Options.HttpVersion       = HttpVersion.Version20;
                        cws.Options.HttpVersionPolicy = Http.HttpVersionPolicy.RequestVersionExact;
                        Task t;
                        var handler = CreateSocketsHttpHandler(allowAllCertificates: true);
                        t           = cws.ConnectAsync(uri, new HttpMessageInvoker(handler), cts.Token);

                        var ex = await Assert.ThrowsAnyAsync <WebSocketException>(() => t);
                        Assert.IsType <HttpRequestException>(ex.InnerException);
                        Assert.True(ex.InnerException.Data.Contains("SETTINGS_ENABLE_CONNECT_PROTOCOL"));
                    }
            },
                                                                 async server =>
            {
                Http2LoopbackConnection connection = await server.EstablishConnectionAsync(new SettingsEntry {
                    SettingId = SettingId.EnableConnect, Value = 0
                });
            }, new Http2Options()
            {
                WebSocketEndpoint = true
            }
                                                                 );
        }
예제 #4
0
        public async Task ReceiveNoThrowAfterSend_NoSsl(bool useHandler)
        {
            var serverMessage = new byte[] { 4, 5, 6 };
            await Http2LoopbackServer.CreateClientAndServerAsync(async uri =>
            {
                using (var cws = new ClientWebSocket())
                    using (var cts = new CancellationTokenSource(TimeOutMilliseconds))
                    {
                        cws.Options.HttpVersion       = HttpVersion.Version20;
                        cws.Options.HttpVersionPolicy = Http.HttpVersionPolicy.RequestVersionExact;
                        if (useHandler)
                        {
                            var handler = new SocketsHttpHandler();
                            await cws.ConnectAsync(uri, new HttpMessageInvoker(handler), cts.Token);
                        }
                        else
                        {
                            await cws.ConnectAsync(uri, cts.Token);
                        }

                        await cws.SendAsync(new byte[] { 2, 3, 4 }, WebSocketMessageType.Binary, true, cts.Token);

                        var readBuffer = new byte[serverMessage.Length];
                        await cws.ReceiveAsync(readBuffer, cts.Token);
                        Assert.Equal(serverMessage, readBuffer);
                    }
            },
                                                                 async server =>
            {
                Http2LoopbackConnection connection = await server.EstablishConnectionAsync(new SettingsEntry {
                    SettingId = SettingId.EnableConnect, Value = 1
                });
                (int streamId, HttpRequestData requestData) = await connection.ReadAndParseRequestHeaderAsync(readBody: false);
                // send status 200 OK to establish websocket
                await connection.SendResponseHeadersAsync(streamId, endStream: false).ConfigureAwait(false);

                // send reply
                byte binaryMessageType  = 2;
                var prefix              = new byte[] { binaryMessageType, (byte)serverMessage.Length };
                byte[] constructMessage = prefix.Concat(serverMessage).ToArray();
                await connection.SendResponseDataAsync(streamId, constructMessage, endStream: false);
            }, new Http2Options()
            {
                WebSocketEndpoint = true, UseSsl = false
            }
                                                                 );
        }