Esempio n. 1
0
        public void UrlObjectConverter_ProducesExpectedRawUrlFromHostBasePathAndPath(string host, string basePath, string path, string expectedRawUrl)
        {
            UrlObjectConverter converter = new UrlObjectConverter(new DefaultValueFactory());
            PostmanUrl         result    = converter.Convert(path, new List <IParameter>(), host, basePath);

            Assert.Equal(expectedRawUrl, result.Raw);
        }
Esempio n. 2
0
        public void UrlObjectConverter_ProducesExpectedHost(string host, string expectedhost)
        {
            UrlObjectConverter converter = new UrlObjectConverter(new DefaultValueFactory());
            PostmanUrl         result    = converter.Convert(_validPath, _validParameters, host, _validPath);

            Assert.Equal(expectedhost, result.Host);
        }
Esempio n. 3
0
        public void UrlObjectConverter_ProducesExpectedRawUrl()
        {
            UrlObjectConverter converter = new UrlObjectConverter(new DefaultValueFactory());
            PostmanUrl         result    = converter.Convert(_validPath, _validParameters, _validHost, _validBasePath);

            Assert.Equal("http://mysite.com/api/Values/postFormDataEndpoint/:id?filter={{filter}}&page={{page}}", result.Raw);
        }
Esempio n. 4
0
        public void UrlObjectConverter_ProducesExpectedPathSegments(string basepath, string path, string[] expectedSegments)
        {
            UrlObjectConverter converter = new UrlObjectConverter(new DefaultValueFactory());
            PostmanUrl         result    = converter.Convert(path, new List <IParameter>(), _validHost, basepath);

            Assert.Equal(expectedSegments, result.Path);
        }
Esempio n. 5
0
        public void UrlObjectConverter_ProducesExpectedPath()
        {
            UrlObjectConverter converter = new UrlObjectConverter(new DefaultValueFactory());
            PostmanUrl         result    = converter.Convert(_validPath, _validParameters, _validHost, _validBasePath);

            Assert.Equal(new string[] { "api", "Values", "postFormDataEndpoint", ":id" }, result.Path);
        }
Esempio n. 6
0
        public void UrlObjectConverter_ProducesTheSameNumberOfVariablesAsUrlParameters()
        {
            int expectedTotal            = _validParameters.Count(p => p.In == SwashbuckleParameterTypeConstants.Query || p.In == SwashbuckleParameterTypeConstants.Path);
            UrlObjectConverter converter = new UrlObjectConverter(new DefaultValueFactory());
            PostmanUrl         result    = converter.Convert(_validPath, _validParameters, _validHost, _validBasePath);

            Assert.Equal(expectedTotal, result.Variables.Count);
        }
Esempio n. 7
0
        public OperationObjectConverterTests()
        {
            _validDoc = new SwaggerDocument()
            {
                BasePath = "http://mysite.com"
            };
            _validOperation = new Operation()
            {
                Description = "sample_description",
                OperationId = Guid.NewGuid().ToString(),
                Parameters  = _validParameters
            };

            _bodyConverterMock   = new Mock <IRequestBodyObjectConverter>();
            _headerConverterMock = new Mock <IHeaderParameterObjectConverter>();
            _validParameters     = new List <IParameter>()
            {
                new NonBodyParameter()
                {
                    In = SwashbuckleParameterTypeConstants.Path, Name = "id", Format = "int32", Type = "number"
                },
                new NonBodyParameter()
                {
                    In = SwashbuckleParameterTypeConstants.Query, Name = "filter", Format = "string", Type = "string"
                },
                new NonBodyParameter()
                {
                    In = SwashbuckleParameterTypeConstants.Query, Name = "page", Format = "int32", Type = "number"
                },
                new NonBodyParameter()
                {
                    In = SwashbuckleParameterTypeConstants.Header, Name = "x-custom-header", Format = "string", Type = "string"
                },
            };

            SwaggerDocument docResult = new SwaggerDocument();

            _expectedUrlResult = new PostmanUrl()
            {
                Hash        = "",
                Raw         = "http://mysite.com/api/action/:id?filter={{filter}}",
                Host        = "mysite.com",
                Path        = new string[] { "api", "action", ":id" },
                Port        = "80",
                Protocol    = "http",
                QueryParams = new List <PostmanQueryParam>
                {
                    new PostmanQueryParam("filter", "test")
                },
                Variables = new List <PostmanVariable>()
            };

            _urlCoverterMock = new Mock <IUrlObjectConverter>();
            _urlCoverterMock.Setup(x => x.Convert(It.IsAny <string>(), It.IsAny <List <IParameter> >(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns(_expectedUrlResult);
        }
Esempio n. 8
0
        public void UrlObjectConverter_ProducesCorrectQueryParameters()
        {
            int expectedTotal = _validParameters.Count(p => p.In == SwashbuckleParameterTypeConstants.Query);

            UrlObjectConverter converter = new UrlObjectConverter(new DefaultValueFactory());
            PostmanUrl         result    = converter.Convert(_validPath, _validParameters, _validHost, _validBasePath);

            foreach (PostmanQueryParam resultParam in result.QueryParams)
            {
                Assert.NotNull(resultParam.Key);
                Assert.NotNull(resultParam.Value);
                Assert.NotNull(resultParam.Description);
                var sourceParam = _validParameters.First(x => x.Name == resultParam.Key);
                Assert.Equal(sourceParam.Name, resultParam.Key);
                Assert.Equal(resultParam.Description.Content, sourceParam.Description);
            }
        }
Esempio n. 9
0
        public PostmanCollectionItem Convert(string path, PostmanHttpMethod method, Operation operation, SwaggerDocument swaggerDoc)
        {
            IList <IParameter> parameters = operation.Parameters;

            if (parameters == null)
            {
                parameters = new List <IParameter>();
            }

            PostmanUrl           urlObject  = this.urlConverter.Convert(path, parameters.ToList(), swaggerDoc.Host, swaggerDoc.BasePath);
            List <PostmanHeader> headerList = this.headerConverter.Convert(parameters.OfType <NonBodyParameter>().ToList());

            BodyParameter      bodyParam = parameters.OfType <BodyParameter>().FirstOrDefault(p => p is BodyParameter);
            PostmanRequestBody body      = requestBodyConverter.Convert(bodyParam, parameters.ToList(), swaggerDoc.Definitions);

            var collectionItem = new PostmanCollectionItem()
            {
                Description = new PostmanDescription {
                    Content = operation.Description
                },
                Id      = operation.OperationId,
                Name    = path,
                Request = new PostmanRequest
                {
                    Method      = method,
                    Description = new PostmanDescription {
                        Content = operation.Description ?? ""
                    },
                    Url     = urlObject,
                    Headers = headerList,
                    Body    = body
                }
            };

            return(collectionItem);
        }
        public PostmanUrl Convert(string path, List <IParameter> swaggerOperationParameters, string host, string basePath)
        {
            // replace aspnet route segments (/put/{id}) with postman style ones (/put/:id) so they can be populated with postman variables
            Regex ItemRegex = new Regex("{.*?}", RegexOptions.Compiled);

            foreach (Match match in ItemRegex.Matches(path))
            {
                string replacement = match.Value.Replace('{', ':').TrimEnd('}');
                path = path.Replace(match.Value, replacement);
            }

            var urlObject = new PostmanUrl
            {
                Raw         = "",
                QueryParams = new List <PostmanQueryParam>()
            };

            string rawUrl = "";

            if (!string.IsNullOrEmpty(host))
            {
                Uri uri = null;
                if (Uri.IsWellFormedUriString(host, UriKind.Absolute))
                {
                    uri = new Uri(host, UriKind.Absolute);
                }

                if (uri == null || (!uri.Scheme.StartsWith("http")))
                {
                    string constructedBaseUrl = $"http://{host.Trim('/')}/";
                    if (Uri.IsWellFormedUriString(constructedBaseUrl, UriKind.Absolute))
                    {
                        uri = new Uri(constructedBaseUrl);
                    }
                }

                if (uri != null)
                {
                    if (!string.IsNullOrEmpty(basePath))
                    {
                        uri = new Uri(uri, basePath);
                    }

                    urlObject.Host     = uri.Host;
                    urlObject.Port     = uri.Port.ToString();
                    urlObject.Protocol = uri.Scheme;

                    rawUrl = uri.AbsoluteUri.TrimEnd('/');
                }

                string fullPath = $"{(basePath ?? "").TrimEnd('/')}/{path.TrimStart('/')}";

                urlObject.Path = fullPath.Trim('/').Split('/');
            }

            rawUrl = $"{(rawUrl ?? "").TrimEnd('/')}/{path.TrimStart('/')}".TrimEnd('/');

            var pathParams = swaggerOperationParameters.OfType <NonBodyParameter>().Where(p => p.In == SwashbuckleParameterTypeConstants.Path).ToList();

            var queryParams = swaggerOperationParameters.OfType <NonBodyParameter>().Where(p => p.In == SwashbuckleParameterTypeConstants.Query).ToList();

            if (queryParams.Count > 0)
            {
                rawUrl += "?" + string.Join("&", (queryParams.Select(x => x.Name + "={{" + x.Name + "}}")));
            }
            urlObject.Raw = rawUrl;

            var allUrlParams = swaggerOperationParameters
                               .OfType <NonBodyParameter>()
                               .Where(p => p.In == SwashbuckleParameterTypeConstants.Query || p.In == SwashbuckleParameterTypeConstants.Path)
                               .ToList();

            foreach (NonBodyParameter p in allUrlParams)
            {
                var defaultValue = valueCreator.GetDefaultValueFromFormat(p.Format, p.Type);
                if (p.In == SwashbuckleParameterTypeConstants.Query)
                {
                    urlObject.QueryParams.Add(new PostmanQueryParam
                    {
                        Key         = p.Name,
                        Value       = (defaultValue != null ? defaultValue.ToString() : ""),
                        Disabled    = false,
                        Description = new PostmanDescription {
                            Content = p.Description
                        }
                    });
                }
                urlObject.Variables.Add(new PostmanVariable
                {
                    Description = new PostmanDescription {
                        Content = p.Description
                    },
                    Disabled = false,
                    Id       = p.Name,
                    Key      = p.Name,
                    Name     = p.Name,
                    System   = false,
                    Type     = p.Type,
                    Value    = defaultValue != null ? defaultValue.ToString() : ""
                });
            }
            return(urlObject);
        }