Initial DispatcherMiddleware tests (#529)
This commit is contained in:
parent
edf26be1bf
commit
21f8ce5e95
|
|
@ -64,7 +64,11 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
await matcher.MatchAsync(httpContext, feature);
|
await matcher.MatchAsync(httpContext, feature);
|
||||||
if (feature.Endpoint != null)
|
if (feature.Endpoint != null)
|
||||||
{
|
{
|
||||||
Log.EndpointMatched(_logger, feature);
|
Log.MatchSuccess(_logger, feature);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.MatchFailure(_logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
await _next(httpContext);
|
await _next(httpContext);
|
||||||
|
|
@ -101,14 +105,24 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
private static class Log
|
private static class Log
|
||||||
{
|
{
|
||||||
private static readonly Action<ILogger, string, Exception> _matchSuccess = LoggerMessage.Define<string>(
|
private static readonly Action<ILogger, string, Exception> _matchSuccess = LoggerMessage.Define<string>(
|
||||||
LogLevel.Information,
|
LogLevel.Debug,
|
||||||
new EventId(0, "MatchSuccess"),
|
new EventId(1, "MatchSuccess"),
|
||||||
"Request matched endpoint '{EndpointName}'.");
|
"Request matched endpoint '{EndpointName}'.");
|
||||||
|
|
||||||
public static void EndpointMatched(ILogger logger, EndpointFeature feature)
|
private static readonly Action<ILogger, Exception> _matchFailure = LoggerMessage.Define(
|
||||||
|
LogLevel.Debug,
|
||||||
|
new EventId(2, "MatchFailure"),
|
||||||
|
"Request did not match any endpoints.");
|
||||||
|
|
||||||
|
public static void MatchSuccess(ILogger logger, EndpointFeature feature)
|
||||||
{
|
{
|
||||||
_matchSuccess(logger, feature.Endpoint.DisplayName, null);
|
_matchSuccess(logger, feature.Endpoint.DisplayName, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void MatchFailure(ILogger logger)
|
||||||
|
{
|
||||||
|
_matchFailure(logger, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,72 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Routing.Matchers;
|
||||||
|
using Microsoft.AspNetCore.Routing.TestObjects;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
using Microsoft.Extensions.Logging.Testing;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Routing
|
||||||
|
{
|
||||||
|
public class DispatcherMiddlewareTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Invoke_OnCall_SetsEndpointFeature()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var httpContext = new DefaultHttpContext();
|
||||||
|
httpContext.RequestServices = new TestServiceProvider();
|
||||||
|
|
||||||
|
RequestDelegate next = (c) => Task.FromResult<object>(null);
|
||||||
|
|
||||||
|
var logger = new Logger<DispatcherMiddleware>(NullLoggerFactory.Instance);
|
||||||
|
var options = Options.Create(new DispatcherOptions());
|
||||||
|
var matcherFactory = new TestMatcherFactory(false);
|
||||||
|
var middleware = new DispatcherMiddleware(matcherFactory, options, logger, next);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await middleware.Invoke(httpContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var endpointFeature = httpContext.Features.Get<IEndpointFeature>();
|
||||||
|
Assert.NotNull(endpointFeature);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Invoke_OnCall_WritesToConfiguredLogger()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expectedMessage = "Request matched endpoint 'Test endpoint'.";
|
||||||
|
|
||||||
|
var sink = new TestSink(
|
||||||
|
TestSink.EnableWithTypeName<DispatcherMiddleware>,
|
||||||
|
TestSink.EnableWithTypeName<DispatcherMiddleware>);
|
||||||
|
var loggerFactory = new TestLoggerFactory(sink, enabled: true);
|
||||||
|
|
||||||
|
var httpContext = new DefaultHttpContext();
|
||||||
|
httpContext.RequestServices = new TestServiceProvider();
|
||||||
|
|
||||||
|
RequestDelegate next = (c) => Task.FromResult<object>(null);
|
||||||
|
|
||||||
|
var logger = new Logger<DispatcherMiddleware>(loggerFactory);
|
||||||
|
var options = Options.Create(new DispatcherOptions());
|
||||||
|
var matcherFactory = new TestMatcherFactory(true);
|
||||||
|
var middleware = new DispatcherMiddleware(matcherFactory, options, logger, next);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await middleware.Invoke(httpContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Empty(sink.Scopes);
|
||||||
|
var write = Assert.Single(sink.Writes);
|
||||||
|
Assert.Equal(expectedMessage, write.State?.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
|
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
|
||||||
|
<RootNamespace>Microsoft.AspNetCore.Routing</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
public class RouterMiddlewareTest
|
public class RouterMiddlewareTest
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public async void Invoke_LogsCorrectValues_WhenNotHandled()
|
public async Task Invoke_LogsCorrectValues_WhenNotHandled()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var expectedMessage = "Request did not match any routes.";
|
var expectedMessage = "Request did not match any routes.";
|
||||||
|
|
@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async void Invoke_DoesNotLog_WhenHandled()
|
public async Task Invoke_DoesNotLog_WhenHandled()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var isHandled = true;
|
var isHandled = true;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Routing.TestObjects
|
||||||
|
{
|
||||||
|
internal class TestEndpoint : Endpoint
|
||||||
|
{
|
||||||
|
public TestEndpoint(EndpointMetadataCollection metadata, string displayName) : base(metadata, displayName)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Routing.Matchers;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Routing.TestObjects
|
||||||
|
{
|
||||||
|
internal class TestMatcher : Matcher
|
||||||
|
{
|
||||||
|
private readonly bool _isHandled;
|
||||||
|
|
||||||
|
public TestMatcher(bool isHandled)
|
||||||
|
{
|
||||||
|
_isHandled = isHandled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Task MatchAsync(HttpContext httpContext, IEndpointFeature feature)
|
||||||
|
{
|
||||||
|
if (_isHandled)
|
||||||
|
{
|
||||||
|
feature.Endpoint = new TestEndpoint(EndpointMetadataCollection.Empty, "Test endpoint");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Routing.Matchers;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Routing.TestObjects
|
||||||
|
{
|
||||||
|
internal class TestMatcherFactory : MatcherFactory
|
||||||
|
{
|
||||||
|
private readonly bool _isHandled;
|
||||||
|
|
||||||
|
public TestMatcherFactory(bool isHandled)
|
||||||
|
{
|
||||||
|
_isHandled = isHandled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Matcher CreateMatcher(EndpointDataSource dataSource)
|
||||||
|
{
|
||||||
|
return new TestMatcher(_isHandled);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Routing.TestObjects
|
||||||
|
{
|
||||||
|
internal class TestServiceProvider : IServiceProvider
|
||||||
|
{
|
||||||
|
public object GetService(Type serviceType)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue