-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathResponseStreamTests.cs
369 lines (305 loc) · 10.3 KB
/
ResponseStreamTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
namespace Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests;
[Collection(nameof(SelfHostedTests))]
public class ResponseStreamTests
{
private readonly string ContentValue = Guid.NewGuid().ToString();
[Fact]
public async Task WriteStreamContent()
{
var result = await RunAsync(context =>
{
context.Response.Write(ContentValue);
});
Assert.Equal(ContentValue, result);
}
[Fact]
public async Task BufferedOutputIsFlushed()
{
var result = await RunAsync(context =>
{
context.Response.Write(ContentValue);
}, builder => builder.BufferResponseStream());
Assert.Equal(ContentValue, result);
}
[Fact]
public async Task OutputPipeIsFlushed()
{
const string Message = "hello world from pipe!";
var result = await RunAsync(context =>
{
context.AsAspNetCore().Response.BodyWriter.Write(Encoding.UTF8.GetBytes(Message));
});
Assert.Equal(Message, result);
}
[Fact]
public async Task OutputPipeIsMarkedCompleteIfRequestIsComplete()
{
var result = await RunAsync(async context =>
{
await context.AsAspNetCore().Response.CompleteAsync();
Assert.Throws<InvalidOperationException>(() => context.AsAspNetCore().Response.BodyWriter.Write("Hello world"u8));
});
Assert.Empty(result);
}
[Fact]
public async Task BufferedOutputIsFlushedOnceWithStart()
{
var result = await RunAsync(async context =>
{
context.Response.Write(ContentValue);
await context.Response.AsAspNetCore().StartAsync();
}, builder => builder.BufferResponseStream());
Assert.Equal(ContentValue, result);
}
[Fact]
public async Task BufferedOutputIsFlushedOnceWithComplete()
{
var result = await RunAsync(async context =>
{
context.Response.Write(ContentValue);
await context.Response.AsAspNetCore().CompleteAsync();
}, builder => builder.BufferResponseStream());
Assert.Equal(ContentValue, result);
}
[Fact]
public async Task EndRequest()
{
var result = await RunAsync(context =>
{
context.Response.Write("test");
context.Response.End();
}, builder => builder.BufferResponseStream());
Assert.Equal("test", result);
}
[Fact]
public async Task SuppressContent()
{
var result = await RunAsync(context =>
{
context.Response.Write("test");
context.Response.SuppressContent = true;
}, builder => builder.BufferResponseStream());
Assert.Empty(result);
}
[Fact]
public async Task SuppressContentFalse()
{
var result = await RunAsync(context =>
{
context.Response.Write(ContentValue);
context.Response.SuppressContent = true;
context.Response.SuppressContent = false;
}, builder => builder.BufferResponseStream());
Assert.Equal(ContentValue, result);
}
[Fact]
public async Task ClearContentNoBuffering()
{
await Assert.ThrowsAsync<InvalidOperationException>(() => RunAsync(context =>
{
context.Response.ClearContent();
}));
}
[Fact]
public async Task SetTrueSuppressNoBuffering()
{
await Assert.ThrowsAsync<InvalidOperationException>(() => RunAsync(context =>
{
context.Response.SuppressContent = true;
}));
}
[Fact]
public async Task GetSetFalseSuppressNoBuffering()
{
var result = await RunAsync(context =>
{
Assert.False(context.Response.SuppressContent);
context.Response.SuppressContent = false;
context.Response.Output.Write(ContentValue);
});
Assert.Equal(ContentValue, result);
}
[Fact]
public async Task ClearContent()
{
var result = await RunAsync(context =>
{
context.Response.Write("part1");
context.Response.ClearContent();
context.Response.Write("part2");
}, builder => builder.BufferResponseStream());
Assert.Equal("part2", result);
}
[Fact]
public async Task FilterInstalled()
{
// Arrange
const string Message = "Hello world!";
var bytes = Encoding.UTF8.GetBytes(Message);
TrackingStream filter = default!;
// Act
var result = await RunAsync(context =>
{
context.Response.Filter = filter = new TrackingStream(context.Response.Filter);
context.Response.OutputStream.Write(bytes);
}, builder => builder.BufferResponseStream());
// Assert
Assert.NotNull(filter);
Assert.Equal(bytes, filter.Bytes);
Assert.Equal(Message, result);
Assert.True(filter.IsDisposed);
}
[Fact]
public async Task FilterUninstalled()
{
// Arrange
const string Message = "Hello world!";
var bytes = Encoding.UTF8.GetBytes(Message);
TrackingStream filter = default!;
// Act
var result = await RunAsync(context =>
{
context.Response.Filter = filter = new TrackingStream(context.Response.Filter);
context.Response.OutputStream.Write(bytes);
context.Response.Filter = null;
}, builder => builder.BufferResponseStream());
// Assert
Assert.NotNull(filter);
Assert.Empty(filter.Bytes);
Assert.Equal(Message, result);
Assert.False(filter.IsDisposed);
}
[Fact]
public async Task MultipleClearContent()
{
var result = await RunAsync(context =>
{
context.Response.Write("part1");
context.Response.ClearContent();
context.Response.Write("part2");
context.Response.ClearContent();
context.Response.Write("part3");
context.Response.ClearContent();
context.Response.Write("part4");
}, builder => builder.BufferResponseStream());
Assert.Equal("part4", result);
}
[Fact]
public async Task BufferOutputIsNotEnabled()
{
var result = await RunAsync(context =>
{
context.Response.Write(context.Response.BufferOutput.ToString());
});
Assert.Equal("False", result);
}
[Fact]
public async Task BufferedOutputIsEnabled()
{
var result = await RunAsync(context =>
{
context.Response.Write(context.Response.BufferOutput.ToString());
}, builder => builder.BufferResponseStream());
Assert.Equal("True", result);
}
private static Task<string> RunAsync(Action<HttpContext> action, Action<IEndpointConventionBuilder>? builder = null)
=> RunAsync(ctx =>
{
action(ctx);
return Task.CompletedTask;
}, builder);
private static async Task<string> RunAsync(Func<HttpContext, Task> action, Action<IEndpointConventionBuilder>? builder = null)
{
builder ??= _ => { };
using var host = await new HostBuilder()
.ConfigureWebHost(webBuilder =>
{
webBuilder
.UseTestServer(options =>
{
options.AllowSynchronousIO = true;
})
.ConfigureServices(services =>
{
services.AddRouting();
services.AddSystemWebAdapters();
})
.Configure(app =>
{
app.UseRouting();
app.UseSystemWebAdapters();
app.UseEndpoints(endpoints =>
{
builder(endpoints.Map("/", (HttpContextCore context) => action(context)));
});
});
})
.StartAsync();
var uri = new Uri("/", UriKind.Relative);
try
{
return await host.GetTestClient().GetStringAsync(uri).ConfigureAwait(false);
}
finally
{
await host.StopAsync();
}
}
private sealed class TrackingStream : Stream
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Is not owned by this instance")]
private readonly Stream _stream;
private readonly List<byte> _list = new();
public TrackingStream(Stream other)
{
_stream = other;
}
public byte[] Bytes => _list.ToArray();
public override bool CanRead => _stream.CanRead;
public override bool CanSeek => _stream.CanSeek;
public override bool CanWrite => _stream.CanWrite;
public override long Length => _stream.Length;
public override long Position { get => _stream.Position; set => _stream.Position = value; }
public override void Flush()
{
_stream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return _stream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return _stream.Seek(offset, origin);
}
public override void SetLength(long value)
{
_stream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
_list.AddRange(buffer.AsMemory(offset, count).ToArray());
_stream.Write(buffer, offset, count);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
IsDisposed = true;
}
public bool IsDisposed { get; private set; }
}
}