csharp/aspnet/AspNetKatana/tests/Microsoft.Owin.StaticFiles.Tests/SendFileResponseExtensionsTests.cs

SendFileResponseExtensionsTests.cs
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Owin.StaticFiles.Tests
{
    public clast SendFileResponseExtensionsTests
    {
        [Fact]
        public void SendFileSupport()
        {
            IOwinResponse response = new OwinResponse();
            astert.False(response.SupportsSendFile());
            // response.Set("sendfile.SendAsync", new Object());
            // astert.False(response.SupportsSendFile()); // Get throw for type mismatch.
            response.Set("sendfile.SendAsync", new Func((_, __, ___, ____) => Task.FromResult(0)));
            astert.True(response.SupportsSendFile());
        }

        [Fact]
        public void SendFileWhenNotSupported()
        {
            IOwinResponse response = new OwinResponse();
            astert.Throws(() => response.SendFileAsync("foo"));
        }

        [Fact]
        public async Task SendFileWorks()
        {
            IOwinResponse response = new OwinResponse();
            string name = null;
            long offset = 0;
            long? length = null;
            CancellationToken token;
            Func func = (n, o, l, c) =>
                {
                    name = n;
                    offset = o;
                    length = l;
                    token = c;
                    return Task.FromResult(0);
                };

            response.Set("sendfile.SendAsync", func);

            await response.SendFileAsync("bob", 1, 3, CancellationToken.None);
            astert.Equal("bob", name);
            astert.Equal(1, offset);
            astert.Equal(3, length);
            astert.Equal(CancellationToken.None, token);
        }
    }
}