public void GenerateSwaggerDocument_ExcludesDisabledMethods()
        {
            // Arrange
            string apiEndpoint      = "/api/HttpTriggerCSharp1";
            string routeTemplate    = apiEndpoint.Substring(1);
            var    disabledFunction = new FunctionMetadata()
            {
                IsDisabled = true
            };

            Dictionary <IHttpRoute, FunctionDescriptor> httpFunctions = new Dictionary <IHttpRoute, FunctionDescriptor>()
            {
                {
                    new HttpRoute(routeTemplate),
                    new FunctionDescriptor("HttpTriggerCSharp1", null, disabledFunction, null, null, null, null)
                },
            };
            var swaggerDocumentManager = new SwaggerDocumentManager(_scriptConfig);

            // Act
            var generatedDocument = swaggerDocumentManager.GenerateSwaggerDocument(httpFunctions);

            // Assert
            var swaggerdoc = generatedDocument.ToObject <SwaggerDocument>();

            Assert.Equal(swaggerdoc.ApiEndpoints.Keys.Count, 0);
        }
        public void GenerateSwaggerDocument_AddsParameterInfoUsingKnownAndUnknownHttpRouteConstraint()
        {
            // Arrange
            string apiEndpoint   = "/api/{id}/{category}/HttpTriggerCSharp1";
            string routeTemplate = apiEndpoint.Substring(1);

            HttpMethod[] allowedMethods       = { HttpMethod.Get, HttpMethod.Post };
            var          httpMethodConstraint = new HttpMethodConstraint(allowedMethods);

            HttpRouteValueDictionary constraints = new HttpRouteValueDictionary();

            constraints.Add(ScriptConstants.HttpMethodConstraintName, httpMethodConstraint);
            constraints.Add("id", new IntRouteConstraint());

            var httpRoute = new HttpRoute(routeTemplate, null, constraints);
            Dictionary <IHttpRoute, FunctionDescriptor> httpFunctions = new Dictionary <IHttpRoute, FunctionDescriptor>()
            {
                {
                    httpRoute,
                    new FunctionDescriptor("HttpTriggerCSharp1", null, new FunctionMetadata(), null, null, null, null)
                }
            };
            var swaggerDocumentManager = new SwaggerDocumentManager(_scriptConfig);

            // Act
            var generatedDocument = swaggerDocumentManager.GenerateSwaggerDocument(httpFunctions);

            // Assert
            var swaggerdoc = generatedDocument.ToObject <SwaggerDocument>();

            Assert.True(swaggerdoc.ApiEndpoints.ContainsKey(apiEndpoint));
            Assert.Equal(swaggerdoc.ApiEndpoints.Keys.Count, 1);

            var httpOperations = swaggerdoc.ApiEndpoints[apiEndpoint];

            Assert.Equal(httpOperations.Count, 2);

            foreach (var httpMethod in httpMethodConstraint.AllowedMethods)
            {
                Assert.True(httpOperations.ContainsKey(httpMethod.Method.ToString().ToLowerInvariant()));
                Assert.NotNull(httpOperations[httpMethod.Method.ToString().ToLowerInvariant()]);

                var inputParams = httpOperations[httpMethod.Method.ToString().ToLowerInvariant()].InputParameters.ToList();
                Assert.Equal(inputParams.Count, 2);
                Assert.Equal(inputParams[0].DataType, SwaggerDataType.Integer.ToString().ToLowerInvariant());
                Assert.Equal(inputParams[1].DataType, SwaggerDataType.String.ToString().ToLowerInvariant());
            }
        }
        public void GenerateSwaggerDocument_CreatesSwaggerDocument_WithSelectHttpMethods()
        {
            // Arrange
            string apiEndpoint   = "/api/HttpTriggerCSharp1";
            string routeTemplate = apiEndpoint.Substring(1);

            HttpMethod[] allowedMethods       = { HttpMethod.Get, HttpMethod.Post };
            var          httpMethodConstraint = new HttpMethodConstraint(allowedMethods);

            HttpRouteValueDictionary constraints = new HttpRouteValueDictionary();

            constraints.Add(ScriptConstants.HttpMethodConstraintName, httpMethodConstraint);

            var httpRoute = new HttpRoute(routeTemplate, null, constraints);

            Dictionary <IHttpRoute, FunctionDescriptor> httpFunctions = new Dictionary <IHttpRoute, FunctionDescriptor>()
            {
                {
                    httpRoute,
                    new FunctionDescriptor("HttpTriggerCSharp1", null, new FunctionMetadata(), null, null, null, null)
                }
            };

            var swaggerDocumentManager = new SwaggerDocumentManager(_scriptConfig);

            // Act
            var generatedDocument = swaggerDocumentManager.GenerateSwaggerDocument(httpFunctions);

            // Assert
            var swaggerdoc = generatedDocument.ToObject <SwaggerDocument>();

            Assert.True(swaggerdoc.ApiEndpoints.ContainsKey(apiEndpoint));
            Assert.Equal(swaggerdoc.ApiEndpoints.Keys.Count, 1);

            var httpOperations = swaggerdoc.ApiEndpoints[apiEndpoint];

            Assert.Equal(httpOperations.Count, 2);
            foreach (var httpMethod in httpMethodConstraint.AllowedMethods)
            {
                Assert.True(httpOperations.ContainsKey(httpMethod.Method.ToString().ToLowerInvariant()));
                Assert.NotNull(httpOperations[httpMethod.Method.ToString().ToLowerInvariant()]);
            }
        }
        public void GenerateSwaggerDocument_CreatesSwaggerDocument_WithAllHttpMethods()
        {
            string apiEndpoint = "/api/HttpTriggerCSharp1";
            Dictionary <IHttpRoute, FunctionDescriptor> httpFunctions = new Dictionary <IHttpRoute, FunctionDescriptor>()
            {
                { new HttpRoute(apiEndpoint.Substring(1)), new FunctionDescriptor("HttpTriggerCSharp1", null, new FunctionMetadata(), null, null, null, null) }
            };
            var swaggerDocumentManager = new SwaggerDocumentManager(_scriptConfig);
            var generatedDocument      = swaggerDocumentManager.GenerateSwaggerDocument(httpFunctions);
            var swaggerdoc             = generatedDocument.ToObject <SwaggerDocument>();

            Assert.True(swaggerdoc.ApiEndpoints.ContainsKey(apiEndpoint));
            Assert.Equal(swaggerdoc.ApiEndpoints.Keys.Count, 1);

            var httpOperations = swaggerdoc.ApiEndpoints[apiEndpoint];

            Assert.Equal(httpOperations.Count, ScriptConstants.HttpMethods.Length);
            foreach (var httpMethod in ScriptConstants.HttpMethods)
            {
                Assert.True(httpOperations.ContainsKey(httpMethod));
                Assert.NotNull(httpOperations[httpMethod]);
            }
        }
        public void GenerateSwaggerDocument_CreatesBasicSwaggerDocument()
        {
            string apiEndpoint   = "/api/HttpTriggerCSharp1";
            string routeTemplate = apiEndpoint.Substring(1);

            HttpMethod[] allowedMethods       = { HttpMethod.Get };
            var          httpMethodConstraint = new HttpMethodConstraint(allowedMethods);

            HttpRouteValueDictionary constraints = new HttpRouteValueDictionary();

            constraints.Add(ScriptConstants.HttpMethodConstraintName, httpMethodConstraint);

            var httpRoute = new HttpRoute(routeTemplate, null, constraints);

            Dictionary <IHttpRoute, FunctionDescriptor> httpFunctions = new Dictionary <IHttpRoute, FunctionDescriptor>()
            {
                {
                    httpRoute,
                    new FunctionDescriptor("HttpTriggerCSharp1", null, new FunctionMetadata(), null, null, null, null)
                }
            };

            // Act
            var swaggerDocumentManager = new SwaggerDocumentManager(_scriptConfig);

            var generatedDocument = swaggerDocumentManager.GenerateSwaggerDocument(httpFunctions);

            string hostName = Environment.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteHostName);

            if (hostName == null)
            {
                generatedDocument["info"]["title"] = string.Empty;
                generatedDocument["host"]          = string.Empty;
            }

            string expectedSwagger = @"
{
    'swagger': '2.0',
    'info': {
        'title': 'localhost',
        'version': '1.0.0'
    },
    'host': 'localhost',
    'basePath': '/',
    'schemes': [
        'https',
        'http'
    ],
    'paths': {
        '/api/HttpTriggerCSharp1': {
            'get': {
                'operationId': '/api/HttpTriggerCSharp1/get',
                'produces': [],
                'consumes': [],
                'parameters': [],
                'description': 'Replace with Operation Object #http://swagger.io/specification/#operationObject',
                'responses': {
                    '200': {
                        'description': 'Success operation'
                    }
                },
                'security': [
                    {
                        'apikeyQuery': []
    }
                ]
            }
        }
    },
    'definitions': {},
    'securityDefinitions': {
        'apikeyQuery': {
            'type': 'apiKey',
            'name': 'code',
            'in': 'query'
        }
    }
}".Replace("localhost", hostName);

            expectedSwagger = JObject.Parse(expectedSwagger).ToString(Formatting.None);
            Assert.Equal(expectedSwagger, generatedDocument.ToString(Formatting.None));
        }