Exemplo n.º 1
0
        public string GetName(PostmanFeature feature, Postman request, Type requestType, string virtualPath)
        {
            var fragments = request.Label ?? feature.DefaultLabelFmt;
            var sb        = new StringBuilder();

            foreach (var fragment in fragments)
            {
                var parts     = fragment.ToLower().Split(':');
                var asEnglish = parts.Length > 1 && parts[1] == "english";

                if (parts[0] == "type")
                {
                    sb.Append(asEnglish ? requestType.Name.ToEnglish() : requestType.Name);
                }
                else if (parts[0] == "route")
                {
                    sb.Append(virtualPath);
                }
                else
                {
                    sb.Append(parts[0]);
                }
            }
            return(sb.ToString());
        }
Exemplo n.º 2
0
        public object Any(Postman request)
        {
            var feature = HostContext.GetPlugin <PostmanFeature>();

            if (request.ExportSession)
            {
                if (feature.EnableSessionExport != true)
                {
                    throw new ArgumentException("PostmanFeature.EnableSessionExport is not enabled");
                }

                var url = Request.ResolveBaseUrl()
                          .CombineWith(Request.PathInfo)
                          .AddQueryParam("ssopt", Request.GetItemOrCookie(SessionFeature.SessionOptionsKey))
                          .AddQueryParam("sspid", Request.GetPermanentSessionId())
                          .AddQueryParam("ssid", Request.GetTemporarySessionId());

                return(HttpResult.Redirect(url));
            }

            var id  = SessionExtensions.CreateRandomSessionId();
            var ret = new PostmanCollection
            {
                id        = id,
                name      = HostContext.AppHost.ServiceName,
                timestamp = DateTime.UtcNow.ToUnixTimeMs(),
                requests  = GetRequests(request, id, HostContext.Metadata.OperationsMap.Values),
            };

            return(ret);
        }
Exemplo n.º 3
0
        public List <PostmanRequest> GetRequests(Postman request, string parentId, IEnumerable <Operation> operations)
        {
            var ret     = new List <PostmanRequest>();
            var feature = HostContext.GetPlugin <PostmanFeature>();

            var headers = feature.Headers ?? ("Accept: " + MimeTypes.Json);

            var httpRes = Response as IHttpResponse;

            if (httpRes != null)
            {
                if (request.ssopt != null ||
                    request.sspid != null ||
                    request.ssid != null)
                {
                    if (feature.EnableSessionExport != true)
                    {
                        throw new ArgumentException("PostmanFeature.EnableSessionExport is not enabled");
                    }
                }

                if (request.ssopt != null)
                {
                    Request.AddSessionOptions(request.ssopt);
                }
                if (request.sspid != null)
                {
                    httpRes.Cookies.AddPermanentCookie(SessionFeature.PermanentSessionId, request.sspid);
                }
                if (request.ssid != null)
                {
                    httpRes.Cookies.AddSessionCookie(SessionFeature.SessionId, request.ssid,
                                                     (HostContext.Config.OnlySendSessionCookiesSecurely && Request.IsSecureConnection));
                }
            }

            foreach (var op in operations)
            {
                if (!HostContext.Metadata.IsVisible(base.Request, op))
                {
                    continue;
                }

                var allVerbs = op.Actions.Concat(
                    op.Routes.SelectMany(x => x.Verbs))
                               .SelectMany(x => x == ActionContext.AnyAction
                        ? feature.DefaultVerbsForAny
                        : new List <string> {
                    x
                })
                               .ToHashSet();

                var propertyTypes = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                op.RequestType.GetSerializableFields()
                .Each(x => propertyTypes[x.Name] = x.FieldType.AsFriendlyName(feature));
                op.RequestType.GetSerializableProperties()
                .Each(x => propertyTypes[x.Name] = x.PropertyType.AsFriendlyName(feature));

                foreach (var route in op.Routes)
                {
                    var routeVerbs = route.Verbs.Contains(ActionContext.AnyAction)
                        ? feature.DefaultVerbsForAny.ToArray()
                        : route.Verbs;

                    var restRoute = route.ToRestRoute();

                    foreach (var verb in routeVerbs)
                    {
                        allVerbs.Remove(verb); //exclude handled verbs

                        var routeData = restRoute.QueryStringVariables
                                        .Map(x => new PostmanData
                        {
                            key   = x,
                            value = "",
                            type  = "text",
                        })
                                        .ApplyPropertyTypes(propertyTypes);

                        ret.Add(new PostmanRequest
                        {
                            collectionId  = parentId,
                            id            = SessionExtensions.CreateRandomSessionId(),
                            method        = verb,
                            url           = Request.GetBaseUrl().CombineWith(restRoute.Path.ToPostmanPathVariables()),
                            name          = GetName(feature, request, op.RequestType, restRoute.Path),
                            description   = op.RequestType.GetDescription(),
                            pathVariables = !verb.HasRequestBody()
                                ? restRoute.Variables.Concat(routeData.Select(x => x.key))
                                            .ApplyPropertyTypes(propertyTypes)
                                : null,
                            data = verb.HasRequestBody()
                                ? routeData
                                : null,
                            dataMode = "params",
                            headers  = headers,
                            version  = 2,
                            time     = DateTime.UtcNow.ToUnixTimeMs(),
                        });
                    }
                }

                var emptyRequest = op.RequestType.CreateInstance();
                var virtualPath  = emptyRequest.ToReplyUrlOnly();

                var requestParams = propertyTypes
                                    .Map(x => new PostmanData
                {
                    key   = x.Key,
                    value = x.Value,
                    type  = "text",
                });

                ret.AddRange(allVerbs.Select(verb =>
                                             new PostmanRequest
                {
                    collectionId  = parentId,
                    id            = SessionExtensions.CreateRandomSessionId(),
                    method        = verb,
                    url           = Request.GetBaseUrl().CombineWith(virtualPath),
                    pathVariables = !verb.HasRequestBody()
                            ? requestParams.Select(x => x.key)
                                    .ApplyPropertyTypes(propertyTypes)
                            : null,
                    name        = GetName(feature, request, op.RequestType, virtualPath),
                    description = op.RequestType.GetDescription(),
                    data        = verb.HasRequestBody()
                            ? requestParams
                            : null,
                    dataMode = "params",
                    headers  = headers,
                    version  = 2,
                    time     = DateTime.UtcNow.ToUnixTimeMs(),
                }));
            }

            return(ret);
        }
Exemplo n.º 4
0
        public string GetName(PostmanFeature feature, Postman request, Type requestType, string virtualPath)
        {
            var fragments = request.Label ?? feature.DefaultLabelFmt;
            var sb = new StringBuilder();
            foreach (var fragment in fragments)
            {
                var parts = fragment.ToLower().Split(':');
                var asEnglish = parts.Length > 1 && parts[1] == "english";

                if (parts[0] == "type")
                {
                    sb.Append(asEnglish ? requestType.Name.ToEnglish() : requestType.Name);
                }
                else if (parts[0] == "route")
                {
                    sb.Append(virtualPath);
                }
                else
                {
                    sb.Append(parts[0]);
                }
            }
            return sb.ToString();
        }
Exemplo n.º 5
0
        public List<PostmanRequest> GetRequests(Postman request, string parentId, IEnumerable<Operation> operations)
        {
            var ret = new List<PostmanRequest>();
            var feature = HostContext.GetPlugin<PostmanFeature>();

            var headers = feature.Headers ?? ("Accept: " + MimeTypes.Json);

            var httpRes = Response as IHttpResponse;
            if (httpRes != null)
            {
                if (request.ssopt != null
                    || request.sspid != null
                    || request.ssid != null)
                {
                    if (feature.EnableSessionExport != true)
                    {
                        throw new ArgumentException("PostmanFeature.EnableSessionExport is not enabled");
                    }
                }

                if (request.ssopt != null)
                {
                    Request.AddSessionOptions(request.ssopt);
                }
                if (request.sspid != null)
                {
                    httpRes.Cookies.AddPermanentCookie(SessionFeature.PermanentSessionId, request.sspid);
                }
                if (request.ssid != null)
                {
                    httpRes.Cookies.AddSessionCookie(SessionFeature.SessionId, request.ssid,
                        (HostContext.Config.OnlySendSessionCookiesSecurely && Request.IsSecureConnection));
                }
            }

            foreach (var op in operations)
            {
                if (!HostContext.Metadata.IsVisible(base.Request, op))
                    continue;

                var allVerbs = op.Actions.Concat(
                    op.Routes.SelectMany(x => x.Verbs))
                        .SelectMany(x => x == ActionContext.AnyAction
                        ? feature.DefaultVerbsForAny
                        : new List<string> { x })
                    .ToHashSet();

                var propertyTypes = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                op.RequestType.GetSerializableFields()
                    .Each(x => propertyTypes[x.Name] = x.FieldType.AsFriendlyName(feature));
                op.RequestType.GetSerializableProperties()
                    .Each(x => propertyTypes[x.Name] = x.PropertyType.AsFriendlyName(feature));

                foreach (var route in op.Routes)
                {
                    var routeVerbs = route.Verbs.Contains(ActionContext.AnyAction)
                        ? feature.DefaultVerbsForAny.ToArray()
                        : route.Verbs;

                    var restRoute = route.ToRestRoute();

                    foreach (var verb in routeVerbs)
                    {
                        allVerbs.Remove(verb); //exclude handled verbs

                        var routeData = restRoute.QueryStringVariables
                            .Map(x => new PostmanData
                            {
                                key = x,
                                value = "",
                                type = "text",
                            })
                            .ApplyPropertyTypes(propertyTypes);

                        ret.Add(new PostmanRequest
                        {
                            collectionId = parentId,
                            id = SessionExtensions.CreateRandomSessionId(),
                            method = verb,
                            url = Request.GetBaseUrl().CombineWith(restRoute.Path.ToPostmanPathVariables()),
                            name = GetName(feature, request, op.RequestType, restRoute.Path),
                            description = op.RequestType.GetDescription(),
                            pathVariables = !verb.HasRequestBody()
                                ? restRoute.Variables.Concat(routeData.Select(x => x.key))
                                    .ApplyPropertyTypes(propertyTypes)
                                : null,
                            data = verb.HasRequestBody()
                                ? routeData
                                : null,
                            dataMode = "params",
                            headers = headers,
                            version = 2,
                            time = DateTime.UtcNow.ToUnixTimeMs(),
                        });
                    }
                }

                var emptyRequest = op.RequestType.CreateInstance();
                var virtualPath = emptyRequest.ToReplyUrlOnly();

                var requestParams = propertyTypes
                    .Map(x => new PostmanData
                    {
                        key = x.Key,
                        value = x.Value,
                        type = "text",
                    });

                ret.AddRange(allVerbs.Select(verb =>
                    new PostmanRequest
                    {
                        collectionId = parentId,
                        id = SessionExtensions.CreateRandomSessionId(),
                        method = verb,
                        url = Request.GetBaseUrl().CombineWith(virtualPath),
                        pathVariables = !verb.HasRequestBody()
                            ? requestParams.Select(x => x.key)
                                .ApplyPropertyTypes(propertyTypes)
                            : null,
                        name = GetName(feature, request, op.RequestType, virtualPath),
                        description = op.RequestType.GetDescription(),
                        data = verb.HasRequestBody()
                            ? requestParams
                            : null,
                        dataMode = "params",
                        headers = headers,
                        version = 2,
                        time = DateTime.UtcNow.ToUnixTimeMs(),
                    }));
            }

            return ret;
        }
Exemplo n.º 6
0
        public object Any(Postman request)
        {
            var feature = HostContext.GetPlugin<PostmanFeature>();

            if (request.ExportSession)
            {
                if (feature.EnableSessionExport != true)
                    throw new ArgumentException("PostmanFeature.EnableSessionExport is not enabled");

                var url = Request.ResolveBaseUrl()
                    .CombineWith(Request.PathInfo)
                    .AddQueryParam("ssopt", Request.GetItemOrCookie(SessionFeature.SessionOptionsKey))
                    .AddQueryParam("sspid", Request.GetPermanentSessionId())
                    .AddQueryParam("ssid", Request.GetTemporarySessionId());

                return HttpResult.Redirect(url);
            }

            var id = SessionExtensions.CreateRandomSessionId();
            var ret = new PostmanCollection
            {
                id = id,
                name = HostContext.AppHost.ServiceName,
                timestamp = DateTime.UtcNow.ToUnixTimeMs(),
                requests = GetRequests(request, id, HostContext.Metadata.OperationsMap.Values),
            };

            return ret;
        }