/// <summary>
    /// Initializes an <see cref="ApiConventionMethodAttribute"/> instance using <paramref name="conventionType"/> and
    /// the specified <paramref name="methodName"/>.
    /// </summary>
    /// <param name="conventionType">
    /// The <see cref="Type"/> of the convention.
    /// <para>
    /// Conventions must be static types. Methods in a convention are
    /// matched to an action method using rules specified by <see cref="ApiConventionNameMatchAttribute" />
    /// that may be applied to a method name or its parameters and <see cref="ApiConventionTypeMatchAttribute"/>
    /// that are applied to parameters.
    /// </para>
    /// </param>
    /// <param name="methodName">The method name.</param>
    public ApiConventionMethodAttribute(Type conventionType, string methodName)
    {
        ConventionType = conventionType ?? throw new ArgumentNullException(nameof(conventionType));
        ApiConventionTypeAttribute.EnsureValid(conventionType);

        if (string.IsNullOrEmpty(methodName))
        {
            throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(methodName));
        }

        Method = GetConventionMethod(conventionType, methodName);
    }
Пример #2
0
    public void GetApiConvention_ReturnsNull_IfNoConventionMatches()
    {
        // Arrange
        var method    = typeof(GetApiConvention_ReturnsNull_IfNoConventionMatchesController).GetMethod(nameof(GetApiConvention_ReturnsNull_IfNoConventionMatchesController.NoMatch));
        var attribute = new ApiConventionTypeAttribute(typeof(DefaultApiConventions));

        // Act
        var result = ApiConventionResult.TryGetApiConvention(method, new[] { attribute }, out var conventionResult);

        // Assert
        Assert.False(result);
        Assert.Null(conventionResult);
    }
Пример #3
0
    public void GetApiConvention_ReturnsResultFromConvention()
    {
        // Arrange
        var method = typeof(GetApiConvention_ReturnsResultFromConventionController)
                     .GetMethod(nameof(GetApiConvention_ReturnsResultFromConventionController.Match));
        var attribute = new ApiConventionTypeAttribute(typeof(GetApiConvention_ReturnsResultFromConventionType));

        // Act
        var result = ApiConventionResult.TryGetApiConvention(method, new[] { attribute }, out var conventionResult);

        // Assert
        Assert.True(result);
        Assert.Collection(
            conventionResult.ResponseMetadataProviders.OrderBy(o => o.StatusCode),
            r => Assert.Equal(201, r.StatusCode),
            r => Assert.Equal(403, r.StatusCode));
    }