public void GetApiConvention_ReturnsResultFromFirstMatchingConvention()
        {
            // Arrange
            var method = typeof(GetApiConvention_ReturnsResultFromFirstMatchingConventionController)
                         .GetMethod(nameof(GetApiConvention_ReturnsResultFromFirstMatchingConventionController.Get));
            var attributes = new[]
            {
                new ApiConventionTypeAttribute(typeof(GetApiConvention_ReturnsResultFromConventionType)),
                new ApiConventionTypeAttribute(typeof(DefaultApiConventions)),
            };

            // Act
            var result = ApiConventionResult.TryGetApiConvention(method, attributes, result: out var conventionResult);

            // Assert
            Assert.True(result);
            Assert.Collection(
                conventionResult.ResponseMetadataProviders.OrderBy(o => o.StatusCode),
                r => Assert.Equal(200, r.StatusCode),
                r => Assert.Equal(202, r.StatusCode),
                r => Assert.Equal(404, r.StatusCode));
        }
Beispiel #2
0
        internal static bool TryGetApiConvention(
            MethodInfo method,
            ApiConventionTypeAttribute[] apiConventionAttributes,
            out ApiConventionResult result)
        {
            foreach (var attribute in apiConventionAttributes)
            {
                var conventionMethod = GetConventionMethod(method, attribute.ConventionType);
                if (conventionMethod != null)
                {
                    var metadataProviders = conventionMethod.GetCustomAttributes(inherit: false)
                                            .OfType <IApiResponseMetadataProvider>()
                                            .ToArray();

                    result = new ApiConventionResult(metadataProviders);
                    return(true);
                }
            }

            result = null;
            return(false);
        }
        public void GetApiConvention_DeleteAction_MatchesDefaultConvention()
        {
            // Arrange
            var method = typeof(DefaultConventionController)
                         .GetMethod(nameof(DefaultConventionController.Delete));
            var conventions = new[]
            {
                new ApiConventionTypeAttribute(typeof(DefaultApiConventions)),
            };

            // Act
            var result = ApiConventionResult.TryGetApiConvention(method, conventions, out var conventionResult);

            // Assert
            Assert.True(result);
            Assert.Collection(
                conventionResult.ResponseMetadataProviders.OrderBy(o => o.StatusCode),
                r => Assert.IsAssignableFrom <IApiDefaultResponseMetadataProvider>(r),
                r => Assert.Equal(200, r.StatusCode),
                r => Assert.Equal(400, r.StatusCode),
                r => Assert.Equal(404, r.StatusCode));
        }