|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.Extensions.Logging.Abstractions; |
| 5 | +using Microsoft.AspNetCore.Http; |
| 6 | + |
| 7 | +namespace Microsoft.AspNetCore.Mvc.Infrastructure; |
| 8 | + |
| 9 | +public class FileStreamResultExecutorTest |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public async Task ExecuteAsync_DisposesStreamAsync() |
| 13 | + { |
| 14 | + // Arrange |
| 15 | + var executor = CreateExecutor(); |
| 16 | + |
| 17 | + var httpContext = new DefaultHttpContext(); |
| 18 | + var actionContext = new ActionContext() { HttpContext = httpContext }; |
| 19 | + |
| 20 | + var stream = new AsyncOnlyStream(); |
| 21 | + var result = new FileStreamResult(stream, "text/plain"); |
| 22 | + |
| 23 | + // Act |
| 24 | + await executor.ExecuteAsync(actionContext, result); |
| 25 | + |
| 26 | + // Assert |
| 27 | + Assert.True(stream.DidDisposeAsync); |
| 28 | + } |
| 29 | + |
| 30 | + private static FileStreamResultExecutor CreateExecutor() |
| 31 | + { |
| 32 | + return new FileStreamResultExecutor(NullLoggerFactory.Instance); |
| 33 | + } |
| 34 | + |
| 35 | + private class AsyncOnlyStream : Stream |
| 36 | + { |
| 37 | + public override bool CanRead => true; |
| 38 | + |
| 39 | + public override bool CanSeek => false; |
| 40 | + |
| 41 | + public override bool CanWrite => false; |
| 42 | + |
| 43 | + public override long Length => throw new NotImplementedException(); |
| 44 | + |
| 45 | + public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } |
| 46 | + |
| 47 | + public override void Flush() { } |
| 48 | + |
| 49 | + public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException("Must use ReadAsync"); |
| 50 | + |
| 51 | + public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| 52 | + => Task.FromResult(0); |
| 53 | + |
| 54 | + public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException(); |
| 55 | + |
| 56 | + public override void SetLength(long value) => throw new NotImplementedException(); |
| 57 | + |
| 58 | + public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException(); |
| 59 | + |
| 60 | + protected override void Dispose(bool disposing) => throw new NotSupportedException("Must use DisposeAsync"); |
| 61 | + |
| 62 | + public bool DidDisposeAsync { get; private set; } |
| 63 | + |
| 64 | + public override ValueTask DisposeAsync() |
| 65 | + { |
| 66 | + DidDisposeAsync = true; |
| 67 | + return ValueTask.CompletedTask; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments