public void UnknownParameterSource_ShowUpCorrectlyOnDescription() { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute("Default", "{controller}/{action}/{id}", new { id = RouteParameter.Optional }); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(ParameterSourceController)); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.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 }); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); Assert.Empty(explorer.ApiDescriptions); }
public void VerifyDescription_OnAttributeRoutes(Type controllerType, List <object> expectedResults) { HttpConfiguration config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); config.EnsureInitialized(); IApiExplorer explorer = config.Services.GetApiExplorer(); ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults); }
public void EmptyDescription_OnAttributeRoutedAction_UsingStandardRoute() { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute("Default", "api/someAction/{id}", new { controller = "Attributed", action = "Get" }); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(AttributedController)); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); config.EnsureInitialized(); IApiExplorer explorer = config.Services.GetApiExplorer(); Assert.Empty(explorer.ApiDescriptions); }
public void FromUriParameterSource_ShowUpCorrectlyOnDescription() { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( "Default", "{controller}/{action}/{id}", new { id = RouteParameter.Optional } ); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector( config, typeof(ParameterSourceController) ); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.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." ); }
public void NoDescription_OnAttributeRoutedAction_UsingStandardControllerRoute() { HttpConfiguration config = new HttpConfiguration(); var route = config.Routes.MapHttpRoute("Default", "api/someController", new { controller = "DefaultRoute" }); config.MapHttpAttributeRoutes(); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(DefaultRouteController)); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); config.EnsureInitialized(); IApiExplorer explorer = config.Services.GetApiExplorer(); Assert.True(explorer.ApiDescriptions.All(d => d.Route != route)); }
public void EnumParameters_ShowUpCorrectlyOnDescription() { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute("Default", "{controller}"); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector( config, typeof(EnumParameterOverloadsController) ); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); ApiDescription description = explorer.ApiDescriptions.FirstOrDefault( desc => desc.ActionDescriptor.ActionName == "GetWithEnumParameter" ); Assert.NotNull(description); ApiParameterDescription parameterDescription = Assert.Single( description.ParameterDescriptions ); Assert.Equal(ApiParameterSource.FromUri, parameterDescription.Source); Assert.Equal("EnumParameterOverloads?scope={scope}", description.RelativePath); description = explorer.ApiDescriptions.FirstOrDefault( desc => desc.ActionDescriptor.ActionName == "GetWithTwoEnumParameters" ); Assert.NotNull(description); Assert.Equal(2, description.ParameterDescriptions.Count); Assert.Equal(ApiParameterSource.FromUri, description.ParameterDescriptions[0].Source); Assert.Equal(ApiParameterSource.FromUri, description.ParameterDescriptions[1].Source); Assert.Equal( "EnumParameterOverloads?level={level}&kind={kind}", description.RelativePath ); description = explorer.ApiDescriptions.FirstOrDefault( desc => desc.ActionDescriptor.ActionName == "GetWithNullableEnumParameter" ); Assert.NotNull(description); parameterDescription = Assert.Single(description.ParameterDescriptions); Assert.Equal(ApiParameterSource.FromUri, parameterDescription.Source); Assert.Equal("EnumParameterOverloads?level={level}", description.RelativePath); }
public void DeclaredResponseType_AppearsOnApiDescription(string actionName, Type declaredType, Type responseType) { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional }); Type controllerToTest = typeof(ResponseTypeController); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerToTest); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); Collection <ApiDescription> apis = config.Services.GetApiExplorer().ApiDescriptions; ApiDescription expectedApi = apis.FirstOrDefault(api => api.ActionDescriptor.ActionName == actionName && api.ResponseDescription.DeclaredType == declaredType && api.ResponseDescription.ResponseType == responseType); Assert.NotNull(expectedApi); }
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); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(ItemController)); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.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 RelativePath_IsCorrectForTypesFromUri(string routeTemplate, string expectedQueryString, string methodName) { // Arrange HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute("Default", routeTemplate, new { controller = "ApiExplorerActionsWithParameters", action = methodName }); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(ApiExplorerActionsWithParametersController)); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); // Act ApiDescription description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == methodName); // Assert Assert.NotNull(description); Assert.Equal(routeTemplate + expectedQueryString, description.RelativePath); }
public void VerifyDescriptionIsNotGeneratedForAmbiguousAction_OnDefaultRoutes() { Type controllerType = typeof(AmbiguousActionController); HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional }); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); Collection <ApiDescription> descriptions = explorer.ApiDescriptions; List <object> expectedResults = new List <object> { new { HttpMethod = HttpMethod.Post, RelativePath = "AmbiguousAction/{id}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 1 } }; ApiExplorerHelper.VerifyApiDescriptions(descriptions, expectedResults); }
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); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(DocumentationController)); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); AttributeDocumentationProvider documentationProvider = new AttributeDocumentationProvider(); config.Services.Replace(typeof(IDocumentationProvider), documentationProvider); IApiExplorer explorer = config.Services.GetApiExplorer(); foreach (ApiDescription description in explorer.ApiDescriptions) { Assert.Equal( "Documentation controller", documentationProvider.GetDocumentation(description.ActionDescriptor.ControllerDescriptor)); 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); } if (description.ResponseDescription.DeclaredType != null) { Assert.Equal( String.Format("{0} response", description.ActionDescriptor.ActionName), description.ResponseDescription.Documentation); } } }
public void VerifyDescription_OnRouteWithControllerOnDefaults( Type controllerType, List <object> expectedResults ) { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( "Default", "myitem/{id}", new { controller = "Item", id = RouteParameter.Optional } ); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults); }
public void HiddenAction_DoesNotShowUpOnDescription( Type controllerType, List <object> expectedResults ) { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( "Default", "{controller}/{id}", new { id = RouteParameter.Optional } ); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults); }
public void RegexConstraint_LimitsTheAction( Type[] controllerTypes, List <object> expectedResults ) { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( "Default", "{controller}/{action}/{id}", new { id = RouteParameter.Optional }, new { action = "Get.+" } ); // actions that start with "Get" and at least one extra character DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerTypes); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults); }
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) } ); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); ApiExplorerHelper.VerifyApiDescriptions(explorer.ApiDescriptions, expectedResults); }
public void VerifyDefaultDocumentationMessage() { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional }); ItemFormatter customFormatter = new ItemFormatter(); config.Formatters.Add(customFormatter); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(ItemController)); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); foreach (ApiDescription description in explorer.ApiDescriptions) { Assert.Null(description.Documentation); foreach (ApiParameterDescription param in description.ParameterDescriptions) { Assert.Null(param.Documentation); } } }
public void SupportedResponseFormatters_ReturnsFormattersWithoutTracers_WithNoTracing() { // Arrange HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional }); ItemFormatter customFormatter = new ItemFormatter(); config.Formatters.Add(customFormatter); config.Initializer.Invoke(config); int expectedFormatterCount = config.Formatters.Count - 2; DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, typeof(ItemController)); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); // Act ApiDescription description = explorer.ApiDescriptions.FirstOrDefault(desc => desc.ActionDescriptor.ActionName == "PostItem"); Collection <MediaTypeFormatter> formatters = description.SupportedResponseFormatters; // Assert Assert.False(formatters.Any(f => f is IFormatterTracer), "Tracers are present"); Assert.Equal(expectedFormatterCount, formatters.Count); }
public void VerifyOnlyOneSetOfDescriptionIsGenerated_OnTwoMatchingRoutes() { Type controllerType = typeof(ItemController); HttpConfiguration config = new HttpConfiguration(); IHttpRoute matchingRoute = config.Routes.MapHttpRoute( "Item", "Item/{id}", new { id = RouteParameter.Optional, controller = "Item" } ); config.Routes.MapHttpRoute( "Default", "{controller}/{id}", new { id = RouteParameter.Optional } ); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); Collection <ApiDescription> descriptions = explorer.ApiDescriptions; foreach (ApiDescription description in descriptions) { Assert.Same(matchingRoute, description.Route); } List <object> expectedResults = new List <object> { new { HttpMethod = HttpMethod.Get, RelativePath = "Item?name={name}&series={series}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 2 }, new { HttpMethod = HttpMethod.Post, RelativePath = "Item", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1 }, new { HttpMethod = HttpMethod.Put, RelativePath = "Item", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 1 }, new { HttpMethod = HttpMethod.Delete, RelativePath = "Item/{id}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 1 } }; ApiExplorerHelper.VerifyApiDescriptions(descriptions, expectedResults); }
public void VerifyDescriptionIsGenerated_WhenRouteParameterIsNotInAction() { Type controllerType = typeof(ItemController); HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( "Custom", "{majorVersion}/{minorVersion}/custom", new { controller = "Item" } ); config.Routes.MapHttpRoute( "Default", "{version}/{controller}/{id}", new { id = RouteParameter.Optional } ); DefaultHttpControllerSelector controllerSelector = ApiExplorerHelper.GetStrictControllerSelector(config, controllerType); config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); IApiExplorer explorer = config.Services.GetApiExplorer(); Collection <ApiDescription> descriptions = explorer.ApiDescriptions; List <object> expectedResults = new List <object> { new { HttpMethod = HttpMethod.Get, RelativePath = "{majorVersion}/{minorVersion}/custom?name={name}&series={series}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 4 }, new { HttpMethod = HttpMethod.Post, RelativePath = "{majorVersion}/{minorVersion}/custom", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 3 }, new { HttpMethod = HttpMethod.Put, RelativePath = "{majorVersion}/{minorVersion}/custom", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 3 }, new { HttpMethod = HttpMethod.Delete, RelativePath = "{majorVersion}/{minorVersion}/custom?id={id}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 3 }, new { HttpMethod = HttpMethod.Get, RelativePath = "{version}/Item?name={name}&series={series}", HasRequestFormatters = false, HasResponseFormatters = true, NumberOfParameters = 3 }, new { HttpMethod = HttpMethod.Post, RelativePath = "{version}/Item", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 2 }, new { HttpMethod = HttpMethod.Put, RelativePath = "{version}/Item", HasRequestFormatters = true, HasResponseFormatters = true, NumberOfParameters = 2 }, new { HttpMethod = HttpMethod.Delete, RelativePath = "{version}/Item/{id}", HasRequestFormatters = false, HasResponseFormatters = false, NumberOfParameters = 2 } }; ApiExplorerHelper.VerifyApiDescriptions(descriptions, expectedResults); }