Esempio n. 1
0
        public void SwaggerToPostmanConverter_ReturnsValidDescription()
        {
            SwaggerToPostmanConverter converter = new SwaggerToPostmanConverter(_swaggerProviderMock.Object, _operationConverterMock.Object);
            PostmanRootCollection     result    = converter.GetPostmanCollection("v1", "mysite.com", "http://mysite.com/");

            Assert.Equal(_validSwaggerDoc.Info.Description, result.Info.Description.Content);
        }
        public PostmanRootCollection GetPostmanCollection(string swaggerDocumentName, string host = null, string basePath = null, string[] schemes = null)
        {
            SwaggerDocument swagger = swaggerProvider.GetSwagger(swaggerDocumentName, host, basePath, schemes);

            string apiBasePath = swagger.BasePath;

            PostmanRootCollection postmanRoot = new PostmanRootCollection();

            postmanRoot.Info = new PostmanCollectionInfo
            {
                PostmanId = Guid.NewGuid().ToString(),
                Schema    = "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
            };
            if (swagger.Info != null)
            {
                postmanRoot.Info.Name        = swagger.Info.Title ?? "";
                postmanRoot.Info.Version     = swagger.Info.Version ?? "";
                postmanRoot.Info.Description = new PostmanDescription(swagger.Info.Description ?? "");
                postmanRoot.Description      = new PostmanDescription(swagger.Info.Description ?? "");
            }

            //TODO: Organise results into folders based on grouping
            //PostmanFolder folder = new PostmanFolder() { Name = "folderTitle", Id = "folderId" };
            foreach (var pathKey in swagger.Paths.Keys)
            {
                var path = swagger.Paths[pathKey];

                if (path.Get != null)
                {
                    postmanRoot.Items.Add(this.operationConverter.Convert(pathKey, PostmanHttpMethod.GET, path.Get, swagger));
                }
                if (path.Post != null)
                {
                    postmanRoot.Items.Add(this.operationConverter.Convert(pathKey, PostmanHttpMethod.POST, path.Post, swagger));
                }
                if (path.Put != null)
                {
                    postmanRoot.Items.Add(this.operationConverter.Convert(pathKey, PostmanHttpMethod.PUT, path.Put, swagger));
                }
                if (path.Delete != null)
                {
                    postmanRoot.Items.Add(this.operationConverter.Convert(pathKey, PostmanHttpMethod.DELETE, path.Delete, swagger));
                }
                if (path.Head != null)
                {
                    postmanRoot.Items.Add(this.operationConverter.Convert(pathKey, PostmanHttpMethod.HEAD, path.Head, swagger));
                }
                if (path.Patch != null)
                {
                    postmanRoot.Items.Add(this.operationConverter.Convert(pathKey, PostmanHttpMethod.PATCH, path.Patch, swagger));
                }
                if (path.Options != null)
                {
                    postmanRoot.Items.Add(this.operationConverter.Convert(pathKey, PostmanHttpMethod.OPTIONS, path.Options, swagger));
                }
            }
            //postmanRoot.Items.Add(folder);

            return(postmanRoot);
        }
Esempio n. 3
0
        public void SwaggerToPostmanConverter_InfoIsNotNull()
        {
            SwaggerToPostmanConverter converter = new SwaggerToPostmanConverter(_swaggerProviderMock.Object, _operationConverterMock.Object);
            PostmanRootCollection     result    = converter.GetPostmanCollection("v1", "mysite.com", "http://mysite.com/");

            Assert.NotNull(result.Info);
        }
Esempio n. 4
0
        public void SwaggerToPostmanConverter_ReturnsCorrectPostmanSchema()
        {
            SwaggerToPostmanConverter converter = new SwaggerToPostmanConverter(_swaggerProviderMock.Object, _operationConverterMock.Object);
            PostmanRootCollection     result    = converter.GetPostmanCollection("v1", "mysite.com", "http://mysite.com/");

            Assert.Equal("https://schema.getpostman.com/json/collection/v2.1.0/collection.json", result.Info.Schema);
        }
Esempio n. 5
0
        public void SwaggerToPostmanConverter_ReturnsCorrectNumberOfCollectionItem_ForSecondPath()
        {
            SwaggerToPostmanConverter converter = new SwaggerToPostmanConverter(_swaggerProviderMock.Object, _operationConverterMock.Object);
            PostmanRootCollection     result    = converter.GetPostmanCollection("v1", "mysite.com", "http://mysite.com/");

            Assert.Equal(0, result.Items.OfType <PostmanCollectionItem>().Count(i => i.Name == "/api/item2" && i.Request.Method == PostmanHttpMethod.GET));
            Assert.Equal(0, result.Items.OfType <PostmanCollectionItem>().Count(i => i.Name == "/api/item2" && i.Request.Method == PostmanHttpMethod.POST));
            Assert.Equal(1, result.Items.OfType <PostmanCollectionItem>().Count(i => i.Name == "/api/item2" && i.Request.Method == PostmanHttpMethod.PUT));
            Assert.Equal(1, result.Items.OfType <PostmanCollectionItem>().Count(i => i.Name == "/api/item2" && i.Request.Method == PostmanHttpMethod.DELETE));
        }
Esempio n. 6
0
        public IActionResult Get()
        {
            string swaggerDocName            = "v1";
            PostmanRootCollection collection = postmanConverter.GetPostmanCollection(swaggerDocName, "localhost", "http://localhost:24724/");

            JsonSerializerSettings jsonSettings = new JsonSerializerSettings();

            jsonSettings.NullValueHandling = NullValueHandling.Ignore;
            jsonSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

            return(Json(collection, jsonSettings));
        }
Esempio n. 7
0
        public async Task Invoke(HttpContext httpContext)
        {
            string documentName;

            if (!PathMatchesPostmanDoc(httpContext.Request, out documentName))
            {
                await _next(httpContext);

                return;
            }

            var basePath = string.IsNullOrEmpty(httpContext.Request.PathBase)
                ? null
                : httpContext.Request.PathBase.ToString();
            var            host           = httpContext.Request.Host != null ? httpContext.Request.Host.Value : "";
            JsonSerializer jsonSerializer = new JsonSerializer()
            {
                Formatting        = Formatting.Indented,
                NullValueHandling = NullValueHandling.Ignore
            };

            jsonSerializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
            var jsonBuilder = new StringBuilder();

            using (var writer = new StringWriter(jsonBuilder))
            {
                httpContext.Response.ContentType = "application/json";

                try
                {
                    PostmanRootCollection postmanCollection = _postmanConverter.GetPostmanCollection(documentName, host, basePath);

                    httpContext.Response.StatusCode = 200;
                    jsonSerializer.Serialize(writer, postmanCollection);
                    await httpContext.Response.WriteAsync(jsonBuilder.ToString(), new UTF8Encoding(false));
                }
                catch (UnknownSwaggerDocument)
                {
                    httpContext.Response.StatusCode = 404;
                    jsonSerializer.Serialize(writer, new ErrorResponse
                    {
                        Message = $"Unknown document '{documentName??""}'"
                    });
                    await httpContext.Response.WriteAsync(jsonBuilder.ToString(), new UTF8Encoding(false));
                }
            }
        }