-
Notifications
You must be signed in to change notification settings - Fork 3
/
FakeHttpResponseFeature.cs
45 lines (41 loc) · 2.13 KB
/
FakeHttpResponseFeature.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
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Net.Http.Headers;
namespace Cuemon.Extensions.Xunit.Hosting.AspNetCore.Http.Features
{
/// <summary>
/// Represents a way to trigger <see cref="HttpResponseFeature.OnStarting"/>.
/// </summary>
/// <seealso cref="HttpResponseFeature" />
public class FakeHttpResponseFeature : HttpResponseFeature
{
private bool _hasStarted;
/// <summary>
/// Initializes a new instance of the <see cref="FakeHttpResponseFeature"/> class.
/// </summary>
public FakeHttpResponseFeature()
{
Headers.Append(HeaderNames.Date, DateTime.UtcNow.ToString("R"));
}
/// <summary>
/// Registers a callback to be invoked just before the response starts. This is the last chance to modify the <see cref="P:Microsoft.AspNetCore.Http.Features.IHttpResponseFeature.Headers" />, <see cref="P:Microsoft.AspNetCore.Http.Features.IHttpResponseFeature.StatusCode" />, or <see cref="P:Microsoft.AspNetCore.Http.Features.IHttpResponseFeature.ReasonPhrase" />.
/// </summary>
/// <param name="callback">The callback to invoke when starting the response.</param>
/// <param name="state">The state to pass into the callback.</param>
public override void OnStarting(Func<object, Task> callback, object state)
{
if (_hasStarted) { return; }
_hasStarted = true;
callback?.Invoke(state);
}
/// <summary>
/// Indicates if the response has started. If true, the <see cref="P:Microsoft.AspNetCore.Http.Features.IHttpResponseFeature.StatusCode" />,
/// <see cref="P:Microsoft.AspNetCore.Http.Features.IHttpResponseFeature.ReasonPhrase" />, and <see cref="P:Microsoft.AspNetCore.Http.Features.IHttpResponseFeature.Headers" /> are now immutable, and
/// OnStarting should no longer be called.
/// </summary>
/// <value><c>true</c> if this instance has started; otherwise, <c>false</c>.</value>
public override bool HasStarted => _hasStarted;
}
}