public async Task SendFileWorks()
        {
            var context = new DefaultHttpContext();
            var response = context.Response;
            var fakeFeature = new FakeSendFileFeature();
            context.Features.Set<IHttpSendFileFeature>(fakeFeature);

            await response.SendFileAsync("bob", 1, 3, CancellationToken.None);

            Assert.Equal("bob", fakeFeature.name);
            Assert.Equal(1, fakeFeature.offset);
            Assert.Equal(3, fakeFeature.length);
            Assert.Equal(CancellationToken.None, fakeFeature.token);
        }
        public async Task SendFileWorks()
        {
            var context     = new DefaultProtoContext();
            var response    = context.Response;
            var fakeFeature = new FakeSendFileFeature();

            context.Features.Set <IProtoSendFileFeature>(fakeFeature);

            await response.SendFileAsync("bob", 1, 3, CancellationToken.None);

            Assert.Equal("bob", fakeFeature.name);
            Assert.Equal(1, fakeFeature.offset);
            Assert.Equal(3, fakeFeature.length);
            Assert.Equal(CancellationToken.None, fakeFeature.token);
        }
        public async Task SendFileAsync_AfterFirstWrite_CompressesAndFlushes()
        {
            FakeSendFileFeature fakeSendFile = null;

            var builder = new WebHostBuilder()
                          .ConfigureServices(services =>
            {
                services.AddResponseCompression();
            })
                          .Configure(app =>
            {
                app.Use((context, next) =>
                {
                    fakeSendFile = new FakeSendFileFeature(context.Response.Body);
                    context.Features.Set <IHttpSendFileFeature>(fakeSendFile);
                    return(next());
                });
                app.UseResponseCompression();
                app.Run(async context =>
                {
                    context.Response.Headers[HeaderNames.ContentMD5] = "MD5";
                    context.Response.ContentType = TextPlain;
                    var sendFile = context.Features.Get <IHttpSendFileFeature>();
                    Assert.NotNull(sendFile);

                    await context.Response.WriteAsync(new string('a', 100));
                    await sendFile.SendFileAsync("testfile1kb.txt", 0, null, CancellationToken.None);
                });
            });

            var server = new TestServer(builder);
            var client = server.CreateClient();

            var request = new HttpRequestMessage(HttpMethod.Get, "");

            request.Headers.AcceptEncoding.ParseAdd("gzip");

            var response = await client.SendAsync(request);

            CheckResponseCompressed(response, expectedBodyLength: 40);

            Assert.False(fakeSendFile.Invoked);
        }
        public async Task SendFileAsync_DifferentContentType_NotBypassed()
        {
            FakeSendFileFeature fakeSendFile = null;

            var builder = new WebHostBuilder()
                          .ConfigureServices(services =>
            {
                services.AddResponseCompression();
            })
                          .Configure(app =>
            {
                app.Use((context, next) =>
                {
                    fakeSendFile = new FakeSendFileFeature(context.Response.Body);
                    context.Features.Set <IHttpSendFileFeature>(fakeSendFile);
                    return(next());
                });
                app.UseResponseCompression();
                app.Run(context =>
                {
                    context.Response.Headers[HeaderNames.ContentMD5] = "MD5";
                    context.Response.ContentType   = "custom/type";
                    context.Response.ContentLength = 1024;
                    var sendFile = context.Features.Get <IHttpSendFileFeature>();
                    Assert.NotNull(sendFile);
                    return(sendFile.SendFileAsync("testfile1kb.txt", 0, null, CancellationToken.None));
                });
            });

            var server = new TestServer(builder);
            var client = server.CreateClient();

            var request = new HttpRequestMessage(HttpMethod.Get, "");

            request.Headers.AcceptEncoding.ParseAdd("gzip");

            var response = await client.SendAsync(request);

            CheckResponseNotCompressed(response, expectedBodyLength: 1024, sendVaryHeader: false);

            Assert.True(fakeSendFile.Invoked);
        }