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