Beispiel #1
0
    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 = CreateContext("/a");

        // Act
        await matcher.MatchAsync(httpContext);

        // Assert
        MatcherAssert.AssertMatch(httpContext, endpoint, new { b = "17", c = "18", d = "19" });
    }
    public virtual async Task Match_SingleParameter()
    {
        // Arrange
        var(matcher, endpoint) = CreateMatcher("/{p}");
        var httpContext = CreateContext("/14");
        var values      = new RouteValueDictionary(new { p = "14", });

        // Act
        await matcher.MatchAsync(httpContext);

        // Assert
        MatcherAssert.AssertMatch(httpContext, endpoint, values);
    }
    public async Task Match_Port_NoHostHeader_InferHttpsPort()
    {
        // Arrange
        var endpoint = CreateEndpoint("/hello", hosts: new string[] { "*:443", });

        var matcher     = CreateMatcher(endpoint);
        var httpContext = CreateContext("/hello", null, "https");

        // Act
        await matcher.MatchAsync(httpContext);

        // Assert
        MatcherAssert.AssertMatch(httpContext, endpoint);
    }
    public async Task Match_HostWithPort_InferHttpsPort()
    {
        // Arrange
        var endpoint = CreateEndpoint("/hello", hosts: new string[] { "contoso.com:443", });

        var matcher     = CreateMatcher(endpoint);
        var httpContext = CreateContext("/hello", "contoso.com", "https");

        // Act
        await matcher.MatchAsync(httpContext);

        // Assert
        MatcherAssert.AssertMatch(httpContext, endpoint);
    }
    public async Task Match_Host_CaseInsensitive()
    {
        // Arrange
        var endpoint = CreateEndpoint("/hello", hosts: new string[] { "Contoso.COM", });

        var matcher     = CreateMatcher(endpoint);
        var httpContext = CreateContext("/hello", "contoso.com");

        // Act
        await matcher.MatchAsync(httpContext);

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

        var matcher     = CreateMatcher(endpoint);
        var httpContext = CreateContext("/hello", "contoso.com");

        // Act
        await matcher.MatchAsync(httpContext);

        // Assert
        MatcherAssert.AssertMatch(httpContext, endpoint);
    }
    public async Task Match_CatchAllRouteWithMatchingHost_Success()
    {
        // Arrange
        var endpoint = CreateEndpoint("/{**path}", hosts: new string[] { "contoso.com", });

        var matcher     = CreateMatcher(endpoint);
        var httpContext = CreateContext("/hello", "contoso.com");

        // Act
        await matcher.MatchAsync(httpContext);

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

        var matcher     = CreateMatcher(endpoint);
        var httpContext = CreateContext("/hello", requestMethod);

        // Act
        await matcher.MatchAsync(httpContext);

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

        var matcher     = CreateMatcher(endpoint);
        var httpContext = CreateContext("/hello", "GET", corsPreflight: true);

        // Act
        await matcher.MatchAsync(httpContext);

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

        var matcher     = CreateMatcher(endpoint);
        var httpContext = CreateContext("/hello", "GET");

        // Act
        await matcher.MatchAsync(httpContext);

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

        var matcher     = CreateMatcher(endpoint);
        var httpContext = CreateContext("/hello", "GET", corsPreflight: true);

        // Act
        await matcher.MatchAsync(httpContext);

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

        var matcher     = CreateMatcher(endpoint);
        var httpContext = CreateContext("/hello", "GET");

        // Act
        await matcher.MatchAsync(httpContext);

        // Assert
        MatcherAssert.AssertMatch(httpContext, endpoint);
    }
    public async Task Match_HostWithWildcard_Unicode()
    {
        // Arrange
        var endpoint = CreateEndpoint("/hello", hosts: new string[] { "*.contoso.com:8080", });

        var matcher     = CreateMatcher(endpoint);
        var httpContext = CreateContext("/hello", "æon.contoso.com:8080");

        // Act
        await matcher.MatchAsync(httpContext);

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

        var matcher     = CreateMatcher(endpoint1, endpoint2);
        var httpContext = CreateContext("/hello", "POST");

        // Act
        await matcher.MatchAsync(httpContext);

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

        var matcher     = CreateMatcher(endpoint1, endpoint2);
        var httpContext = CreateContext("/hello", "GET");

        // Act
        await matcher.MatchAsync(httpContext);

        // Assert
        MatcherAssert.AssertMatch(httpContext, endpoint1);
    }
Beispiel #16
0
    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 = CreateContext(path);

        // Act
        await matcher.MatchAsync(httpContext);

        // Assert
        MatcherAssert.AssertMatch(httpContext, expected, ignoreValues: true);
    }
    public virtual async Task Match_SingleParameter_WeirdNames()
    {
        // Arrange
        var(matcher, endpoint) = CreateMatcher("/foo/{ }/{.!$%}/{dynamic.data}");
        var httpContext = CreateContext("/foo/space/weirdmatch/matcherid");
        var values      = new RouteValueDictionary()
        {
            { " ", "space" },
            { ".!$%", "weirdmatch" },
            { "dynamic.data", "matcherid" },
        };

        // Act
        await matcher.MatchAsync(httpContext);

        // Assert
        MatcherAssert.AssertMatch(httpContext, endpoint, values);
    }