| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using HandlebarsDotNet; |
| | | 5 | | using Newtonsoft.Json; |
| | | 6 | | using Newtonsoft.Json.Linq; |
| | | 7 | | using WireMock.Util; |
| | | 8 | | |
| | | 9 | | namespace WireMock.Transformers |
| | | 10 | | { |
| | | 11 | | internal static class ResponseMessageTransformer |
| | | 12 | | { |
| | | 13 | | static ResponseMessageTransformer() |
| | 1 | 14 | | { |
| | 1 | 15 | | HandlebarsHelpers.Register(); |
| | 1 | 16 | | } |
| | | 17 | | |
| | | 18 | | public static ResponseMessage Transform(RequestMessage requestMessage, ResponseMessage original) |
| | 32 | 19 | | { |
| | 32 | 20 | | bool bodyIsJson = original.BodyAsJson != null; |
| | 32 | 21 | | var responseMessage = new ResponseMessage { StatusCode = original.StatusCode }; |
| | | 22 | | |
| | 32 | 23 | | if (!bodyIsJson) |
| | 19 | 24 | | { |
| | 19 | 25 | | responseMessage.BodyOriginal = original.Body; |
| | 19 | 26 | | } |
| | | 27 | | |
| | 32 | 28 | | var template = new { request = requestMessage }; |
| | | 29 | | |
| | 32 | 30 | | if (!bodyIsJson) |
| | 19 | 31 | | { |
| | 19 | 32 | | TransformBodyAsString(template, original, responseMessage); |
| | 17 | 33 | | } |
| | | 34 | | else |
| | 13 | 35 | | { |
| | 13 | 36 | | TransformBodyAsJson(template, original, responseMessage); |
| | 10 | 37 | | } |
| | | 38 | | |
| | | 39 | | // Headers |
| | 27 | 40 | | var newHeaders = new Dictionary<string, WireMockList<string>>(); |
| | 105 | 41 | | foreach (var header in original.Headers) |
| | 12 | 42 | | { |
| | 12 | 43 | | var templateHeaderKey = Handlebars.Compile(header.Key); |
| | 12 | 44 | | var templateHeaderValues = header.Value |
| | 12 | 45 | | .Select(Handlebars.Compile) |
| | 25 | 46 | | .Select(func => func(template)) |
| | 12 | 47 | | .ToArray(); |
| | | 48 | | |
| | 12 | 49 | | newHeaders.Add(templateHeaderKey(template), new WireMockList<string>(templateHeaderValues)); |
| | 12 | 50 | | } |
| | | 51 | | |
| | 27 | 52 | | responseMessage.Headers = newHeaders; |
| | | 53 | | |
| | 27 | 54 | | return responseMessage; |
| | 27 | 55 | | } |
| | | 56 | | |
| | | 57 | | private static void TransformBodyAsJson(object template, ResponseMessage original, ResponseMessage responseMessa |
| | 13 | 58 | | { |
| | | 59 | | JToken jToken; |
| | 13 | 60 | | switch (original.BodyAsJson) |
| | | 61 | | { |
| | | 62 | | case JObject bodyAsJObject: |
| | 0 | 63 | | jToken = bodyAsJObject; |
| | 0 | 64 | | break; |
| | | 65 | | |
| | | 66 | | case Array bodyAsArray: |
| | 1 | 67 | | jToken = JArray.FromObject(bodyAsArray); |
| | 1 | 68 | | break; |
| | | 69 | | |
| | | 70 | | default: |
| | 12 | 71 | | jToken = JObject.FromObject(original.BodyAsJson); |
| | 12 | 72 | | break; |
| | | 73 | | } |
| | | 74 | | |
| | 13 | 75 | | WalkNode(jToken, template); |
| | | 76 | | |
| | 10 | 77 | | responseMessage.BodyAsJson = jToken; |
| | 10 | 78 | | } |
| | | 79 | | |
| | | 80 | | private static void WalkNode(JToken node, object template) |
| | 30 | 81 | | { |
| | 30 | 82 | | if (node.Type == JTokenType.Object) |
| | 12 | 83 | | { |
| | | 84 | | // In case of Object, loop all children. Do a ToArray() to avoid `Collection was modified` exceptions. |
| | 57 | 85 | | foreach (JProperty child in node.Children<JProperty>().ToArray()) |
| | 12 | 86 | | { |
| | 12 | 87 | | WalkNode(child.Value, template); |
| | 9 | 88 | | } |
| | 9 | 89 | | } |
| | 18 | 90 | | else if (node.Type == JTokenType.Array) |
| | 1 | 91 | | { |
| | | 92 | | // In case of Array, loop all items. Do a ToArray() to avoid `Collection was modified` exceptions. |
| | 13 | 93 | | foreach (JToken child in node.Children().ToArray()) |
| | 5 | 94 | | { |
| | 5 | 95 | | WalkNode(child, template); |
| | 5 | 96 | | } |
| | 1 | 97 | | } |
| | 17 | 98 | | else if (node.Type == JTokenType.String) |
| | 17 | 99 | | { |
| | | 100 | | // In case of string, try to transform the value. |
| | 17 | 101 | | string stringValue = node.Value<string>(); |
| | 17 | 102 | | if (string.IsNullOrEmpty(stringValue)) |
| | 0 | 103 | | { |
| | 0 | 104 | | return; |
| | | 105 | | } |
| | | 106 | | |
| | 17 | 107 | | var templateForStringValue = Handlebars.Compile(stringValue); |
| | 17 | 108 | | string transformedString = templateForStringValue(template); |
| | 14 | 109 | | if (!string.Equals(stringValue, transformedString)) |
| | 12 | 110 | | { |
| | | 111 | | JToken value; |
| | | 112 | | try |
| | 12 | 113 | | { |
| | | 114 | | // Try to convert this string into a real JsonObject |
| | 12 | 115 | | value = JToken.Parse(transformedString); |
| | 2 | 116 | | } |
| | 10 | 117 | | catch (JsonException) |
| | 10 | 118 | | { |
| | | 119 | | // Ignore JsonException and just convert to JToken |
| | 10 | 120 | | value = transformedString; |
| | 10 | 121 | | } |
| | | 122 | | |
| | 12 | 123 | | node.Replace(value); |
| | 12 | 124 | | } |
| | 14 | 125 | | } |
| | 24 | 126 | | } |
| | | 127 | | |
| | | 128 | | private static void TransformBodyAsString(object template, ResponseMessage original, ResponseMessage responseMes |
| | 19 | 129 | | { |
| | 19 | 130 | | var templateBody = Handlebars.Compile(original.Body); |
| | | 131 | | |
| | 19 | 132 | | responseMessage.Body = templateBody(template); |
| | 17 | 133 | | } |
| | | 134 | | } |
| | | 135 | | } |