private static void TransformBodyAsJson(ITransformerContext handlebarsContext, object model, ResponseMessage original, ResponseMessage responseMessage) { JToken jToken; switch (original.BodyData.BodyAsJson) { case JObject bodyAsJObject: jToken = bodyAsJObject.DeepClone(); WalkNode(handlebarsContext, jToken, model); break; case Array bodyAsArray: jToken = JArray.FromObject(bodyAsArray); WalkNode(handlebarsContext, jToken, model); break; case string bodyAsString: jToken = ReplaceSingleNode(handlebarsContext, bodyAsString, model); break; default: jToken = JObject.FromObject(original.BodyData.BodyAsJson); WalkNode(handlebarsContext, jToken, model); break; } responseMessage.BodyData = new BodyData { Encoding = original.BodyData.Encoding, DetectedBodyType = original.BodyData.DetectedBodyType, DetectedBodyTypeFromContentType = original.BodyData.DetectedBodyTypeFromContentType, BodyAsJson = jToken }; }
private static void WalkNode(ITransformerContext handlebarsContext, JToken node, object model) { if (node.Type == JTokenType.Object) { // In case of Object, loop all children. Do a ToArray() to avoid `Collection was modified` exceptions. foreach (JProperty child in node.Children <JProperty>().ToArray()) { WalkNode(handlebarsContext, child.Value, model); } } else if (node.Type == JTokenType.Array) { // In case of Array, loop all items. Do a ToArray() to avoid `Collection was modified` exceptions. foreach (JToken child in node.Children().ToArray()) { WalkNode(handlebarsContext, child, model); } } else if (node.Type == JTokenType.String) { // In case of string, try to transform the value. string stringValue = node.Value <string>(); if (string.IsNullOrEmpty(stringValue)) { return; } string transformedString = handlebarsContext.ParseAndRender(stringValue, model); if (!string.Equals(stringValue, transformedString)) { ReplaceNodeValue(node, transformedString); } } }
private void TransformBodyAsFile(ITransformerContext handlebarsContext, object model, ResponseMessage original, ResponseMessage responseMessage, bool useTransformerForBodyAsFile) { string transformedBodyAsFilename = handlebarsContext.ParseAndRender(original.BodyData.BodyAsFile, model); if (!useTransformerForBodyAsFile) { responseMessage.BodyData = new BodyData { DetectedBodyType = original.BodyData.DetectedBodyType, DetectedBodyTypeFromContentType = original.BodyData.DetectedBodyTypeFromContentType, BodyAsFile = transformedBodyAsFilename }; } else { string text = handlebarsContext.FileSystemHandler.ReadResponseBodyAsString(transformedBodyAsFilename); responseMessage.BodyData = new BodyData { DetectedBodyType = BodyType.String, DetectedBodyTypeFromContentType = original.BodyData.DetectedBodyTypeFromContentType, BodyAsString = handlebarsContext.ParseAndRender(text, model), BodyAsFile = transformedBodyAsFilename }; } }
private static void TransformBodyAsString(ITransformerContext handlebarsContext, object model, ResponseMessage original, ResponseMessage responseMessage) { responseMessage.BodyData = new BodyData { Encoding = original.BodyData.Encoding, DetectedBodyType = original.BodyData.DetectedBodyType, DetectedBodyTypeFromContentType = original.BodyData.DetectedBodyTypeFromContentType, BodyAsString = handlebarsContext.ParseAndRender(original.BodyData.BodyAsString, model) }; }
private static IBodyData TransformBodyAsString(ITransformerContext handlebarsContext, object model, IBodyData original) { return(new BodyData { Encoding = original.Encoding, DetectedBodyType = original.DetectedBodyType, DetectedBodyTypeFromContentType = original.DetectedBodyTypeFromContentType, BodyAsString = handlebarsContext.ParseAndRender(original.BodyAsString, model) }); }
private static JToken ReplaceSingleNode(ITransformerContext handlebarsContext, string stringValue, object model) { string transformedString = handlebarsContext.ParseAndRender(stringValue, model) as string; if (!string.Equals(stringValue, transformedString)) { const string property = "_"; JObject dummy = JObject.Parse($"{{ \"{property}\": null }}"); JToken node = dummy[property]; ReplaceNodeValue(node, transformedString); return(dummy[property]); } return(stringValue); }
private static IBodyData TransformBodyData(ITransformerContext transformerContext, object model, IBodyData original, bool useTransformerForBodyAsFile) { switch (original?.DetectedBodyType) { case BodyType.Json: return(TransformBodyAsJson(transformerContext, model, original)); case BodyType.File: return(TransformBodyAsFile(transformerContext, model, original, useTransformerForBodyAsFile)); case BodyType.String: return(TransformBodyAsString(transformerContext, model, original)); default: return(null); } }
private static IDictionary <string, WireMockList <string> > TransformHeaders(ITransformerContext transformerContext, object model, IDictionary <string, WireMockList <string> > original) { if (original == null) { return(new Dictionary <string, WireMockList <string> >()); } var newHeaders = new Dictionary <string, WireMockList <string> >(); foreach (var header in original) { var headerKey = transformerContext.ParseAndRender(header.Key, model); var templateHeaderValues = header.Value.Select(text => transformerContext.ParseAndRender(text, model)).ToArray(); newHeaders.Add(headerKey, new WireMockList <string>(templateHeaderValues)); } return(newHeaders); }