|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.AspNetCore.Connections.Features; |
| 9 | +using Microsoft.AspNetCore.Http; |
| 10 | +using Microsoft.AspNetCore.Server.Kestrel.Core; |
| 11 | +using Microsoft.AspNetCore.Server.Kestrel.Core.Internal; |
| 12 | +using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport; |
| 13 | +using Microsoft.AspNetCore.Testing; |
| 14 | +using Microsoft.Extensions.DependencyInjection; |
| 15 | +using Microsoft.Extensions.Logging; |
| 16 | +using Xunit; |
| 17 | + |
| 18 | +namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.Http2 |
| 19 | +{ |
| 20 | + public class Http2EndToEndTests : TestApplicationErrorLoggerLoggedTest |
| 21 | + { |
| 22 | + [Fact] |
| 23 | + public async Task MiddlewareIsRunWithConnectionLoggingScopeForHttp2Requests() |
| 24 | + { |
| 25 | + var expectedLogMessage = "Log from connection scope!"; |
| 26 | + string connectionIdFromFeature = null; |
| 27 | + |
| 28 | + var mockScopeLoggerProvider = new MockScopeLoggerProvider(expectedLogMessage); |
| 29 | + LoggerFactory.AddProvider(mockScopeLoggerProvider); |
| 30 | + |
| 31 | + await using var server = new TestServer(async context => |
| 32 | + { |
| 33 | + connectionIdFromFeature = context.Features.Get<IConnectionIdFeature>().ConnectionId; |
| 34 | + |
| 35 | + var logger = context.RequestServices.GetRequiredService<ILogger<Http2EndToEndTests>>(); |
| 36 | + logger.LogInformation(expectedLogMessage); |
| 37 | + |
| 38 | + await context.Response.WriteAsync("hello, world"); |
| 39 | + }, |
| 40 | + new TestServiceContext(LoggerFactory), |
| 41 | + listenOptions => |
| 42 | + { |
| 43 | + listenOptions.Protocols = HttpProtocols.Http2; |
| 44 | + }); |
| 45 | + |
| 46 | + var connectionCount = 0; |
| 47 | + using var connection = server.CreateConnection(); |
| 48 | + |
| 49 | + using var socketsHandler = new SocketsHttpHandler() |
| 50 | + { |
| 51 | + ConnectCallback = (_, _) => |
| 52 | + { |
| 53 | + if (connectionCount != 0) |
| 54 | + { |
| 55 | + throw new InvalidOperationException(); |
| 56 | + } |
| 57 | + |
| 58 | + connectionCount++; |
| 59 | + return new ValueTask<Stream>(connection.Stream); |
| 60 | + }, |
| 61 | + }; |
| 62 | + |
| 63 | + using var httpClient = new HttpClient(socketsHandler); |
| 64 | + |
| 65 | + using var httpRequsetMessage = new HttpRequestMessage() |
| 66 | + { |
| 67 | + RequestUri = new Uri("http://localhost/"), |
| 68 | + Version = new Version(2, 0), |
| 69 | + VersionPolicy = HttpVersionPolicy.RequestVersionExact, |
| 70 | + }; |
| 71 | + |
| 72 | + using var responseMessage = await httpClient.SendAsync(httpRequsetMessage); |
| 73 | + |
| 74 | + Assert.Equal("hello, world", await responseMessage.Content.ReadAsStringAsync()); |
| 75 | + |
| 76 | + Assert.NotNull(connectionIdFromFeature); |
| 77 | + Assert.NotNull(mockScopeLoggerProvider.ConnectionLogScope); |
| 78 | + Assert.Equal(connectionIdFromFeature, mockScopeLoggerProvider.ConnectionLogScope[0].Value); |
| 79 | + } |
| 80 | + |
| 81 | + private class MockScopeLoggerProvider : ILoggerProvider, ISupportExternalScope |
| 82 | + { |
| 83 | + private readonly string _expectedLogMessage; |
| 84 | + private IExternalScopeProvider _scopeProvider; |
| 85 | + |
| 86 | + public MockScopeLoggerProvider(string expectedLogMessage) |
| 87 | + { |
| 88 | + _expectedLogMessage = expectedLogMessage; |
| 89 | + } |
| 90 | + |
| 91 | + public ConnectionLogScope ConnectionLogScope { get; private set; } |
| 92 | + |
| 93 | + public ILogger CreateLogger(string categoryName) |
| 94 | + { |
| 95 | + return new MockScopeLogger(this); |
| 96 | + } |
| 97 | + |
| 98 | + public void SetScopeProvider(IExternalScopeProvider scopeProvider) |
| 99 | + { |
| 100 | + _scopeProvider = scopeProvider; |
| 101 | + } |
| 102 | + |
| 103 | + public void Dispose() |
| 104 | + { |
| 105 | + } |
| 106 | + |
| 107 | + private class MockScopeLogger : ILogger |
| 108 | + { |
| 109 | + private readonly MockScopeLoggerProvider _loggerProvider; |
| 110 | + |
| 111 | + public MockScopeLogger(MockScopeLoggerProvider parent) |
| 112 | + { |
| 113 | + _loggerProvider = parent; |
| 114 | + } |
| 115 | + |
| 116 | + public IDisposable BeginScope<TState>(TState state) |
| 117 | + { |
| 118 | + return _loggerProvider._scopeProvider?.Push(state); |
| 119 | + } |
| 120 | + |
| 121 | + public bool IsEnabled(LogLevel logLevel) |
| 122 | + { |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) |
| 127 | + { |
| 128 | + if (formatter(state, exception) != _loggerProvider._expectedLogMessage) |
| 129 | + { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + _loggerProvider._scopeProvider?.ForEachScope( |
| 134 | + (scopeObject, loggerPovider) => |
| 135 | + { |
| 136 | + loggerPovider.ConnectionLogScope ??= scopeObject as ConnectionLogScope; |
| 137 | + }, |
| 138 | + _loggerProvider); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments