public async Task SSEWaitsForResponseToStart()
            {
                using (StartVerifiableLog())
                {
                    var httpHandler = new TestHttpMessageHandler();

                    var connectResponseTcs = new TaskCompletionSource <object>();
                    httpHandler.OnGet("/?id=00000000-0000-0000-0000-000000000000", async(_, __) =>
                    {
                        await connectResponseTcs.Task;
                        return(ResponseUtils.CreateResponse(HttpStatusCode.Accepted));
                    });

                    var sse = new ServerSentEventsTransport(new HttpClient(httpHandler), LoggerFactory);

                    await WithConnectionAsync(
                        CreateConnection(httpHandler, loggerFactory : LoggerFactory, transport : sse),
                        async (connection) =>
                    {
                        var startTask = connection.StartAsync(TransferFormat.Text).OrTimeout();
                        Assert.False(connectResponseTcs.Task.IsCompleted);
                        Assert.False(startTask.IsCompleted);
                        connectResponseTcs.TrySetResult(null);
                        await startTask;
                    });
                }
            }
            public async Task SSEWontStartIfSuccessfulConnectionIsNotEstablished()
            {
                bool ExpectedErrors(WriteContext writeContext)
                {
                    return(writeContext.LoggerName == typeof(HttpConnection).FullName &&
                           writeContext.EventId.Name == "ErrorStartingTransport");
                }

                using (StartVerifiableLog(expectedErrorsFilter: ExpectedErrors))
                {
                    var httpHandler = new TestHttpMessageHandler();

                    httpHandler.OnGet("/?id=00000000-0000-0000-0000-000000000000", (_, __) =>
                    {
                        return(Task.FromResult(ResponseUtils.CreateResponse(HttpStatusCode.InternalServerError)));
                    });

                    var sse = new ServerSentEventsTransport(new HttpClient(httpHandler), LoggerFactory);

                    await WithConnectionAsync(
                        CreateConnection(httpHandler, loggerFactory : LoggerFactory, transport : sse),
                        async (connection) =>
                    {
                        await Assert.ThrowsAsync <AggregateException>(
                            () => connection.StartAsync(TransferFormat.Text).OrTimeout());
                    });
                }
            }
            public async Task SSECanBeCanceled()
            {
                bool ExpectedErrors(WriteContext writeContext)
                {
                    return(writeContext.LoggerName == typeof(HttpConnection).FullName &&
                           writeContext.EventId.Name == "ErrorStartingTransport");
                }

                using (StartVerifiableLog(expectedErrorsFilter: ExpectedErrors))
                {
                    var httpHandler = new TestHttpMessageHandler();
                    httpHandler.OnGet("/?id=00000000-0000-0000-0000-000000000000", (_, __) =>
                    {
                        // Simulating a cancellationToken canceling this request.
                        throw new OperationCanceledException("Cancel SSE Start.");
                    });

                    var sse = new ServerSentEventsTransport(new HttpClient(httpHandler), LoggerFactory);

                    await WithConnectionAsync(
                        CreateConnection(httpHandler, loggerFactory : LoggerFactory, transport : sse, transportType : HttpTransportType.ServerSentEvents),
                        async (connection) =>
                    {
                        var ex = await Assert.ThrowsAsync <AggregateException>(async() => await connection.StartAsync()).OrTimeout();
                    });
                }
            }
            public async Task SSEWontStartIfSuccessfulConnectionIsNotEstablished()
            {
                // TODO: Add logging https://github.com/aspnet/SignalR/issues/2879
                var httpHandler = new TestHttpMessageHandler();

                httpHandler.OnGet("/?id=00000000-0000-0000-0000-000000000000", (_, __) =>
                {
                    return(Task.FromResult(ResponseUtils.CreateResponse(HttpStatusCode.InternalServerError)));
                });

                var sse = new ServerSentEventsTransport(new HttpClient(httpHandler));

                await WithConnectionAsync(
                    CreateConnection(httpHandler, transport : sse),
                    async (connection) =>
                {
                    await Assert.ThrowsAsync <InvalidOperationException>(
                        () => connection.StartAsync(TransferFormat.Text).OrTimeout());
                });
            }
Beispiel #5
0
            public async Task SSEWontStartIfSuccessfulConnectionIsNotEstablished()
            {
                using (StartLog(out var loggerFactory))
                {
                    var httpHandler = new TestHttpMessageHandler();

                    httpHandler.OnGet("/?id=00000000-0000-0000-0000-000000000000", (_, __) =>
                    {
                        return(Task.FromResult(ResponseUtils.CreateResponse(HttpStatusCode.InternalServerError)));
                    });

                    var sse = new ServerSentEventsTransport(new HttpClient(httpHandler));

                    await WithConnectionAsync(
                        CreateConnection(httpHandler, loggerFactory : loggerFactory, url : null, transport : sse),
                        async (connection) =>
                    {
                        await Assert.ThrowsAsync <InvalidOperationException>(
                            () => connection.StartAsync(TransferFormat.Text).OrTimeout());
                    });
                }
            }