forked from DebugProbe/DebugProbe.AspNetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiddlewareExecutionFlowTests.cs
More file actions
134 lines (112 loc) · 4.69 KB
/
Copy pathMiddlewareExecutionFlowTests.cs
File metadata and controls
134 lines (112 loc) · 4.69 KB
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
using System.Net;
using DebugProbe.AspNetCore.Middleware;
using DebugProbe.AspNetCore.Options;
using DebugProbe.AspNetCore.Storage;
using DebugProbe.AspNetCore.Tests.Infrastructure;
using Microsoft.AspNetCore.Http;
namespace DebugProbe.AspNetCore.Tests.Middleware;
public class MiddlewareExecutionFlowTests
{
[Fact]
public async Task Middleware_executes_and_request_continues_through_pipeline()
{
await using var app = await DebugProbeTestApp.CreateAsync(endpoints =>
{
endpoints.MapGet("/hello", () => Results.Text("hello from endpoint"));
});
var response = await app.Client.GetAsync("/hello");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("hello from endpoint", await response.Content.ReadAsStringAsync());
var entry = app.SingleEntry;
Assert.Equal("GET", entry.Method);
Assert.Equal("/hello", entry.Path);
Assert.Equal(200, entry.StatusCode);
Assert.Equal("hello from endpoint", entry.ResponseBody);
}
[Fact]
public async Task Ignored_paths_are_skipped()
{
await using var app = await DebugProbeTestApp.CreateAsync(
endpoints => endpoints.MapGet("/health", () => Results.Ok()),
options => options.IgnorePaths = ["/health"]);
var response = await app.Client.GetAsync("/health");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Empty(app.Store.GetAll());
}
[Fact]
public async Task Similar_paths_are_not_skipped()
{
await using var app = await DebugProbeTestApp.CreateAsync(
endpoints => endpoints.MapGet("/healthcare", () => Results.Ok()),
options => options.IgnorePaths = ["/health"]);
var response = await app.Client.GetAsync("/healthcare");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Single(app.Store.GetAll());
}
[Theory]
[InlineData("/health")]
[InlineData("/healthz")]
[InlineData("/ready")]
[InlineData("/live")]
public async Task Default_health_probe_paths_are_skipped(string path)
{
await using var app = await DebugProbeTestApp.CreateAsync(endpoints =>
{
endpoints.MapGet(path, () => Results.Ok());
});
var response = await app.Client.GetAsync(path);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Empty(app.Store.GetAll());
}
[Fact]
public async Task Debug_paths_are_skipped()
{
await using var app = await DebugProbeTestApp.CreateAsync(endpoints =>
{
endpoints.MapGet("/debug/custom", () => Results.Text("debug"));
});
var response = await app.Client.GetAsync("/debug/custom");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Empty(app.Store.GetAll());
}
[Fact]
public async Task Response_stream_is_restored_after_successful_request()
{
var originalBody = new MemoryStream();
var context = CreateHttpContext(originalBody);
var store = new DebugEntryStore(new DebugProbeOptions());
var middleware = new DebugProbeMiddleware(
async httpContext => await httpContext.Response.WriteAsync("ok"),
new DebugProbeOptions());
await middleware.Invoke(context, store);
Assert.Same(originalBody, context.Response.Body);
Assert.Equal("ok", Assert.Single(store.GetAll()).ResponseBody);
}
[Fact]
public async Task Response_stream_is_restored_after_exception()
{
var originalBody = new MemoryStream();
var context = CreateHttpContext(originalBody);
var store = new DebugEntryStore(new DebugProbeOptions());
var middleware = new DebugProbeMiddleware(
_ => throw new InvalidOperationException("broken"),
new DebugProbeOptions());
await Assert.ThrowsAsync<InvalidOperationException>(() => middleware.Invoke(context, store));
Assert.Same(originalBody, context.Response.Body);
var entry = Assert.Single(store.GetAll());
Assert.Equal(500, entry.StatusCode);
Assert.Contains("broken", entry.ResponseBody);
}
private static DefaultHttpContext CreateHttpContext(Stream responseBody)
{
var context = new DefaultHttpContext();
context.SetEndpoint(new Endpoint(_ => Task.CompletedTask, EndpointMetadataCollection.Empty, "test"));
context.Request.Method = HttpMethods.Get;
context.Request.Scheme = "http";
context.Request.Host = new HostString("example.test");
context.Request.Path = "/direct";
context.Response.Body = responseBody;
context.Response.ContentType = "text/plain";
return context;
}
}