Example #1
0
        public void TryExpandUriParameters_CompositeParametersFromUri_Succeeds()
        {
            // Arrange
            string       finalPath;
            const string expectedPath =
                "?id[0]={id[0]}&id[1]={id[1]}&property[0]={property[0]}&property[1]={property[1]}&name={name}";
            List <ApiParameterDescription> descriptions = new List <ApiParameterDescription>()
            {
                CreateApiParameterDescription(typeof(int[]), "id"),
                CreateApiParameterDescription(typeof(ICollection <string>), "property"),
                CreateApiParameterDescription(typeof(string), "name"),
            };

            // Act
            bool isExpanded = ApiExplorer.TryExpandUriParameters(
                new HttpRoute(),
                new HttpParsedRoute(new List <PathSegment>()),
                descriptions,
                out finalPath
                );

            // Assert
            Assert.True(isExpanded);
            Assert.Equal(expectedPath, finalPath);
        }
Example #2
0
        public void Descriptions_RecognizesMixedCaseParameters()
        {
            // Ensure that two "Id"s, one from "api/values/{id}" and another "Id" from ClassWithId,
            // would not cause any exception and only one of them is added.
            var config               = new HttpConfiguration();
            var routeTemplate        = "api/values/{id}";
            var controllerDescriptor = new HttpControllerDescriptor(
                config,
                "ApiExplorerValues",
                typeof(DuplicatedIdController)
                );
            var action = new ReflectedHttpActionDescriptor(
                controllerDescriptor,
                typeof(DuplicatedIdController).GetMethod("Get")
                );
            var actions = new ReflectedHttpActionDescriptor[] { action };

            config.Routes.Add("Route", CreateDirectRoute(routeTemplate, actions));

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            ApiDescription description = Assert.Single(descriptions);

            Assert.Equal(HttpMethod.Get, description.HttpMethod);
            Assert.Equal(routeTemplate, description.RelativePath, StringComparer.OrdinalIgnoreCase);
            Assert.Equal(action, description.ActionDescriptor);
        }
Example #3
0
        public void Descriptions_RecognizesIgnoreApiForDirectRoutes_Action()
        {
            var config               = new HttpConfiguration();
            var routeTemplate        = "api/values";
            var controllerDescriptor = new HttpControllerDescriptor(
                config,
                "ApiExplorerValues",
                typeof(ApiExplorerValuesController)
                );
            var actions = new ReflectedHttpActionDescriptor[]
            {
                new ReflectedHttpActionDescriptor(
                    controllerDescriptor,
                    typeof(ApiExplorerValuesController).GetMethod("Get")
                    ),
                new ReflectedHttpActionDescriptor(
                    controllerDescriptor,
                    typeof(ApiExplorerValuesController).GetMethod("Post")
                    ),
            };

            config.Routes.Add("Route", CreateDirectRoute(routeTemplate, actions));

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            ApiDescription description = Assert.Single(descriptions);

            Assert.Equal(HttpMethod.Get, description.HttpMethod);
            Assert.Equal(routeTemplate, description.RelativePath);
        }
Example #4
0
        public void Descriptions_RecognizesIgnoreApiForDirectRoutes_Controller()
        {
            var config               = new HttpConfiguration();
            var routeTemplate        = "api/values";
            var controllerDescriptor = new HttpControllerDescriptor(
                config,
                "IgnoreApiValues",
                typeof(IgnoreApiValuesController)
                );
            var actions = new ReflectedHttpActionDescriptor[]
            {
                new ReflectedHttpActionDescriptor(
                    controllerDescriptor,
                    typeof(IgnoreApiValuesController).GetMethod("Get")
                    ),
                new ReflectedHttpActionDescriptor(
                    controllerDescriptor,
                    typeof(IgnoreApiValuesController).GetMethod("Post")
                    ),
            };

            config.Routes.Add("Route", CreateDirectRoute(routeTemplate, actions));

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            Assert.Empty(descriptions);
        }
Example #5
0
        public void TryExpandUriParameters_FromUri_Succeeds(
            string expectedPath,
            Type parameterType,
            string parameterName
            )
        {
            // Arrange
            string finalPath;
            List <ApiParameterDescription> descriptions = new List <ApiParameterDescription>()
            {
                CreateApiParameterDescription(parameterType, parameterName),
            };

            // Act
            bool isExpanded = ApiExplorer.TryExpandUriParameters(
                new HttpRoute(),
                new HttpParsedRoute(new List <PathSegment>()),
                descriptions,
                out finalPath
                );

            // Assert
            Assert.True(isExpanded);
            Assert.Equal(expectedPath, finalPath);
        }
Example #6
0
        public void Descriptions_RecognizesCompositeRoutes()
        {
            var config               = new HttpConfiguration();
            var routeTemplate        = "api/values";
            var controllerDescriptor = new HttpControllerDescriptor(
                config,
                "AttributeApiExplorerValues",
                typeof(AttributeApiExplorerValuesController)
                );
            var action = new ReflectedHttpActionDescriptor(
                controllerDescriptor,
                typeof(AttributeApiExplorerValuesController).GetMethod("Action")
                );
            var actions = new ReflectedHttpActionDescriptor[] { action };

            var routeCollection = new List <IHttpRoute>();

            routeCollection.Add(CreateDirectRoute(routeTemplate, actions));

            RouteCollectionRoute route = new RouteCollectionRoute();

            route.EnsureInitialized(() => routeCollection);

            config.Routes.Add("Route", route);

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            ApiDescription description = Assert.Single(descriptions);

            Assert.Equal(HttpMethod.Get, description.HttpMethod);
            Assert.Equal(routeTemplate, description.RelativePath);
            Assert.Equal(action, description.ActionDescriptor);
        }
        public void Descriptions_RecognizesDirectRoutes()
        {
            var config = new HttpConfiguration();
            var routeTemplate = "api/values";
            var controllerDescriptor = new HttpControllerDescriptor(config, "ApiExplorerValues", typeof(ApiExplorerValuesController));
            var action = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(ApiExplorerValuesController).GetMethod("Get"));
            var actions = new ReflectedHttpActionDescriptor[] { action };
            config.Routes.Add("Route", new HttpDirectRoute(routeTemplate, 0, actions));

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            ApiDescription description = Assert.Single(descriptions);
            Assert.Equal(HttpMethod.Get, description.HttpMethod);
            Assert.Equal(routeTemplate, description.RelativePath);
            Assert.Equal(action, description.ActionDescriptor);
        }
        public void Descriptions_RecognizesIgnoreApiForDirectRoutes_Controller()
        {
            var config = new HttpConfiguration();
            var routeTemplate = "api/values";
            var controllerDescriptor = new HttpControllerDescriptor(config, "IgnoreApiValues", typeof(IgnoreApiValuesController));
            var actions = new ReflectedHttpActionDescriptor[] 
            {
                new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(IgnoreApiValuesController).GetMethod("Get")),
                new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(IgnoreApiValuesController).GetMethod("Post")),
            };
            config.Routes.Add("Route", new HttpDirectRoute(routeTemplate, 0, actions));

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            Assert.Empty(descriptions);
        }
Example #9
0
        public void Descriptions_RecognizesDirectRoutes()
        {
            var config = new HttpConfiguration();
            var routeTemplate = "api/values";
            var controllerDescriptor = new HttpControllerDescriptor(config, "ApiExplorerValues", typeof(ApiExplorerValuesController));
            var action = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(ApiExplorerValuesController).GetMethod("Get"));
            var actions = new ReflectedHttpActionDescriptor[] { action };
            config.Routes.Add("Route", HttpRouteBuilder.BuildDirectRoute(routeTemplate, 0, actions));

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            ApiDescription description = Assert.Single(descriptions);
            Assert.Equal(HttpMethod.Get, description.HttpMethod);
            Assert.Equal(routeTemplate, description.RelativePath);
            Assert.Equal(action, description.ActionDescriptor);
        }
        public static List<ApiEndPoint> Get(HttpConfiguration config)
        {
            var apiDescriptions = new ApiExplorer(config).ApiDescriptions;
            var controllers = apiDescriptions
                .GroupBy(x => x.ActionDescriptor.ControllerDescriptor.ControllerName)
                .Select(x => x.First().ActionDescriptor.ControllerDescriptor.ControllerName)
                .ToList();

            var apiEndPoints = new List<ApiEndPoint>();

            foreach (var controller in controllers)
            {
                apiEndPoints.Add(getApiEndPoint(controller, config));
            }

            return apiEndPoints;
        }
Example #11
0
        public void TryExpandUriParameters_EnsureNoKeyConflicts()
        {
            // This test ensures that keys adding to parameterValuesForRoute are case-insensitive
            // and would not cause any exeception if it already has the key. So set up two
            // ApiParameterDescription instances, one with "id" and another with "Id". Act the
            // method and assert that no exception occurs and the output is correct.
            // Arrange
            string expectedExpandedRouteTemplate = "?id={id}";
            string expandedRouteTemplate;
            Mock <HttpParameterDescriptor> parameterDescriptorMock =
                new Mock <HttpParameterDescriptor>();

            parameterDescriptorMock.SetupGet(p => p.ParameterType).Returns(typeof(ClassWithId));
            List <ApiParameterDescription> descriptions = new List <ApiParameterDescription>()
            {
                new ApiParameterDescription()
                {
                    Source = ApiParameterSource.FromUri, Name = "id"
                },
                new ApiParameterDescription()
                {
                    Source = ApiParameterSource.FromUri,
                    ParameterDescriptor = parameterDescriptorMock.Object
                },
            };

            // Act
            bool isExpanded = ApiExplorer.TryExpandUriParameters(
                new HttpRoute(),
                new HttpParsedRoute(new List <PathSegment>()),
                descriptions,
                out expandedRouteTemplate
                );

            // Assert
            Assert.True(isExpanded);
            Assert.Equal(expectedExpandedRouteTemplate, expandedRouteTemplate);
        }
        public IEnumerable<Endpoint> Endpoints()
        {
            var config = ControllerContext.Configuration;// new HttpConfiguration();//
            IApiExplorer apiExplorer = new ApiExplorer(config);//config.Services.GetApiExplorer();

            List<Endpoint> data = new List<Endpoint>();

            foreach (ApiDescription api in apiExplorer.ApiDescriptions)
            {
                Endpoint e = new Endpoint { Method = api.HttpMethod, URI = api.RelativePath, Parameters = new List<EndpointParam>() };

                foreach (ApiParameterDescription parameter in api.ParameterDescriptions)
                {
                    if (parameter != null)
                    {
                        e.Parameters.Add(new EndpointParam { Name = parameter.Name, Source = parameter.Source });
                    }
                }

                data.Add(e);
            }

            return data;
        }
        static ApiEndPoint getApiEndPoint(string controller, HttpConfiguration config)
        {
            string ctrl = controller;
            var apis = new ApiExplorer(config)
             .ApiDescriptions
             .Where(x => x.ActionDescriptor.ControllerDescriptor.ControllerName == ctrl).ToList();

            List<ApiEndPointDetail> apiEndPointDetails = null;

            if (apis.ToList().Count > 0)
            {

                apiEndPointDetails = new List<ApiEndPointDetail>();
                foreach (var api in apis)
                {
                    apiEndPointDetails.Add(getApiEndPointDetail(api));
                }
            }
            else
            {
                controller = string.Format("The {0} api does not exist.", controller);
            }
            return new ApiEndPoint(controller, apiEndPointDetails);
        }
        public string GenerateScript(HttpConfiguration config)
        {
            if (String.IsNullOrEmpty(script))
            {
                lock (lockObject)
                {
                    if (String.IsNullOrEmpty(script))
                    {
                        ApiExplorer apiExplorer = new ApiExplorer(config);

                        IEnumerable<ApiResource> resources = apiExplorer.ApiDescriptions
                                                                .Select(d => new ApiResource(d))
                                                                .GroupBy(r => r.Name)
                                                                .Select(g => ApiResource.Join(g.ToArray()));

                        ScriptProxyTemplate proxyTemplate = new ScriptProxyTemplate(resources);

                        script = proxyTemplate.TransformText();
                    }
                }
            }

            return script;
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="T:System.Object" /> class.
 /// </summary>
 public VersionedApiExplorer(HttpConfiguration configuration) {
     this._configuration = configuration;
     this._defaultApiExplorer = new ApiExplorer(this._configuration);
     this._apiDescriptions = new Lazy<Collection<ApiDescription>>(() => this._defaultApiExplorer.ApiDescriptions);
 }
 public VersionedApiExplorer(HttpConfiguration configuration)
 {
     this.configuration = configuration;
     this.apiDescription = new Lazy<Collection<ApiDescription>>(InitializeApiDescriptions);
     this.DefaultExplorer = new ApiExplorer(configuration);
 }
        public void Descriptions_RecognizesCompositeRoutes()
        {
            var config = new HttpConfiguration();
            var routeTemplate = "api/values";
            var controllerDescriptor = new HttpControllerDescriptor(config, "AttributeApiExplorerValues", typeof(AttributeApiExplorerValuesController));
            var action = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(AttributeApiExplorerValuesController).GetMethod("Action"));
            var actions = new ReflectedHttpActionDescriptor[] { action };

            var routeCollection = new HttpSubRouteCollection();
            routeCollection.Add("testroute", new HttpDirectRoute(routeTemplate, 0, actions));

            RouteCollectionRoute route = new RouteCollectionRoute();
            route.EnsureInitialized(() => routeCollection);

            config.Routes.Add("Route", route);

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            ApiDescription description = Assert.Single(descriptions);
            Assert.Equal(HttpMethod.Get, description.HttpMethod);
            Assert.Equal(routeTemplate, description.RelativePath);
            Assert.Equal(action, description.ActionDescriptor);
        }
 public void Setup()
 {
     // Get ApiExplorer for TestApp
     var httpConfiguration = new HttpConfiguration();
     TestApp.WebApiConfig.Register(httpConfiguration);
     _apiExplorer = new ApiExplorer(httpConfiguration);
 }
        public void Setup()
        {
            // Get ApiExplorer for TestApp
            var httpConfiguration = new HttpConfiguration();
            TestApp.WebApiConfig.Register(httpConfiguration);
            _apiExplorer = new ApiExplorer(httpConfiguration);

            var xmlCommentsDoc = new XPathDocument(String.Format(@"{0}\XmlComments.xml", AppDomain.CurrentDomain.BaseDirectory));
            _filter = new ApplyActionXmlComments(xmlCommentsDoc);
        }
        public void Setup()
        {
            SwaggerSpecConfig.Customize(c =>
                {
                    c.ResolveBasePath(() => "http://tempuri.org");
                    c.PostFilter<AddStandardErrorCodes>();
                    c.PostFilter<AddAuthorizationErrorCodes>();
                });

            // Get ApiExplorer for TestApp
            var httpConfiguration = new HttpConfiguration();
            WebApiConfig.Register(httpConfiguration);
            var apiExplorer = new ApiExplorer(httpConfiguration);

            _swaggerSpec = SwaggerSpec.CreateFrom(apiExplorer);
        }
        public void Setup()
        {
            // Get dummy ApiExplorer
            var config = new HttpConfiguration();
            Dummy.WebApiConfig.Register(config);
            _apiExplorer = new ApiExplorer(config);
            config.EnsureInitialized();

            var xmlCommentsDoc = new XPathDocument(String.Format(@"{0}\XmlComments.xml", AppDomain.CurrentDomain.BaseDirectory));
            _filter = new ApplyActionXmlComments(xmlCommentsDoc);
        }
Example #22
0
        private string HelpContent()
        {
            var sb = new StringBuilder();
            var apiExExplorer = new ApiExplorer(ControllerContext.Configuration);
            foreach (var api in apiExExplorer.ApiDescriptions)
            {
                sb.AppendFormat("<li><a href='{0}'>/{1}</a> - {2} - {3}", ToLink(api.RelativePath), api.RelativePath.ToLower(), api.HttpMethod, api.Documentation);
                //                if (api.ParameterDescriptions.Count > 0)
                //                {
                //                    sb.AppendFormat("<ul>");
                //                    foreach (var parameter in api.ParameterDescriptions)
                //                    {
                //                        sb.AppendFormat("<li>{0}: {1} ({2})</li>", parameter.Name, parameter.Documentation, parameter.Source);
                //                    }
                //                    sb.AppendFormat("</ul></li>");
                //                }
            }

            var content = string.Format(@"
                            <!DOCTYPE html>
                            <html>
                                <head>
                                    <title>httpapi - Request Response Service</title>
                                    <style>
                                        body {{font-family: monospace;font-size:14px;line-height:1.5em;}}
                                        a:visited {{color: blue}}
                                        ul {{list-style: none}}
                                    </style>
                                </head>
                                <body>
            <a href='http://github.com/chandmk/httpapi'><img style='position: absolute; top: 0; right: 0; border: 0;' src='https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png' alt='Fork me on GitHub'></a>
                                    <h1>httpapi - Request Response Service</h1>
                                    <section>
                                    <h3>ENDPOINTS</h3>
                                    <ul>
                                    {0}
                                    </ul>
                                    </section>
                                    <section>
                                        <h3>DESCRIPTION</h3>
                                        <ul><li>
                                        Inspiration for this service is from httpbin.org.</li>
                                        <li>This service provides api for http scenarios. </li>
                                        <li>All endpoints, where it makes sense by default return JSON-encoded content.</li>
                                        <li>You can also request for xml/json by adding respective extension.</li>
                                        <li>
                                        &nbsp;&nbsp;<a href='/get.xml' > /get.xml </a> returns xml content. </li><li>&nbsp;&nbsp;<a href='/get.json' > /get.json </a> returns json content.</li></ul>
                                    </section>
                                </body>
                            </html>", sb);
            return content;
        }
        public void Setup()
        {
            // Basic config
            _config = new SwaggerSpecConfig()
                .ResolveBasePath(() => "http://tempuri.org");

            // Get ApiExplorer for TestApp
            var httpConfiguration = new HttpConfiguration();
            WebApiConfig.Register(httpConfiguration);
            _apiExplorer = new ApiExplorer(httpConfiguration);
        }
 public void Setup()
 {
     // Get dummy ApiExplorer
     var config = new HttpConfiguration();
     Dummy.WebApiConfig.Register(config);
     _apiExplorer = new ApiExplorer(config);
     config.EnsureInitialized();
 }
Example #25
0
        public void Descriptions_RecognizesMixedCaseParameters()
        {
            // Ensure that two "Id"s, one from "api/values/{id}" and another "Id" from ClassWithId,
            // would not cause any exception and only one of them is added.
            var config = new HttpConfiguration();
            var routeTemplate = "api/values/{id}";
            var controllerDescriptor = new HttpControllerDescriptor(config, "ApiExplorerValues", typeof(DuplicatedIdController));
            var action = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(DuplicatedIdController).GetMethod("Get"));
            var actions = new ReflectedHttpActionDescriptor[] { action };
            config.Routes.Add("Route", CreateDirectRoute(routeTemplate, actions));

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            ApiDescription description = Assert.Single(descriptions);
            Assert.Equal(HttpMethod.Get, description.HttpMethod);
            Assert.Equal(routeTemplate, description.RelativePath, StringComparer.OrdinalIgnoreCase);
            Assert.Equal(action, description.ActionDescriptor);
        }