public void VerifyCustomDocumentationProviderMessage()
        {
            HttpConfiguration config = new HttpConfiguration();

            config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });
            ItemFormatter customFormatter = new ItemFormatter();

            config.Formatters.Add(customFormatter);

            DefaultHttpControllerFactory controllerFactory = ApiExplorerHelper.GetStrictControllerFactory(config, typeof(DocumentationController));

            config.ServiceResolver.SetService(typeof(IHttpControllerFactory), controllerFactory);

            AttributeDocumentationProvider documentationProvider = new AttributeDocumentationProvider();

            config.ServiceResolver.SetService(typeof(IDocumentationProvider), documentationProvider);

            IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();

            foreach (ApiDescription description in explorer.ApiDescriptions)
            {
                Assert.Equal(
                    String.Format("{0} action", description.ActionDescriptor.ActionName),
                    description.Documentation);
                foreach (ApiParameterDescription param in description.ParameterDescriptions)
                {
                    Assert.Equal(
                        String.Format("{0} parameter", param.Name),
                        param.Documentation);
                }
            }
        }
Example #2
0
        public void RegexConstraint_LimitsTheController(Type[] controllerTypes, List <object> expectedResults)
        {
            HttpConfiguration config = new HttpConfiguration();

            config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional }, new { controller = "It.*" }); // controllers that start with "It"

            DefaultHttpControllerFactory controllerFactory = ApiExplorerHelper.GetStrictControllerFactory(config, controllerTypes);

            config.ServiceResolver.SetService(typeof(IHttpControllerFactory), controllerFactory);

            IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();

            ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults);
        }
Example #3
0
        public void HttpMethodConstraints_LimitsTheDescriptions(Type controllerType, List <object> expectedResults)
        {
            HttpConfiguration config = new HttpConfiguration();

            config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional }, new { routeConstraint = new HttpMethodConstraint(HttpMethod.Get, HttpMethod.Put) });

            DefaultHttpControllerFactory controllerFactory = ApiExplorerHelper.GetStrictControllerFactory(config, controllerType);

            config.ServiceResolver.SetService(typeof(IHttpControllerFactory), controllerFactory);

            IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();

            ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults);
        }
        public void VerifyDescription_On_RouteWithActionOnDefaults(Type controllerType, List <object> expectedResults)
        {
            HttpConfiguration config = new HttpConfiguration();

            config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { action = "RemoveItem", id = RouteParameter.Optional });

            DefaultHttpControllerFactory controllerFactory = ApiExplorerHelper.GetStrictControllerFactory(config, controllerType);

            config.ServiceResolver.SetService(typeof(IHttpControllerFactory), controllerFactory);

            IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();

            ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults);
        }
        public void UnknownParameterSource_ShowUpCorrectlyOnDescription()
        {
            HttpConfiguration config = new HttpConfiguration();

            config.Routes.MapHttpRoute("Default", "{controller}/{action}/{id}", new { id = RouteParameter.Optional });
            DefaultHttpControllerFactory controllerFactory = ApiExplorerHelper.GetStrictControllerFactory(config, typeof(ParameterSourceController));

            config.ServiceResolver.SetService(typeof(IHttpControllerFactory), controllerFactory);
            IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();

            ApiDescription description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "GetFromHeaderAttribute");

            Assert.NotNull(description);
            Assert.True(description.ParameterDescriptions.All(param => param.Source == ApiParameterSource.Unknown), "The parameter source should be Unknown.");
        }
        public void InvalidControllerNameOnRoute_DoesNotThrow()
        {
            Type controllerType      = typeof(OverloadsController);
            HttpConfiguration config = new HttpConfiguration();

            config.Routes.MapHttpRoute("Default", "mycontroller/{id}", new { controller = "ControllerThatDoesNotExist", id = RouteParameter.Optional });

            DefaultHttpControllerFactory controllerFactory = ApiExplorerHelper.GetStrictControllerFactory(config, controllerType);

            config.ServiceResolver.SetService(typeof(IHttpControllerFactory), controllerFactory);

            IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();

            Assert.Empty(explorer.ApiDescriptions);
        }
Example #7
0
        public void CustomResponseFormatters_ShowUpOnDescription()
        {
            HttpConfiguration config = new HttpConfiguration();

            config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });
            ItemFormatter customFormatter = new ItemFormatter();

            config.Formatters.Add(customFormatter);

            DefaultHttpControllerFactory controllerFactory = ApiExplorerHelper.GetStrictControllerFactory(config, typeof(ItemController));

            config.ServiceResolver.SetService(typeof(IHttpControllerFactory), controllerFactory);

            IApiExplorer   explorer    = config.ServiceResolver.GetApiExplorer();
            ApiDescription description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "PostItem");

            Assert.True(description.SupportedResponseFormatters.Any(formatter => formatter == customFormatter), "Did not find the custom formatter on the SupportedResponseFormatters.");
        }
        public void FromUriParameterSource_ShowUpCorrectlyOnDescription()
        {
            HttpConfiguration config = new HttpConfiguration();

            config.Routes.MapHttpRoute("Default", "{controller}/{action}/{id}", new { id = RouteParameter.Optional });
            DefaultHttpControllerFactory controllerFactory = ApiExplorerHelper.GetStrictControllerFactory(config, typeof(ParameterSourceController));

            config.ServiceResolver.SetService(typeof(IHttpControllerFactory), controllerFactory);
            IApiExplorer explorer = config.ServiceResolver.GetApiExplorer();

            ApiDescription description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "GetCompleTypeFromUri");

            Assert.NotNull(description);
            Assert.True(description.ParameterDescriptions.All(param => param.Source == ApiParameterSource.FromUri), "All parameters should come from URI.");

            description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "GetCustomFromUriAttribute");
            Assert.NotNull(description);
            Assert.True(description.ParameterDescriptions.Any(param => param.Source == ApiParameterSource.FromUri && param.Name == "value"), "The 'value' parameter should come from URI.");
            Assert.True(description.ParameterDescriptions.Any(param => param.Source == ApiParameterSource.FromBody && param.Name == "bodyValue"), "The 'bodyValue' parameter should come from body.");
        }