|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using System;
|
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Text; |
5 | 8 | using System.Threading.Tasks;
|
6 | 9 | using Microsoft.AspNetCore.Builder;
|
7 | 10 | using Microsoft.AspNetCore.Hosting;
|
@@ -146,6 +149,53 @@ public async Task ClearContent()
|
146 | 149 | Assert.Equal("part2", result);
|
147 | 150 | }
|
148 | 151 |
|
| 152 | + [Fact] |
| 153 | + public async Task FilterInstalled() |
| 154 | + { |
| 155 | + // Arrange |
| 156 | + const string Message = "Hello world!"; |
| 157 | + var bytes = Encoding.UTF8.GetBytes(Message); |
| 158 | + |
| 159 | + TrackingStream filter = default!; |
| 160 | + |
| 161 | + // Act |
| 162 | + var result = await RunAsync(context => |
| 163 | + { |
| 164 | + context.Response.Filter = filter = new TrackingStream(context.Response.Filter); |
| 165 | + context.Response.OutputStream.Write(bytes); |
| 166 | + }, builder => builder.BufferResponseStream()); |
| 167 | + |
| 168 | + // Assert |
| 169 | + Assert.NotNull(filter); |
| 170 | + Assert.Equal(bytes, filter.Bytes); |
| 171 | + Assert.Equal(Message, result); |
| 172 | + Assert.True(filter.IsDisposed); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public async Task FilterUninstalled() |
| 177 | + { |
| 178 | + // Arrange |
| 179 | + const string Message = "Hello world!"; |
| 180 | + var bytes = Encoding.UTF8.GetBytes(Message); |
| 181 | + |
| 182 | + TrackingStream filter = default!; |
| 183 | + |
| 184 | + // Act |
| 185 | + var result = await RunAsync(context => |
| 186 | + { |
| 187 | + context.Response.Filter = filter = new TrackingStream(context.Response.Filter); |
| 188 | + context.Response.OutputStream.Write(bytes); |
| 189 | + context.Response.Filter = null; |
| 190 | + }, builder => builder.BufferResponseStream()); |
| 191 | + |
| 192 | + // Assert |
| 193 | + Assert.NotNull(filter); |
| 194 | + Assert.Empty(filter.Bytes); |
| 195 | + Assert.Equal(Message, result); |
| 196 | + Assert.False(filter.IsDisposed); |
| 197 | + } |
| 198 | + |
149 | 199 | [Fact]
|
150 | 200 | public async Task MultipleClearContent()
|
151 | 201 | {
|
@@ -232,4 +282,62 @@ private static async Task<string> RunAsync(Func<HttpContext, Task> action, Actio
|
232 | 282 | await host.StopAsync();
|
233 | 283 | }
|
234 | 284 | }
|
| 285 | + |
| 286 | + private sealed class TrackingStream : Stream |
| 287 | + { |
| 288 | + [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Is not owned by this instance")] |
| 289 | + private readonly Stream _stream; |
| 290 | + private readonly List<byte> _list = new(); |
| 291 | + |
| 292 | + public TrackingStream(Stream other) |
| 293 | + { |
| 294 | + _stream = other; |
| 295 | + } |
| 296 | + |
| 297 | + public byte[] Bytes => _list.ToArray(); |
| 298 | + |
| 299 | + public override bool CanRead => _stream.CanRead; |
| 300 | + |
| 301 | + public override bool CanSeek => _stream.CanSeek; |
| 302 | + |
| 303 | + public override bool CanWrite => _stream.CanWrite; |
| 304 | + |
| 305 | + public override long Length => _stream.Length; |
| 306 | + |
| 307 | + public override long Position { get => _stream.Position; set => _stream.Position = value; } |
| 308 | + |
| 309 | + public override void Flush() |
| 310 | + { |
| 311 | + _stream.Flush(); |
| 312 | + } |
| 313 | + |
| 314 | + public override int Read(byte[] buffer, int offset, int count) |
| 315 | + { |
| 316 | + return _stream.Read(buffer, offset, count); |
| 317 | + } |
| 318 | + |
| 319 | + public override long Seek(long offset, SeekOrigin origin) |
| 320 | + { |
| 321 | + return _stream.Seek(offset, origin); |
| 322 | + } |
| 323 | + |
| 324 | + public override void SetLength(long value) |
| 325 | + { |
| 326 | + _stream.SetLength(value); |
| 327 | + } |
| 328 | + |
| 329 | + public override void Write(byte[] buffer, int offset, int count) |
| 330 | + { |
| 331 | + _list.AddRange(buffer.AsMemory(offset, count).ToArray()); |
| 332 | + _stream.Write(buffer, offset, count); |
| 333 | + } |
| 334 | + |
| 335 | + protected override void Dispose(bool disposing) |
| 336 | + { |
| 337 | + base.Dispose(disposing); |
| 338 | + IsDisposed = true; |
| 339 | + } |
| 340 | + |
| 341 | + public bool IsDisposed { get; private set; } |
| 342 | + } |
235 | 343 | }
|
0 commit comments