Esempio n. 1
0
        public async Task SSEWritesVeryLargeMessages()
        {
            using (StartVerifiableLog())
            {
                var pair       = DuplexPipe.CreateConnectionPair(PipeOptions.Default, new PipeOptions(readerScheduler: PipeScheduler.Inline));
                var connection = new DefaultConnectionContext("foo", pair.Transport, pair.Application);
                var context    = new DefaultHttpContext();

                var ms = new MemoryStream();
                context.Response.Body = ms;
                var sse = new ServerSentEventsServerTransport(connection.Application.Input, connectionId: string.Empty, LoggerFactory);

                var task = sse.ProcessRequestAsync(context, context.RequestAborted);

                string hText = new string('H', 60000);
                string wText = new string('W', 60000);

                await connection.Transport.Output.WriteAsync(Encoding.ASCII.GetBytes(hText + wText));

                connection.Transport.Output.Complete();
                await task.OrTimeout();

                Assert.Equal(":\r\ndata: " + hText + wText + "\r\n\r\n", Encoding.ASCII.GetString(ms.ToArray()));
            }
        }
Esempio n. 2
0
        public async Task SSESetsContentType()
        {
            using (StartVerifiableLog())
            {
                var pair       = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
                var connection = new DefaultConnectionContext("foo", pair.Transport, pair.Application);
                var context    = new DefaultHttpContext();

                var sse = new ServerSentEventsServerTransport(connection.Application.Input, connectionId: string.Empty, LoggerFactory);

                connection.Transport.Output.Complete();

                await sse.ProcessRequestAsync(context, context.RequestAborted);

                Assert.Equal("text/event-stream", context.Response.ContentType);
                Assert.Equal("no-cache", context.Response.Headers["Cache-Control"]);
            }
        }
Esempio n. 3
0
        public async Task SSETurnsResponseBufferingOff()
        {
            using (StartVerifiableLog())
            {
                var pair       = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
                var connection = new DefaultConnectionContext("foo", pair.Transport, pair.Application);
                var context    = new DefaultHttpContext();

                var feature = new HttpBufferingFeature(new MemoryStream());
                context.Features.Set <IHttpResponseBodyFeature>(feature);
                var sse = new ServerSentEventsServerTransport(connection.Application.Input, connectionId: connection.ConnectionId, LoggerFactory);

                connection.Transport.Output.Complete();

                await sse.ProcessRequestAsync(context, context.RequestAborted);

                Assert.True(feature.ResponseBufferingDisabled);
            }
        }
Esempio n. 4
0
        public async Task SSEAddsAppropriateFraming(string message, string expected)
        {
            using (StartVerifiableLog())
            {
                var pair       = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
                var connection = new DefaultConnectionContext("foo", pair.Transport, pair.Application);
                var context    = new DefaultHttpContext();

                var sse = new ServerSentEventsServerTransport(connection.Application.Input, connectionId: string.Empty, LoggerFactory);
                var ms  = new MemoryStream();
                context.Response.Body = ms;

                await connection.Transport.Output.WriteAsync(Encoding.UTF8.GetBytes(message));

                connection.Transport.Output.Complete();

                await sse.ProcessRequestAsync(context, context.RequestAborted);

                Assert.Equal(expected, Encoding.UTF8.GetString(ms.ToArray()));
            }
        }