Пример #1
0
        /// <summary>
        ///     Detrmines the body match based on the rules in the contract.
        /// </summary>
        /// <param name="contract">The contract.</param>
        /// <returns>A member of the <see cref="T:Stumps.Web.Models.BodyMatch"/> enumeration.</returns>
        private BodyMatch DetrmineBodyMatch(StumpContract contract)
        {
            var rules = contract.Rules.FindRuleContractByName(typeof(BodyLengthRule).Name);

            if (rules.Count > 0)
            {
                var blr = ContractBindings.CreateRuleFromContract <BodyLengthRule>(rules[0]);

                if (blr.MinimumBodyLength == 0 && blr.MaximumBodyLength == 0)
                {
                    return(BodyMatch.IsBlank);
                }

                if (blr.MinimumBodyLength == 1 && blr.MaximumBodyLength == int.MaxValue)
                {
                    return(BodyMatch.IsNotBlank);
                }
            }

            if (contract.Rules.FindRuleContractByName(typeof(BodyContentRule).Name).Count > 0)
            {
                return(BodyMatch.ContainsText);
            }

            if (contract.Rules.FindRuleContractByName(typeof(BodyMatchRule).Name).Count > 0)
            {
                return(BodyMatch.ExactMatch);
            }

            return(BodyMatch.IsAnything);
        }
Пример #2
0
        /// <summary>
        ///     Creates a <see cref="T:Stumps.Web.Models.StumpModel"/> from an existing Stump.
        /// </summary>
        /// <param name="stump">The <see cref="T:Stumps.Server.StumpContract"/> used to create the model.</param>
        /// <param name="serverId">The unique identifier for the proxy the Stump belongs to.</param>
        /// <param name="stumpId">The unique identifier of the Stump.</param>
        /// <returns>
        ///     A new <see cref="T:Stumps.Web.Models.StumpModel"/> object.
        /// </returns>
        private StumpModel CreateStumpModel(StumpContract stump, string serverId, string stumpId)
        {
            var bodyMatch = DetrmineBodyMatch(stump);

            var model = new StumpModel
            {
                Name        = stump.StumpName,
                Origin      = StumpOrigin.ExistingStump,
                RecordId    = -1,
                RequestBody =
                    stump.OriginalRequest.BodyType == HttpBodyClassification.Text ? stump.OriginalRequest.GetBodyAsString() : string.Empty,
                RequestBodyIsImage     = stump.OriginalRequest.BodyType == HttpBodyClassification.Image,
                RequestBodyIsText      = stump.OriginalRequest.BodyType == HttpBodyClassification.Text,
                RequestBodyLength      = stump.OriginalRequest.BodyLength,
                RequestBodyMatch       = bodyMatch,
                RequestBodyMatchValues =
                    stump.Rules.FindRuleContractByName(typeof(BodyContentRule).Name).Count > 0 ?
                    ContractBindings.CreateRuleFromContract <BodyContentRule>(stump.Rules.FindRuleContractByName(typeof(BodyContentRule).Name)[0]).GetTextEvaluators() :
                    new string[0],
                RequestBodyUrl         = "/api/proxy/" + serverId + "/stumps/" + stumpId + "/request",
                RequestHeaderMatch     = CreateHeadersFromRules(stump),
                RequestHttpMethod      = stump.OriginalRequest.HttpMethod,
                RequestHttpMethodMatch = stump.Rules.FindRuleContractByName(typeof(HttpMethodRule).Name).Count > 0,
                RequestUrl             = stump.OriginalRequest.RawUrl,
                RequestUrlMatch        = stump.Rules.FindRuleContractByName(typeof(UrlRule).Name).Count > 0,
                ResponseBody           =
                    stump.Response.BodyType == HttpBodyClassification.Text
                        ? stump.Response.GetBodyAsString()
                        : string.Empty,
                ResponseBodyIsImage       = stump.Response.BodyType == HttpBodyClassification.Image,
                ResponseBodyIsText        = stump.Response.BodyType == HttpBodyClassification.Text,
                ResponseBodyLength        = stump.Response.BodyLength,
                ResponseBodyModification  = string.Empty,
                ResponseBodySource        = BodySource.Origin,
                ResponseBodyUrl           = "/api/proxy/" + serverId + "/stumps/" + stumpId + "/response",
                ResponseHeaders           = CreateHeaderModel(stump.Response.Headers),
                ResponseStatusCode        = stump.Response.StatusCode,
                ResponseStatusDescription = stump.Response.StatusDescription,
                StumpId = stump.StumpId
            };

            return(model);
        }
Пример #3
0
        private HeaderModel[] CreateHeadersFromRules(StumpContract contract)
        {
            var models = new List <HeaderModel>();

            var rules = contract.Rules.FindRuleContractByName(typeof(HeaderRule).Name);

            foreach (var rule in rules)
            {
                var headerRule = ContractBindings.CreateRuleFromContract <HeaderRule>(rule);

                var model = new HeaderModel
                {
                    Name  = headerRule.HeaderNameTextMatch,
                    Value = headerRule.HeaderValueTextMatch
                };

                models.Add(model);
            }

            return(models.ToArray());
        }