diff options
Diffstat (limited to 'src/Http/Routing/test/UnitTests/ConstraintMatcherTest.cs')
-rw-r--r-- | src/Http/Routing/test/UnitTests/ConstraintMatcherTest.cs | 347 |
1 files changed, 173 insertions, 174 deletions
diff --git a/src/Http/Routing/test/UnitTests/ConstraintMatcherTest.cs b/src/Http/Routing/test/UnitTests/ConstraintMatcherTest.cs index 949b070350..186c1d60b8 100644 --- a/src/Http/Routing/test/UnitTests/ConstraintMatcherTest.cs +++ b/src/Http/Routing/test/UnitTests/ConstraintMatcherTest.cs @@ -8,245 +8,244 @@ using Microsoft.Extensions.Logging.Testing; using Moq; using Xunit; -namespace Microsoft.AspNetCore.Routing +namespace Microsoft.AspNetCore.Routing; + +public class ConstraintMatcherTest { - public class ConstraintMatcherTest - { - private const string _name = "name"; + private const string _name = "name"; - [Fact] - public void MatchUrlGeneration_DoesNotLogData() - { - // Arrange - var sink = new TestSink(); - var logger = new TestLogger(_name, sink, enabled: true); + [Fact] + public void MatchUrlGeneration_DoesNotLogData() + { + // Arrange + var sink = new TestSink(); + var logger = new TestLogger(_name, sink, enabled: true); - var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); - var constraints = new Dictionary<string, IRouteConstraint> + var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); + var constraints = new Dictionary<string, IRouteConstraint> { {"a", new PassConstraint()}, {"b", new FailConstraint()} }; - // Act - RouteConstraintMatcher.Match( - constraints: constraints, - routeValues: routeValueDictionary, - httpContext: new Mock<HttpContext>().Object, - route: new Mock<IRouter>().Object, - routeDirection: RouteDirection.UrlGeneration, - logger: logger); - - // Assert - // There are no BeginScopes called. - Assert.Empty(sink.Scopes); - - // There are no WriteCores called. - Assert.Empty(sink.Writes); - } + // Act + RouteConstraintMatcher.Match( + constraints: constraints, + routeValues: routeValueDictionary, + httpContext: new Mock<HttpContext>().Object, + route: new Mock<IRouter>().Object, + routeDirection: RouteDirection.UrlGeneration, + logger: logger); + + // Assert + // There are no BeginScopes called. + Assert.Empty(sink.Scopes); + + // There are no WriteCores called. + Assert.Empty(sink.Writes); + } - [Fact] - public void MatchFail_LogsCorrectData() - { - // Arrange & Act - var constraints = new Dictionary<string, IRouteConstraint> + [Fact] + public void MatchFail_LogsCorrectData() + { + // Arrange & Act + var constraints = new Dictionary<string, IRouteConstraint> { {"a", new PassConstraint()}, {"b", new FailConstraint()} }; - var sink = SetUpMatch(constraints, loggerEnabled: true); - var expectedMessage = "Route value 'value' with key 'b' did not match the constraint " + - $"'{typeof(FailConstraint).FullName}'"; - - // Assert - Assert.Empty(sink.Scopes); - var write = Assert.Single(sink.Writes); - Assert.Equal(expectedMessage, write.State?.ToString()); - } + var sink = SetUpMatch(constraints, loggerEnabled: true); + var expectedMessage = "Route value 'value' with key 'b' did not match the constraint " + + $"'{typeof(FailConstraint).FullName}'"; + + // Assert + Assert.Empty(sink.Scopes); + var write = Assert.Single(sink.Writes); + Assert.Equal(expectedMessage, write.State?.ToString()); + } - [Fact] - public void MatchSuccess_DoesNotLog() - { - // Arrange & Act - var constraints = new Dictionary<string, IRouteConstraint> + [Fact] + public void MatchSuccess_DoesNotLog() + { + // Arrange & Act + var constraints = new Dictionary<string, IRouteConstraint> { {"a", new PassConstraint()}, {"b", new PassConstraint()} }; - var sink = SetUpMatch(constraints, false); + var sink = SetUpMatch(constraints, false); - // Assert - Assert.Empty(sink.Scopes); - Assert.Empty(sink.Writes); - } + // Assert + Assert.Empty(sink.Scopes); + Assert.Empty(sink.Writes); + } - [Fact] - public void ReturnsTrueOnValidConstraints() - { - var constraints = new Dictionary<string, IRouteConstraint> + [Fact] + public void ReturnsTrueOnValidConstraints() + { + var constraints = new Dictionary<string, IRouteConstraint> { {"a", new PassConstraint()}, {"b", new PassConstraint()} }; - var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); + var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); - Assert.True(RouteConstraintMatcher.Match( - constraints: constraints, - routeValues: routeValueDictionary, - httpContext: new Mock<HttpContext>().Object, - route: new Mock<IRouter>().Object, - routeDirection: RouteDirection.IncomingRequest, - logger: NullLogger.Instance)); - } + Assert.True(RouteConstraintMatcher.Match( + constraints: constraints, + routeValues: routeValueDictionary, + httpContext: new Mock<HttpContext>().Object, + route: new Mock<IRouter>().Object, + routeDirection: RouteDirection.IncomingRequest, + logger: NullLogger.Instance)); + } - [Fact] - public void ConstraintsGetTheRightKey() - { - var constraints = new Dictionary<string, IRouteConstraint> + [Fact] + public void ConstraintsGetTheRightKey() + { + var constraints = new Dictionary<string, IRouteConstraint> { {"a", new PassConstraint("a")}, {"b", new PassConstraint("b")} }; - var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); + var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); - Assert.True(RouteConstraintMatcher.Match( - constraints: constraints, - routeValues: routeValueDictionary, - httpContext: new Mock<HttpContext>().Object, - route: new Mock<IRouter>().Object, - routeDirection: RouteDirection.IncomingRequest, - logger: NullLogger.Instance)); - } + Assert.True(RouteConstraintMatcher.Match( + constraints: constraints, + routeValues: routeValueDictionary, + httpContext: new Mock<HttpContext>().Object, + route: new Mock<IRouter>().Object, + routeDirection: RouteDirection.IncomingRequest, + logger: NullLogger.Instance)); + } - [Fact] - public void ReturnsFalseOnInvalidConstraintsThatDontMatch() - { - var constraints = new Dictionary<string, IRouteConstraint> + [Fact] + public void ReturnsFalseOnInvalidConstraintsThatDontMatch() + { + var constraints = new Dictionary<string, IRouteConstraint> { {"a", new FailConstraint()}, {"b", new FailConstraint()} }; - var routeValueDictionary = new RouteValueDictionary(new { c = "value", d = "value" }); + var routeValueDictionary = new RouteValueDictionary(new { c = "value", d = "value" }); - Assert.False(RouteConstraintMatcher.Match( - constraints: constraints, - routeValues: routeValueDictionary, - httpContext: new Mock<HttpContext>().Object, - route: new Mock<IRouter>().Object, - routeDirection: RouteDirection.IncomingRequest, - logger: NullLogger.Instance)); - } + Assert.False(RouteConstraintMatcher.Match( + constraints: constraints, + routeValues: routeValueDictionary, + httpContext: new Mock<HttpContext>().Object, + route: new Mock<IRouter>().Object, + routeDirection: RouteDirection.IncomingRequest, + logger: NullLogger.Instance)); + } - [Fact] - public void ReturnsFalseOnInvalidConstraintsThatMatch() - { - var constraints = new Dictionary<string, IRouteConstraint> + [Fact] + public void ReturnsFalseOnInvalidConstraintsThatMatch() + { + var constraints = new Dictionary<string, IRouteConstraint> { {"a", new FailConstraint()}, {"b", new FailConstraint()} }; - var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); + var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); - Assert.False(RouteConstraintMatcher.Match( - constraints: constraints, - routeValues: routeValueDictionary, - httpContext: new Mock<HttpContext>().Object, - route: new Mock<IRouter>().Object, - routeDirection: RouteDirection.IncomingRequest, - logger: NullLogger.Instance)); - } + Assert.False(RouteConstraintMatcher.Match( + constraints: constraints, + routeValues: routeValueDictionary, + httpContext: new Mock<HttpContext>().Object, + route: new Mock<IRouter>().Object, + routeDirection: RouteDirection.IncomingRequest, + logger: NullLogger.Instance)); + } - [Fact] - public void ReturnsFalseOnValidAndInvalidConstraintsMixThatMatch() - { - var constraints = new Dictionary<string, IRouteConstraint> + [Fact] + public void ReturnsFalseOnValidAndInvalidConstraintsMixThatMatch() + { + var constraints = new Dictionary<string, IRouteConstraint> { {"a", new PassConstraint()}, {"b", new FailConstraint()} }; - var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); + var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); - Assert.False(RouteConstraintMatcher.Match( - constraints: constraints, - routeValues: routeValueDictionary, - httpContext: new Mock<HttpContext>().Object, - route: new Mock<IRouter>().Object, - routeDirection: RouteDirection.IncomingRequest, - logger: NullLogger.Instance)); - } + Assert.False(RouteConstraintMatcher.Match( + constraints: constraints, + routeValues: routeValueDictionary, + httpContext: new Mock<HttpContext>().Object, + route: new Mock<IRouter>().Object, + routeDirection: RouteDirection.IncomingRequest, + logger: NullLogger.Instance)); + } - [Fact] - public void ReturnsTrueOnNullInput() - { - Assert.True(RouteConstraintMatcher.Match( - constraints: null, - routeValues: new RouteValueDictionary(), - httpContext: new Mock<HttpContext>().Object, - route: new Mock<IRouter>().Object, - routeDirection: RouteDirection.IncomingRequest, - logger: NullLogger.Instance)); - } + [Fact] + public void ReturnsTrueOnNullInput() + { + Assert.True(RouteConstraintMatcher.Match( + constraints: null, + routeValues: new RouteValueDictionary(), + httpContext: new Mock<HttpContext>().Object, + route: new Mock<IRouter>().Object, + routeDirection: RouteDirection.IncomingRequest, + logger: NullLogger.Instance)); + } - private TestSink SetUpMatch(Dictionary<string, IRouteConstraint> constraints, bool loggerEnabled) + private TestSink SetUpMatch(Dictionary<string, IRouteConstraint> constraints, bool loggerEnabled) + { + // Arrange + var sink = new TestSink(); + var logger = new TestLogger(_name, sink, loggerEnabled); + + var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); + + // Act + RouteConstraintMatcher.Match( + constraints: constraints, + routeValues: routeValueDictionary, + httpContext: new Mock<HttpContext>().Object, + route: new Mock<IRouter>().Object, + routeDirection: RouteDirection.IncomingRequest, + logger: logger); + return sink; + } + + private class PassConstraint : IRouteConstraint + { + private readonly string _expectedKey; + + public PassConstraint(string expectedKey = null) { - // Arrange - var sink = new TestSink(); - var logger = new TestLogger(_name, sink, loggerEnabled); - - var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); - - // Act - RouteConstraintMatcher.Match( - constraints: constraints, - routeValues: routeValueDictionary, - httpContext: new Mock<HttpContext>().Object, - route: new Mock<IRouter>().Object, - routeDirection: RouteDirection.IncomingRequest, - logger: logger); - return sink; + _expectedKey = expectedKey; } - private class PassConstraint : IRouteConstraint + public bool Match( + HttpContext httpContext, + IRouter route, + string routeKey, + RouteValueDictionary values, + RouteDirection routeDirection) { - private readonly string _expectedKey; - - public PassConstraint(string expectedKey = null) + if (_expectedKey != null) { - _expectedKey = expectedKey; + Assert.Equal(_expectedKey, routeKey); } - public bool Match( - HttpContext httpContext, - IRouter route, - string routeKey, - RouteValueDictionary values, - RouteDirection routeDirection) - { - if (_expectedKey != null) - { - Assert.Equal(_expectedKey, routeKey); - } - - return true; - } + return true; } + } - private class FailConstraint : IRouteConstraint + private class FailConstraint : IRouteConstraint + { + public bool Match( + HttpContext httpContext, + IRouter route, + string routeKey, + RouteValueDictionary values, + RouteDirection routeDirection) { - public bool Match( - HttpContext httpContext, - IRouter route, - string routeKey, - RouteValueDictionary values, - RouteDirection routeDirection) - { - return false; - } + return false; } } -}
\ No newline at end of file +} |