public void CallingMethodInvokesDelegate()
        {
            var res = new OwinResponse(OwinRequest.Create());

            res.CanSendFile.ShouldBe(false);

            string            aa = null;
            long              bb = 0;
            long?             cc = null;
            CancellationToken dd = CancellationToken.None;

            var cts = new CancellationTokenSource();

            res.SendFileAsyncDelegate = (a, b, c, d) =>
            {
                aa = a;
                bb = b;
                cc = c;
                dd = d;
                return(null);
            };
            res.SendFileAsync("one", 2, 3, cts.Token);
            aa.ShouldBe("one");
            bb.ShouldBe(2);
            cc.ShouldBe(3);
            dd.ShouldBe(cts.Token);

            res.SendFileAsync("four");
            aa.ShouldBe("four");
            bb.ShouldBe(0);
            cc.ShouldBe(null);
            dd.ShouldBe(CancellationToken.None);
        }
        public void CallingMethodInvokesDelegate()
        {
            var res = new OwinResponse(OwinRequest.Create());
            res.CanSendFile.ShouldBe(false);

            string aa = null;
            long bb = 0;
            long? cc = null;
            CancellationToken dd = CancellationToken.None;

            var cts = new CancellationTokenSource();

            res.SendFileAsyncDelegate = (a, b, c, d) =>
            {
                aa = a;
                bb = b;
                cc = c;
                dd = d;
                return null;
            };
            res.SendFileAsync("one", 2, 3, cts.Token);
            aa.ShouldBe("one");
            bb.ShouldBe(2);
            cc.ShouldBe(3);
            dd.ShouldBe(cts.Token);

            res.SendFileAsync("four");
            aa.ShouldBe("four");
            bb.ShouldBe(0);
            cc.ShouldBe(null);
            dd.ShouldBe(CancellationToken.None);
        }
        public async Task SendFileWorks()
        {
            IOwinResponse     response = new OwinResponse();
            string            name     = null;
            long              offset   = 0;
            long?             length   = null;
            CancellationToken token;
            Func <string, long, long?, CancellationToken, Task> 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);

            Assert.Equal("bob", name);
            Assert.Equal(1, offset);
            Assert.Equal(3, length);
            Assert.Equal(CancellationToken.None, token);
        }
Пример #4
0
        private static async Task SendFileToResponse(OwinResponse response, CrossAppDomainResponseFile file)
        {
            if (response.CanSendFile)
            {
                await response.SendFileAsync(file.Path, file.Length, file.Offset, CancellationToken.None);

                return;
            }

            var buffer = new byte[file.Length - file.Offset];

            using (var stream = File.OpenRead(file.Path)) {
                stream.Seek(file.Offset, SeekOrigin.Begin);
                await stream.ReadAsync(buffer, 0, (int)file.Length);
            }
            await response.WriteAsync(buffer);
        }
        public Task SendAsync(int statusCode)
        {
            _response.StatusCode = statusCode;
            _response.SetHeader(Constants.ContentLength, _length.ToString(CultureInfo.InvariantCulture));

            string physicalPath = _fileInfo.PhysicalPath;

            if (_response.CanSendFile && !string.IsNullOrEmpty(physicalPath))
            {
                return(_response.SendFileAsync(physicalPath, 0, _length, _request.CallCancelled));
            }

            Stream readStream    = _fileInfo.CreateReadStream();
            var    copyOperation = new StreamCopyOperation(readStream, _response.Body, _length, _request.CallCancelled);
            Task   task          = copyOperation.Start();

            task.ContinueWith(resultTask => readStream.Close(), TaskContinuationOptions.ExecuteSynchronously);
            return(task);
        }
        public async Task SendFileWorks()
        {
            IOwinResponse response = new OwinResponse();
            string name = null;
            long offset = 0;
            long? length = null;
            CancellationToken token;
            Func<string, long, long?, CancellationToken, Task> 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);
            Assert.Equal("bob", name);
            Assert.Equal(1, offset);
            Assert.Equal(3, length);
            Assert.Equal(CancellationToken.None, token);
        }
 public void SendFileWhenNotSupported()
 {
     IOwinResponse response = new OwinResponse();
     Assert.Throws<NotSupportedException>(() => response.SendFileAsync("foo"));
 }
 public async Task SendFileWhenNotSupported()
 {
     IOwinResponse response = new OwinResponse();
     await Assert.ThrowsAsync <NotSupportedException>(() => response.SendFileAsync("foo"));
 }
        public void SendFileWhenNotSupported()
        {
            IOwinResponse response = new OwinResponse();

            Assert.Throws <NotSupportedException>(() => response.SendFileAsync("foo"));
        }
 public async Task SendFileWhenNotSupported()
 {
     IOwinResponse response = new OwinResponse();
     await Assert.ThrowsAsync<NotSupportedException>(() => response.SendFileAsync("foo"));
 }