Пример #1
0
        public MappingModel ToMappingModel(IMapping mapping)
        {
            var request  = (Request)mapping.RequestMatcher;
            var response = (Response)mapping.Provider;

            var clientIPMatchers = request.GetRequestMessageMatchers <RequestMessageClientIPMatcher>().Where(m => m.Matchers != null).SelectMany(m => m.Matchers).ToList();
            var pathMatchers     = request.GetRequestMessageMatchers <RequestMessagePathMatcher>().Where(m => m.Matchers != null).SelectMany(m => m.Matchers).ToList();
            var urlMatchers      = request.GetRequestMessageMatchers <RequestMessageUrlMatcher>().Where(m => m.Matchers != null).SelectMany(m => m.Matchers).ToList();
            var headerMatchers   = request.GetRequestMessageMatchers <RequestMessageHeaderMatcher>();
            var cookieMatchers   = request.GetRequestMessageMatchers <RequestMessageCookieMatcher>();
            var paramsMatchers   = request.GetRequestMessageMatchers <RequestMessageParamMatcher>();
            var methodMatcher    = request.GetRequestMessageMatcher <RequestMessageMethodMatcher>();
            var bodyMatcher      = request.GetRequestMessageMatcher <RequestMessageBodyMatcher>();

            var mappingModel = new MappingModel
            {
                Guid        = mapping.Guid,
                Title       = mapping.Title,
                Priority    = mapping.Priority != 0 ? mapping.Priority : (int?)null,
                Scenario    = mapping.Scenario,
                WhenStateIs = mapping.ExecutionConditionState,
                SetStateTo  = mapping.NextState,
                Request     = new RequestModel
                {
                    ClientIP = clientIPMatchers.Any() ? new ClientIPModel
                    {
                        Matchers = _mapper.Map(clientIPMatchers)
                    } : null,

                    Path = pathMatchers.Any() ? new PathModel
                    {
                        Matchers = _mapper.Map(pathMatchers)
                    } : null,

                    Url = urlMatchers.Any() ? new UrlModel
                    {
                        Matchers = _mapper.Map(urlMatchers)
                    } : null,

                    Methods = methodMatcher?.Methods,

                    Headers = headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderModel
                    {
                        Name     = hm.Name,
                        Matchers = _mapper.Map(hm.Matchers)
                    }).ToList() : null,

                    Cookies = cookieMatchers.Any() ? cookieMatchers.Select(cm => new CookieModel
                    {
                        Name     = cm.Name,
                        Matchers = _mapper.Map(cm.Matchers)
                    }).ToList() : null,

                    Params = paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel
                    {
                        Name       = pm.Key,
                        IgnoreCase = pm.IgnoreCase == true ? true : (bool?)null,
                        Matchers   = _mapper.Map(pm.Matchers)
                    }).ToList() : null
                },
                Response = new ResponseModel
                {
                    Delay = (int?)response.Delay?.TotalMilliseconds
                },
                Webhook = WebhookMapper.Map(mapping.Webhook)
            };

            if (bodyMatcher?.Matchers != null)
            {
                mappingModel.Request.Body = new BodyModel();

                if (bodyMatcher.Matchers.Length == 1)
                {
                    mappingModel.Request.Body.Matcher = _mapper.Map(bodyMatcher.Matchers[0]);
                }
                else if (bodyMatcher.Matchers.Length > 1)
                {
                    mappingModel.Request.Body.Matchers = _mapper.Map(bodyMatcher.Matchers);
                }
            }

            if (response.ProxyAndRecordSettings != null)
            {
                mappingModel.Response.StatusCode         = null;
                mappingModel.Response.Headers            = null;
                mappingModel.Response.BodyDestination    = null;
                mappingModel.Response.BodyAsJson         = null;
                mappingModel.Response.BodyAsJsonIndented = null;
                mappingModel.Response.Body                        = null;
                mappingModel.Response.BodyAsBytes                 = null;
                mappingModel.Response.BodyAsFile                  = null;
                mappingModel.Response.BodyAsFileIsCached          = null;
                mappingModel.Response.UseTransformer              = null;
                mappingModel.Response.TransformerType             = null;
                mappingModel.Response.UseTransformerForBodyAsFile = null;
                mappingModel.Response.BodyEncoding                = null;
                mappingModel.Response.ProxyUrl                    = response.ProxyAndRecordSettings.Url;
                mappingModel.Response.Fault                       = null;
                mappingModel.Response.WebProxy                    = MapWebProxy(response.ProxyAndRecordSettings.WebProxySettings);
            }
            else
            {
                mappingModel.Response.WebProxy        = null;
                mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination;
                mappingModel.Response.StatusCode      = response.ResponseMessage.StatusCode;

                if (response.ResponseMessage.Headers != null && response.ResponseMessage.Headers.Count > 0)
                {
                    mappingModel.Response.Headers = MapHeaders(response.ResponseMessage.Headers);
                }

                if (response.UseTransformer)
                {
                    mappingModel.Response.UseTransformer  = response.UseTransformer;
                    mappingModel.Response.TransformerType = response.TransformerType.ToString();
                }

                if (response.UseTransformerForBodyAsFile)
                {
                    mappingModel.Response.UseTransformerForBodyAsFile = response.UseTransformerForBodyAsFile;
                }

                if (response.ResponseMessage.BodyData != null)
                {
                    switch (response.ResponseMessage.BodyData?.DetectedBodyType)
                    {
                    case BodyType.String:
                        mappingModel.Response.Body = response.ResponseMessage.BodyData.BodyAsString;
                        break;

                    case BodyType.Json:
                        mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyData.BodyAsJson;
                        if (response.ResponseMessage.BodyData.BodyAsJsonIndented == true)
                        {
                            mappingModel.Response.BodyAsJsonIndented = response.ResponseMessage.BodyData.BodyAsJsonIndented;
                        }
                        break;

                    case BodyType.Bytes:
                        mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyData.BodyAsBytes;
                        break;

                    case BodyType.File:
                        mappingModel.Response.BodyAsFile         = response.ResponseMessage.BodyData.BodyAsFile;
                        mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyData.BodyAsFileIsCached;
                        break;
                    }

                    if (response.ResponseMessage.BodyData.Encoding != null && response.ResponseMessage.BodyData.Encoding.WebName != "utf-8")
                    {
                        mappingModel.Response.BodyEncoding = new EncodingModel
                        {
                            EncodingName = response.ResponseMessage.BodyData.Encoding.EncodingName,
                            CodePage     = response.ResponseMessage.BodyData.Encoding.CodePage,
                            WebName      = response.ResponseMessage.BodyData.Encoding.WebName
                        };
                    }
                }

                if (response.ResponseMessage.FaultType != FaultType.NONE)
                {
                    mappingModel.Response.Fault = new FaultModel
                    {
                        Type       = response.ResponseMessage.FaultType.ToString(),
                        Percentage = response.ResponseMessage.FaultPercentage
                    };
                }
            }

            return(mappingModel);
        }
Пример #2
0
        public static MappingModel ToMappingModel(Mapping mapping)
        {
            var request  = (Request)mapping.RequestMatcher;
            var response = (Response)mapping.Provider;

            var clientIPMatchers = request.GetRequestMessageMatchers <RequestMessageClientIPMatcher>();
            var pathMatchers     = request.GetRequestMessageMatchers <RequestMessagePathMatcher>();
            var urlMatchers      = request.GetRequestMessageMatchers <RequestMessageUrlMatcher>();
            var headerMatchers   = request.GetRequestMessageMatchers <RequestMessageHeaderMatcher>();
            var cookieMatchers   = request.GetRequestMessageMatchers <RequestMessageCookieMatcher>();
            var paramsMatchers   = request.GetRequestMessageMatchers <RequestMessageParamMatcher>();
            var bodyMatcher      = request.GetRequestMessageMatcher <RequestMessageBodyMatcher>();
            var methodMatcher    = request.GetRequestMessageMatcher <RequestMessageMethodMatcher>();

            var mappingModel = new MappingModel
            {
                Guid        = mapping.Guid,
                Title       = mapping.Title,
                Priority    = mapping.Priority,
                Scenario    = mapping.Scenario,
                WhenStateIs = mapping.ExecutionConditionState,
                SetStateTo  = mapping.NextState,
                Request     = new RequestModel
                {
                    ClientIP = clientIPMatchers != null && clientIPMatchers.Any() ? new ClientIPModel
                    {
                        Matchers = MatcherMapper.Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
                                   //Funcs = Map(clientIPMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
                    } : null,

                    Path = pathMatchers != null && pathMatchers.Any() ? new PathModel
                    {
                        Matchers = MatcherMapper.Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
                                   //Funcs = Map(pathMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
                    } : null,

                    Url = urlMatchers != null && urlMatchers.Any() ? new UrlModel
                    {
                        Matchers = MatcherMapper.Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
                                   //Funcs = Map(urlMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
                    } : null,

                    Methods = methodMatcher?.Methods,

                    Headers = headerMatchers != null && headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderModel
                    {
                        Name     = hm.Name,
                        Matchers = MatcherMapper.Map(hm.Matchers)
                                   //Funcs = Map(hm.Funcs)
                    }).ToList() : null,

                    Cookies = cookieMatchers != null && cookieMatchers.Any() ? cookieMatchers.Select(cm => new CookieModel
                    {
                        Name     = cm.Name,
                        Matchers = MatcherMapper.Map(cm.Matchers)
                                   //Funcs = Map(cm.Funcs)
                    }).ToList() : null,

                    Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel
                    {
                        Name   = pm.Key,
                        Values = pm.Values?.ToList()
                                 //Funcs = Map(pm.Funcs)
                    }).ToList() : null,

                    Body = methodMatcher?.Methods != null && methodMatcher.Methods.Any(m => m == "get") ? null : new BodyModel
                    {
                        Matcher = bodyMatcher != null?MatcherMapper.Map(bodyMatcher.Matcher) : null
                                      //Func = bodyMatcher != null ? Map(bodyMatcher.Func) : null,
                                      //DataFunc = bodyMatcher != null ? Map(bodyMatcher.DataFunc) : null
                    }
                },
                Response = new ResponseModel
                {
                    Delay = response.Delay?.Milliseconds
                }
            };

            if (!string.IsNullOrEmpty(response.ProxyUrl))
            {
                mappingModel.Response.StatusCode         = null;
                mappingModel.Response.Headers            = null;
                mappingModel.Response.BodyDestination    = null;
                mappingModel.Response.BodyAsJson         = null;
                mappingModel.Response.BodyAsJsonIndented = null;
                mappingModel.Response.Body               = null;
                mappingModel.Response.BodyAsBytes        = null;
                mappingModel.Response.BodyAsFile         = null;
                mappingModel.Response.BodyAsFileIsCached = null;
                mappingModel.Response.UseTransformer     = false;
                mappingModel.Response.BodyEncoding       = null;
                mappingModel.Response.ProxyUrl           = response.ProxyUrl;
            }
            else
            {
                mappingModel.Response.BodyDestination    = response.ResponseMessage.BodyDestination;
                mappingModel.Response.StatusCode         = response.ResponseMessage.StatusCode;
                mappingModel.Response.Headers            = Map(response.ResponseMessage.Headers);
                mappingModel.Response.BodyAsJson         = response.ResponseMessage.BodyAsJson;
                mappingModel.Response.BodyAsJsonIndented = response.ResponseMessage.BodyAsJsonIndented;
                mappingModel.Response.Body               = response.ResponseMessage.Body;
                mappingModel.Response.BodyAsBytes        = response.ResponseMessage.BodyAsBytes;
                mappingModel.Response.BodyAsFile         = response.ResponseMessage.BodyAsFile;
                mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyAsFileIsCached;
                mappingModel.Response.UseTransformer     = response.UseTransformer;

                if (response.ResponseMessage.BodyEncoding != null)
                {
                    mappingModel.Response.BodyEncoding = new EncodingModel
                    {
                        EncodingName = response.ResponseMessage.BodyEncoding.EncodingName,
                        CodePage     = response.ResponseMessage.BodyEncoding.CodePage,
                        WebName      = response.ResponseMessage.BodyEncoding.WebName
                    };
                }
            }

            return(mappingModel);
        }
Пример #3
0
        public static MappingModel ToMappingModel(IMapping mapping)
        {
            var request  = (Request)mapping.RequestMatcher;
            var response = (Response)mapping.Provider;

            var clientIPMatchers = request.GetRequestMessageMatchers <RequestMessageClientIPMatcher>();
            var pathMatchers     = request.GetRequestMessageMatchers <RequestMessagePathMatcher>();
            var urlMatchers      = request.GetRequestMessageMatchers <RequestMessageUrlMatcher>();
            var headerMatchers   = request.GetRequestMessageMatchers <RequestMessageHeaderMatcher>();
            var cookieMatchers   = request.GetRequestMessageMatchers <RequestMessageCookieMatcher>();
            var paramsMatchers   = request.GetRequestMessageMatchers <RequestMessageParamMatcher>();
            var bodyMatcher      = request.GetRequestMessageMatcher <RequestMessageBodyMatcher>();
            var methodMatcher    = request.GetRequestMessageMatcher <RequestMessageMethodMatcher>();

            var mappingModel = new MappingModel
            {
                Guid        = mapping.Guid,
                Title       = mapping.Title,
                Priority    = mapping.Priority != 0 ? mapping.Priority : (int?)null,
                Scenario    = mapping.Scenario,
                WhenStateIs = mapping.ExecutionConditionState,
                SetStateTo  = mapping.NextState,
                Request     = new RequestModel
                {
                    ClientIP = clientIPMatchers != null && clientIPMatchers.Any() ? new ClientIPModel
                    {
                        Matchers = MatcherMapper.Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
                    } : null,

                    Path = pathMatchers != null && pathMatchers.Any() ? new PathModel
                    {
                        Matchers = MatcherMapper.Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
                    } : null,

                    Url = urlMatchers != null && urlMatchers.Any() ? new UrlModel
                    {
                        Matchers = MatcherMapper.Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
                    } : null,

                    Methods = methodMatcher?.Methods,

                    Headers = headerMatchers != null && headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderModel
                    {
                        Name     = hm.Name,
                        Matchers = MatcherMapper.Map(hm.Matchers)
                    }).ToList() : null,

                    Cookies = cookieMatchers != null && cookieMatchers.Any() ? cookieMatchers.Select(cm => new CookieModel
                    {
                        Name     = cm.Name,
                        Matchers = MatcherMapper.Map(cm.Matchers)
                    }).ToList() : null,

                    Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel
                    {
                        Name       = pm.Key,
                        IgnoreCase = pm.IgnoreCase == true ? true : (bool?)null,
                        Matchers   = MatcherMapper.Map(pm.Matchers)
                    }).ToList() : null,

                    Body = methodMatcher?.Methods != null && methodMatcher.Methods.Any(m => m == "get") ? null : new BodyModel
                    {
                        Matcher = bodyMatcher != null?MatcherMapper.Map(bodyMatcher.Matcher) : null
                    }
                },
                Response = new ResponseModel
                {
                    Delay = (int?)response.Delay?.TotalMilliseconds
                }
            };

            if (!string.IsNullOrEmpty(response.ProxyUrl))
            {
                mappingModel.Response.StatusCode         = null;
                mappingModel.Response.Headers            = null;
                mappingModel.Response.BodyDestination    = null;
                mappingModel.Response.BodyAsJson         = null;
                mappingModel.Response.BodyAsJsonIndented = null;
                mappingModel.Response.Body               = null;
                mappingModel.Response.BodyAsBytes        = null;
                mappingModel.Response.BodyAsFile         = null;
                mappingModel.Response.BodyAsFileIsCached = null;
                mappingModel.Response.UseTransformer     = null;
                mappingModel.Response.BodyEncoding       = null;
                mappingModel.Response.ProxyUrl           = response.ProxyUrl;
            }
            else
            {
                mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination;
                mappingModel.Response.StatusCode      = response.ResponseMessage.StatusCode;
                mappingModel.Response.Headers         = Map(response.ResponseMessage.Headers);
                if (response.UseTransformer)
                {
                    mappingModel.Response.UseTransformer = response.UseTransformer;
                }

                if (response.ResponseMessage.BodyData != null)
                {
                    switch (response.ResponseMessage.BodyData?.DetectedBodyType)
                    {
                    case BodyType.String:
                        mappingModel.Response.Body = response.ResponseMessage.BodyData.BodyAsString;
                        break;

                    case BodyType.Json:
                        mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyData.BodyAsJson;
                        if (response.ResponseMessage.BodyData.BodyAsJsonIndented == true)
                        {
                            mappingModel.Response.BodyAsJsonIndented = response.ResponseMessage.BodyData.BodyAsJsonIndented;
                        }
                        break;

                    case BodyType.Bytes:
                        mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyData.BodyAsBytes;
                        break;

                    case BodyType.File:
                        mappingModel.Response.BodyAsFile         = response.ResponseMessage.BodyData.BodyAsFile;
                        mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyData.BodyAsFileIsCached;
                        break;
                    }

                    if (response.ResponseMessage.BodyData.Encoding != null && response.ResponseMessage.BodyData.Encoding.WebName != "utf-8")
                    {
                        mappingModel.Response.BodyEncoding = new EncodingModel
                        {
                            EncodingName = response.ResponseMessage.BodyData.Encoding.EncodingName,
                            CodePage     = response.ResponseMessage.BodyData.Encoding.CodePage,
                            WebName      = response.ResponseMessage.BodyData.Encoding.WebName
                        };
                    }
                }
            }

            return(mappingModel);
        }