示例#1
0
        // See the comments in the base class. DfaMatcher fixes a long-standing bug
        // with catchall parameters and empty segments.
        public override async Task Quirks_CatchAllParameter(string template, string path, string[] keys, string[] values)
        {
            // Arrange
            var(matcher, endpoint)    = CreateMatcher(template);
            var(httpContext, context) = CreateContext(path);

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint, keys, values);
        }
        public virtual async Task Match_WierdCharacterCases(string template, string path, string[] keys, string[] values)
        {
            // Arrange
            var(matcher, endpoint)    = CreateMatcher(template);
            var(httpContext, context) = CreateContext(path);

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint, keys, values);
        }
        public virtual async Task NotMatch_DefaultValues(string template, string path)
        {
            // Arrange
            var(matcher, endpoint)    = CreateMatcher(template);
            var(httpContext, context) = CreateContext(path);

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertNotMatch(context, httpContext);
        }
        public virtual async Task Match_Constraint()
        {
            // Arrange
            var(matcher, endpoint)    = CreateMatcher("/{p:int}");
            var(httpContext, context) = CreateContext("/14");
            var values = new RouteValueDictionary(new { p = "14", });

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint, values);
        }
        public virtual async Task Match_SingleParameter_TrailingSlash()
        {
            // Arrange
            var(matcher, endpoint)    = CreateMatcher("/{p}");
            var(httpContext, context) = CreateContext("/14/");
            var values = new RouteValueDictionary(new { p = "14", });

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint, values);
        }
        public virtual async Task Match_ExtraDefaultValues()
        {
            // Arrange
            var endpoint = CreateEndpoint("/a/{b}/{c}", new { b = "17", c = "18", d = "19" });
            var matcher  = CreateMatcher(endpoint);

            var(httpContext, context) = CreateContext("/a");

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint, new { b = "17", c = "18", d = "19" });
        }
示例#7
0
        public async Task Match_HostWithPort_IncorrectHost()
        {
            // Arrange
            var endpoint = CreateEndpoint("/hello", hosts: new string[] { "contoso.com:8080", });

            var matcher = CreateMatcher(endpoint);

            var(httpContext, context) = CreateContext("/hello", "www.contoso.com:8080");

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertNotMatch(context, httpContext);
        }
示例#8
0
        public async Task Match_Host_Unicode()
        {
            // Arrange
            var endpoint = CreateEndpoint("/hello", hosts: new string[] { "æon.contoso.com", });

            var matcher = CreateMatcher(endpoint);

            var(httpContext, context) = CreateContext("/hello", "æon.contoso.com");

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint);
        }
        public async Task Match_NoMetadata_MatchesAnyProtoMethod()
        {
            // Arrange
            var endpoint = CreateEndpoint("/hello");

            var matcher = CreateMatcher(endpoint);

            var(httpContext, context) = CreateContext("/hello", "GET");

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint);
        }
示例#10
0
        public async Task Match_Port_NoHostHeader_InferProtosPort()
        {
            // Arrange
            var endpoint = CreateEndpoint("/hello", hosts: new string[] { "*:443", });

            var matcher = CreateMatcher(endpoint);

            var(httpContext, context) = CreateContext("/hello", null, "https");

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint);
        }
        public async Task Match_ProtoMethod_CaseInsensitive()
        {
            // Arrange
            var endpoint = CreateEndpoint("/hello", httpMethods: new string[] { "GeT", });

            var matcher = CreateMatcher(endpoint);

            var(httpContext, context) = CreateContext("/hello", "GET");

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint);
        }
        public async Task Match_EmptyMethodList_MatchesAnyProtoMethod()
        {
            // Arrange
            var endpoint = CreateEndpoint("/hello", httpMethods: new string[] { });

            var matcher = CreateMatcher(endpoint);

            var(httpContext, context) = CreateContext("/hello", "GET");

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint);
        }
        public async Task Match_ProtoMethod_CORS_Preflight()
        {
            // Arrange
            var endpoint = CreateEndpoint("/hello", httpMethods: new string[] { "GET", }, acceptCorsPreflight: true);

            var matcher = CreateMatcher(endpoint);

            var(httpContext, context) = CreateContext("/hello", "GET", corsPreflight: true);

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint);
        }
        [Fact] // This matches because the endpoint accepts OPTIONS
        public async Task Match_NoMetadata_MatchesAnyProtoMethod_CORS_Preflight_DoesNotSupportPreflight()
        {
            // Arrange
            var endpoint = CreateEndpoint("/hello", acceptCorsPreflight: false);

            var matcher = CreateMatcher(endpoint);

            var(httpContext, context) = CreateContext("/hello", "GET", corsPreflight: true);

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint);
        }
示例#15
0
        public async Task Match_WildcardHostAndWildcardPort_MatchesAnyHost()
        {
            // Arrange
            var endpoint = CreateEndpoint("/hello", hosts: new string[] { "*:*", });

            var matcher = CreateMatcher(endpoint);

            var(httpContext, context) = CreateContext("/hello", "contoso.com");

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint);
        }
        [Fact] // The non-http-method-specific endpoint is part of the same candidate set
        public async Task Match_EndpointWithProtoMethodPreferred_FallsBackToNonSpecific()
        {
            // Arrange
            var endpoint1 = CreateEndpoint("/{x}", httpMethods: new string[] { "GET", });
            var endpoint2 = CreateEndpoint("/{x}", httpMethods: new string[] { });

            var matcher = CreateMatcher(endpoint1, endpoint2);

            var(httpContext, context) = CreateContext("/hello", "POST");

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint2, ignoreValues: true);
        }
        public async Task Match_EndpointWithProtoMethodPreferred_EmptyList()
        {
            // Arrange
            var endpoint1 = CreateEndpoint("/hello", httpMethods: new string[] { "GET", });
            var endpoint2 = CreateEndpoint("/bar", httpMethods: new string[] { });

            var matcher = CreateMatcher(endpoint1, endpoint2);

            var(httpContext, context) = CreateContext("/hello", "GET");

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint1);
        }
        [Fact] // When one of the candidates handles all verbs, dont use a 405 endpoint
        public async Task NotMatch_ProtoMethod_WithAllMethodEndpoint_DoesNotReturn405()
        {
            // Arrange
            var endpoint1 = CreateEndpoint("/{x:int}", httpMethods: new string[] { });
            var endpoint2 = CreateEndpoint("/hello", httpMethods: new string[] { "DELETE" });

            var matcher = CreateMatcher(endpoint1, endpoint2);

            var(httpContext, context) = CreateContext("/hello", "POST");

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertNotMatch(context, httpContext);
        }
        [Fact] // When all of the candidates handles specific verbs, use a 405 endpoint
        public async Task NotMatch_ProtoMethod_CORS_DoesNotReturn405()
        {
            // Arrange
            var endpoint1 = CreateEndpoint("/hello", httpMethods: new string[] { "GET", "PUT" }, acceptCorsPreflight: true);
            var endpoint2 = CreateEndpoint("/hello", httpMethods: new string[] { "DELETE" });

            var matcher = CreateMatcher(endpoint1, endpoint2);

            var(httpContext, context) = CreateContext("/hello", "POST", corsPreflight: true);

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertNotMatch(context, httpContext);
        }
        public virtual async Task Quirks_CatchAllParameter(string template, string path, string[] keys, string[] values)
        {
            // Arrange
            var(matcher, endpoint)    = CreateMatcher(template);
            var(httpContext, context) = CreateContext(path);

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertNotMatch(context, httpContext);

            // Need to access these to prevent a warning from the xUnit analyzer.
            // Some of these tests will match (and process the values) and some will not.
            GC.KeepAlive(keys);
            GC.KeepAlive(values);
        }
        public virtual async Task Match_SingleParameter_WierdNames()
        {
            // Arrange
            var(matcher, endpoint)    = CreateMatcher("/foo/{ }/{.!$%}/{dynamic.data}");
            var(httpContext, context) = CreateContext("/foo/space/weirdmatch/matcherid");
            var values = new RouteValueDictionary()
            {
                { " ", "space" },
                { ".!$%", "weirdmatch" },
                { "dynamic.data", "matcherid" },
            };

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, endpoint, values);
        }
        public virtual async Task Match_SelectEndpoint_BasedOnOrder(string template1, string template2)
        {
            // Arrange
            var expected = CreateEndpoint(template1, order: 0);
            var other    = CreateEndpoint(template2, order: 1);
            var path     = "/template/5";

            // Arrange
            var matcher = CreateMatcher(other, expected);

            var(httpContext, context) = CreateContext(path);

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, expected, ignoreValues: true);
        }
        public virtual async Task Match_IntegrationTest_MultipleEndpoints(string path, string expectedTemplate)
        {
            // Arrange
            var templates = new[]
            {
                "",
                "Literal1",
                "Literal1/Literal2",
                "Literal1/Literal2/Literal3",
                "Literal1/Literal2/Literal3/{*constrainedCatchAll:int}",
                "Literal1/Literal2/Literal3/{*catchAll}",
                "{constrained1:int}",
                "{constrained1:int}/{constrained2:int}",
                "{constrained1:int}/{constrained2:int}/{constrained3:int}",
                "{constrained1:int}/{constrained2:int}/{constrained3:int}/{*constrainedCatchAll:int}",
                "{constrained1:int}/{constrained2:int}/{constrained3:int}/{*catchAll}",
                "{parameter1}",
                "{parameter1}/{parameter2}",
                "{parameter1}/{parameter2}/{parameter3}",
                "{parameter1}/{parameter2}/{parameter3}/{*constrainedCatchAll:int}",
                "{parameter1}/{parameter2}/{parameter3}/{*catchAll}",
            };

            var endpoints = templates.Select((t) => CreateEndpoint(t)).ToArray();
            var expected  = endpoints[Array.IndexOf(templates, expectedTemplate)];

            var matcher = CreateMatcher(endpoints);

            var(httpContext, context) = CreateContext(path);

            // Act
            await matcher.MatchAsync(httpContext, context);

            // Assert
            MatcherAssert.AssertMatch(context, httpContext, expected, ignoreValues: true);
        }