|
3 | 3 |
|
4 | 4 | using Microsoft.AspNetCore.Http;
|
5 | 5 | using Microsoft.AspNetCore.Http.Features;
|
| 6 | +using Microsoft.AspNetCore.Testing; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
6 | 8 |
|
7 | 9 | namespace Microsoft.AspNetCore.Builder.Internal;
|
8 | 10 |
|
9 |
| -public class ApplicationBuilderTests |
| 11 | +public class ApplicationBuilderTests : LoggedTest |
10 | 12 | {
|
11 | 13 | [Fact]
|
12 |
| - public void BuildReturnsCallableDelegate() |
| 14 | + public async Task BuildReturnsCallableDelegate() |
13 | 15 | {
|
14 | 16 | var builder = new ApplicationBuilder(null);
|
15 | 17 | var app = builder.Build();
|
16 | 18 |
|
17 | 19 | var httpContext = new DefaultHttpContext();
|
18 | 20 |
|
19 |
| - app.Invoke(httpContext); |
| 21 | + await app.Invoke(httpContext); |
20 | 22 | Assert.Equal(404, httpContext.Response.StatusCode);
|
21 | 23 | }
|
22 | 24 |
|
@@ -74,6 +76,55 @@ public async Task BuildImplicitlyThrowsForMatchedEndpointAsLastStep()
|
74 | 76 | Assert.False(endpointCalled);
|
75 | 77 | }
|
76 | 78 |
|
| 79 | + [Fact] |
| 80 | + public async Task BuildLogAtRequestPipelineEnd() |
| 81 | + { |
| 82 | + var services = new ServiceCollection(); |
| 83 | + services.AddSingleton(LoggerFactory); |
| 84 | + var serviceProvider = services.BuildServiceProvider(); |
| 85 | + |
| 86 | + var builder = new ApplicationBuilder(serviceProvider); |
| 87 | + var app = builder.Build(); |
| 88 | + |
| 89 | + var httpContext = new DefaultHttpContext(); |
| 90 | + httpContext.Request.Protocol = "HTTP/2"; |
| 91 | + httpContext.Request.Scheme = "https"; |
| 92 | + httpContext.Request.Method = "GET"; |
| 93 | + httpContext.Request.Host = new HostString("localhost:5000"); |
| 94 | + httpContext.Request.Path = "/path"; |
| 95 | + httpContext.Request.PathBase = "/pathbase"; |
| 96 | + httpContext.Request.QueryString = new QueryString("?query=true"); |
| 97 | + |
| 98 | + await app.Invoke(httpContext); |
| 99 | + |
| 100 | + Assert.Equal(404, httpContext.Response.StatusCode); |
| 101 | + |
| 102 | + var log = TestSink.Writes.Single(w => w.EventId.Name == "RequestPipelineEnd"); |
| 103 | + Assert.Equal("Request reached the end of the middleware pipeline without being handled by application code. Request path: GET https://localhost:5000/pathbase/path, Response status code: 404", log.Message); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public async Task BuildDoesNotLogOrChangeStatusWithTerminalMiddleware() |
| 108 | + { |
| 109 | + var services = new ServiceCollection(); |
| 110 | + services.AddSingleton(LoggerFactory); |
| 111 | + var serviceProvider = services.BuildServiceProvider(); |
| 112 | + |
| 113 | + var builder = new ApplicationBuilder(serviceProvider); |
| 114 | + builder.Use((HttpContext context, RequestDelegate next) => |
| 115 | + { |
| 116 | + context.Response.StatusCode = StatusCodes.Status204NoContent; |
| 117 | + return Task.CompletedTask; |
| 118 | + }); |
| 119 | + var app = builder.Build(); |
| 120 | + |
| 121 | + var httpContext = new DefaultHttpContext(); |
| 122 | + await app.Invoke(httpContext); |
| 123 | + |
| 124 | + Assert.Equal(StatusCodes.Status204NoContent, httpContext.Response.StatusCode); |
| 125 | + Assert.DoesNotContain(TestSink.Writes, w => w.EventId.Name == "RequestPipelineEnd"); |
| 126 | + } |
| 127 | + |
77 | 128 | [Fact]
|
78 | 129 | public void BuildDoesNotCallMatchedEndpointWhenTerminated()
|
79 | 130 | {
|
|
0 commit comments