| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using WireMock.Admin.Mappings; |
| | | 6 | | using WireMock.Matchers.Request; |
| | | 7 | | using WireMock.RequestBuilders; |
| | | 8 | | using WireMock.ResponseBuilders; |
| | | 9 | | using WireMock.Util; |
| | | 10 | | |
| | | 11 | | namespace WireMock.Serialization |
| | | 12 | | { |
| | | 13 | | internal static class MappingConverter |
| | | 14 | | { |
| | | 15 | | public static MappingModel ToMappingModel(Mapping mapping) |
| | 1 | 16 | | { |
| | 1 | 17 | | var request = (Request)mapping.RequestMatcher; |
| | 1 | 18 | | var response = (Response)mapping.Provider; |
| | | 19 | | |
| | 1 | 20 | | var clientIPMatchers = request.GetRequestMessageMatchers<RequestMessageClientIPMatcher>(); |
| | 1 | 21 | | var pathMatchers = request.GetRequestMessageMatchers<RequestMessagePathMatcher>(); |
| | 1 | 22 | | var urlMatchers = request.GetRequestMessageMatchers<RequestMessageUrlMatcher>(); |
| | 1 | 23 | | var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>(); |
| | 1 | 24 | | var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>(); |
| | 1 | 25 | | var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>(); |
| | 1 | 26 | | var bodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>(); |
| | 1 | 27 | | var methodMatcher = request.GetRequestMessageMatcher<RequestMessageMethodMatcher>(); |
| | | 28 | | |
| | 1 | 29 | | var mappingModel = new MappingModel |
| | 1 | 30 | | { |
| | 1 | 31 | | Guid = mapping.Guid, |
| | 1 | 32 | | Title = mapping.Title, |
| | 1 | 33 | | Priority = mapping.Priority, |
| | 1 | 34 | | Scenario = mapping.Scenario, |
| | 1 | 35 | | WhenStateIs = mapping.ExecutionConditionState, |
| | 1 | 36 | | SetStateTo = mapping.NextState, |
| | 1 | 37 | | Request = new RequestModel |
| | 1 | 38 | | { |
| | 1 | 39 | | ClientIP = clientIPMatchers != null && clientIPMatchers.Any() ? new ClientIPModel |
| | 1 | 40 | | { |
| | 1 | 41 | | Matchers = MatcherMapper.Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.M |
| | 1 | 42 | | Funcs = Map(clientIPMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs)) |
| | 1 | 43 | | } : null, |
| | 1 | 44 | | |
| | 1 | 45 | | Path = pathMatchers != null && pathMatchers.Any() ? new PathModel |
| | 1 | 46 | | { |
| | 1 | 47 | | Matchers = MatcherMapper.Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Match |
| | 1 | 48 | | Funcs = Map(pathMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs)) |
| | 1 | 49 | | } : null, |
| | 1 | 50 | | |
| | 1 | 51 | | Url = urlMatchers != null && urlMatchers.Any() ? new UrlModel |
| | 1 | 52 | | { |
| | 1 | 53 | | Matchers = MatcherMapper.Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matche |
| | 1 | 54 | | Funcs = Map(urlMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs)) |
| | 1 | 55 | | } : null, |
| | 1 | 56 | | |
| | 1 | 57 | | Methods = methodMatcher?.Methods, |
| | 1 | 58 | | |
| | 1 | 59 | | Headers = headerMatchers != null && headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderMod |
| | 1 | 60 | | { |
| | 1 | 61 | | Name = hm.Name, |
| | 1 | 62 | | Matchers = MatcherMapper.Map(hm.Matchers), |
| | 1 | 63 | | Funcs = Map(hm.Funcs) |
| | 1 | 64 | | }).ToList() : null, |
| | 1 | 65 | | |
| | 1 | 66 | | Cookies = cookieMatchers != null && cookieMatchers.Any() ? cookieMatchers.Select(cm => new CookieMod |
| | 1 | 67 | | { |
| | 1 | 68 | | Name = cm.Name, |
| | 1 | 69 | | Matchers = MatcherMapper.Map(cm.Matchers), |
| | 1 | 70 | | Funcs = Map(cm.Funcs) |
| | 1 | 71 | | }).ToList() : null, |
| | 1 | 72 | | |
| | 1 | 73 | | Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel |
| | 1 | 74 | | { |
| | 1 | 75 | | Name = pm.Key, |
| | 1 | 76 | | Values = pm.Values?.ToList(), |
| | 1 | 77 | | Funcs = Map(pm.Funcs) |
| | 1 | 78 | | }).ToList() : null, |
| | 1 | 79 | | |
| | 1 | 80 | | Body = methodMatcher?.Methods != null && methodMatcher.Methods.Any(m => m == "get") ? null : new Bod |
| | 1 | 81 | | { |
| | 1 | 82 | | Matcher = bodyMatcher != null ? MatcherMapper.Map(bodyMatcher.Matcher) : null, |
| | 1 | 83 | | Func = bodyMatcher != null ? Map(bodyMatcher.Func) : null, |
| | 1 | 84 | | DataFunc = bodyMatcher != null ? Map(bodyMatcher.DataFunc) : null |
| | 1 | 85 | | } |
| | 1 | 86 | | }, |
| | 1 | 87 | | Response = new ResponseModel |
| | 1 | 88 | | { |
| | 1 | 89 | | Delay = response.Delay?.Milliseconds |
| | 1 | 90 | | } |
| | 1 | 91 | | }; |
| | | 92 | | |
| | 1 | 93 | | if (!string.IsNullOrEmpty(response.ProxyUrl)) |
| | 0 | 94 | | { |
| | 0 | 95 | | mappingModel.Response.StatusCode = null; |
| | 0 | 96 | | mappingModel.Response.Headers = null; |
| | 0 | 97 | | mappingModel.Response.BodyDestination = null; |
| | 0 | 98 | | mappingModel.Response.BodyAsJson = null; |
| | 0 | 99 | | mappingModel.Response.Body = null; |
| | 0 | 100 | | mappingModel.Response.BodyAsBytes = null; |
| | 0 | 101 | | mappingModel.Response.BodyAsFile = null; |
| | 0 | 102 | | mappingModel.Response.BodyAsFileIsCached = null; |
| | 0 | 103 | | mappingModel.Response.UseTransformer = false; |
| | 0 | 104 | | mappingModel.Response.BodyEncoding = null; |
| | 0 | 105 | | mappingModel.Response.ProxyUrl = response.ProxyUrl; |
| | 0 | 106 | | } |
| | | 107 | | else |
| | 1 | 108 | | { |
| | 1 | 109 | | mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination; |
| | 1 | 110 | | mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode; |
| | 1 | 111 | | mappingModel.Response.Headers = Map(response.ResponseMessage.Headers); |
| | 1 | 112 | | mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyAsJson; |
| | 1 | 113 | | mappingModel.Response.Body = response.ResponseMessage.Body; |
| | 1 | 114 | | mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyAsBytes; |
| | 1 | 115 | | mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyAsFile; |
| | 1 | 116 | | mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyAsFileIsCached; |
| | 1 | 117 | | mappingModel.Response.UseTransformer = response.UseTransformer; |
| | | 118 | | |
| | 1 | 119 | | if (response.ResponseMessage.BodyEncoding != null) |
| | 1 | 120 | | { |
| | 1 | 121 | | mappingModel.Response.BodyEncoding = new EncodingModel |
| | 1 | 122 | | { |
| | 1 | 123 | | EncodingName = response.ResponseMessage.BodyEncoding.EncodingName, |
| | 1 | 124 | | CodePage = response.ResponseMessage.BodyEncoding.CodePage, |
| | 1 | 125 | | WebName = response.ResponseMessage.BodyEncoding.WebName |
| | 1 | 126 | | }; |
| | 1 | 127 | | } |
| | 1 | 128 | | } |
| | | 129 | | |
| | 1 | 130 | | return mappingModel; |
| | 1 | 131 | | } |
| | | 132 | | |
| | | 133 | | private static IDictionary<string, object> Map(IDictionary<string, WireMockList<string>> dictionary) |
| | 1 | 134 | | { |
| | 1 | 135 | | if (dictionary == null) |
| | 0 | 136 | | { |
| | 0 | 137 | | return null; |
| | | 138 | | } |
| | | 139 | | |
| | 1 | 140 | | var newDictionary = new Dictionary<string, object>(); |
| | 3 | 141 | | foreach (var entry in dictionary) |
| | 0 | 142 | | { |
| | 0 | 143 | | object value = entry.Value.Count == 1 ? (object)entry.Value.ToString() : entry.Value; |
| | 0 | 144 | | newDictionary.Add(entry.Key, value); |
| | 0 | 145 | | } |
| | | 146 | | |
| | 1 | 147 | | return newDictionary; |
| | 1 | 148 | | } |
| | | 149 | | |
| | | 150 | | |
| | | 151 | | |
| | | 152 | | private static string[] Map<T>([CanBeNull] IEnumerable<Func<T, bool>> funcs) |
| | 0 | 153 | | { |
| | 0 | 154 | | return funcs?.Select(Map).Where(x => x != null).ToArray(); |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | private static string Map<T>([CanBeNull] Func<T, bool> func) |
| | 0 | 158 | | { |
| | 0 | 159 | | return func?.ToString(); |
| | 0 | 160 | | } |
| | | 161 | | } |
| | | 162 | | } |