Initial DispatcherMiddleware tests (#529)

This commit is contained in:
James Newton-King 2018-06-07 21:47:06 +12:00 committed by GitHub
parent edf26be1bf
commit 21f8ce5e95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 174 additions and 7 deletions

View File

@ -64,7 +64,11 @@ namespace Microsoft.AspNetCore.Routing
await matcher.MatchAsync(httpContext, feature);
if (feature.Endpoint != null)
{
Log.EndpointMatched(_logger, feature);
Log.MatchSuccess(_logger, feature);
}
else
{
Log.MatchFailure(_logger);
}
await _next(httpContext);
@ -101,14 +105,24 @@ namespace Microsoft.AspNetCore.Routing
private static class Log
{
private static readonly Action<ILogger, string, Exception> _matchSuccess = LoggerMessage.Define<string>(
LogLevel.Information,
new EventId(0, "MatchSuccess"),
LogLevel.Debug,
new EventId(1, "MatchSuccess"),
"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);
}
public static void MatchFailure(ILogger logger)
{
_matchFailure(logger, null);
}
}
}
}

View File

@ -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());
}
}
}

View File

@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
<RootNamespace>Microsoft.AspNetCore.Routing</RootNamespace>
</PropertyGroup>
<ItemGroup>

View File

@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Routing
public class RouterMiddlewareTest
{
[Fact]
public async void Invoke_LogsCorrectValues_WhenNotHandled()
public async Task Invoke_LogsCorrectValues_WhenNotHandled()
{
// Arrange
var expectedMessage = "Request did not match any routes.";
@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.Routing
}
[Fact]
public async void Invoke_DoesNotLog_WhenHandled()
public async Task Invoke_DoesNotLog_WhenHandled()
{
// Arrange
var isHandled = true;

View File

@ -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)
{
}
}
}

View File

@ -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;
}
}
}

View File

@ -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);
}
}
}

View File

@ -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();
}
}
}