From 94dc1a0dc14a4b220483f057d2333700536f84cc Mon Sep 17 00:00:00 2001 From: Cezary Piatek Date: Mon, 22 Jan 2024 17:13:44 +0100 Subject: [PATCH] Fix code generator for request and response body --- .../CodeGenerators/CollectionExtensions.cs | 16 +++ .../CodeGenerators/MappingCodeGenerator.cs | 122 ++++++++++++------ .../CodeGenerators/default_template.liquid | 14 +- 3 files changed, 104 insertions(+), 48 deletions(-) create mode 100644 src/WireMockInspector/CodeGenerators/CollectionExtensions.cs diff --git a/src/WireMockInspector/CodeGenerators/CollectionExtensions.cs b/src/WireMockInspector/CodeGenerators/CollectionExtensions.cs new file mode 100644 index 0000000..7e3f92d --- /dev/null +++ b/src/WireMockInspector/CodeGenerators/CollectionExtensions.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; + +namespace WireMockInspector.CodeGenerators; + +internal static class CollectionExtensions +{ + public static IDictionary? OrNullWhenEmpty(this IDictionary? @this) + { + if (@this == null || @this.Count == 0) + { + return null; + } + + return @this; + } +} \ No newline at end of file diff --git a/src/WireMockInspector/CodeGenerators/MappingCodeGenerator.cs b/src/WireMockInspector/CodeGenerators/MappingCodeGenerator.cs index 5bb12dc..76f86ef 100644 --- a/src/WireMockInspector/CodeGenerators/MappingCodeGenerator.cs +++ b/src/WireMockInspector/CodeGenerators/MappingCodeGenerator.cs @@ -1,6 +1,9 @@ -using System.IO; +using System; +using System.Collections.Specialized; +using System.IO; using System.Linq; using System.Reflection; +using System.Web; using Fluid; using Fluid.Values; using Newtonsoft.Json; @@ -54,6 +57,24 @@ private static string ReadEmbeddedResource(string resourceName) return reader.ReadToEnd(); } public const string DefaultTemplateName = "(default)"; + + + private static JToken? TryParseJson(string? payload) + { + try + { + return payload switch + { + { } => JToken.Parse(payload), + _ => null + }; + } + catch (Exception e) + { + return null; + } + } + public static string GenerateCSharpCode(LogRequestModel logRequest, LogResponseModel logResponse, MappingCodeGeneratorConfigViewModel config) { var options = new TemplateOptions(); @@ -95,47 +116,48 @@ public static string GenerateCSharpCode(LogRequestModel logRequest, LogResponseM var reader = new JsonDataSourceReader(); var data = reader.Read(JsonConvert.SerializeObject( - new + new + { + request = new { - request = new - { - ClientIP = logRequest.ClientIP, - DateTime = logRequest.DateTime, - Path = logRequest.Path, - AbsolutePath = logRequest.AbsolutePath, - Url = logRequest.Url, - AbsoluteUrl = logRequest.AbsoluteUrl, - ProxyUrl = logRequest.ProxyUrl, - Query = logRequest.Query, - Method = logRequest.Method, - Headers = logRequest.Headers, - Cookies = logRequest.Cookies, - Body = logRequest.Body, - BodyAsJson = logRequest.BodyAsJson?.ToString(), - BodyAsBytes = logRequest.BodyAsBytes, - BodyEncoding = logRequest.BodyEncoding, - DetectedBodyType = logRequest.DetectedBodyType, - DetectedBodyTypeFromContentType = logRequest.DetectedBodyTypeFromContentType - }, - response = new - { - StatusCode = logResponse.StatusCode, - Headers = logResponse.Headers, - BodyDestination = logResponse.BodyDestination, - Body = logResponse.Body, - BodyAsJson = logResponse.BodyAsJson?.ToString(), - BodyAsBytes = logResponse.BodyAsBytes, - BodyAsFile = logResponse.BodyAsFile, - BodyAsFileIsCached = logResponse.BodyAsFileIsCached, - BodyOriginal = logResponse.BodyOriginal, - BodyEncoding = logResponse.BodyEncoding, - DetectedBodyType = logResponse.DetectedBodyType, - DetectedBodyTypeFromContentType = logResponse.DetectedBodyTypeFromContentType, - FaultType = logResponse.FaultType, - FaultPercentage = logResponse.FaultPercentage - }, - config - })); + ClientIP = logRequest.ClientIP, + DateTime = logRequest.DateTime, + Path = logRequest.Path, + FullPath = GetFullPath(logRequest), + AbsolutePath = logRequest.AbsolutePath, + Url = logRequest.Url, + AbsoluteUrl = logRequest.AbsoluteUrl, + ProxyUrl = logRequest.ProxyUrl, + Query = logRequest.Query.OrNullWhenEmpty(), + Method = logRequest.Method, + Headers = logRequest.Headers.OrNullWhenEmpty(), + Cookies = logRequest.Cookies.OrNullWhenEmpty(), + Body = logRequest.Body, + BodyAsJson = (TryParseJson(logRequest.Body) ?? logRequest.BodyAsJson)?.ToString(), + BodyAsBytes = logRequest.BodyAsBytes, + BodyEncoding = logRequest.BodyEncoding, + DetectedBodyType = logRequest.DetectedBodyType, + DetectedBodyTypeFromContentType = logRequest.DetectedBodyTypeFromContentType + }, + response = new + { + StatusCode = logResponse.StatusCode, + Headers = logResponse.Headers.OrNullWhenEmpty(), + BodyDestination = logResponse.BodyDestination, + Body = logResponse.Body, + BodyAsJson = (TryParseJson(logResponse.Body) ?? logResponse.BodyAsJson)?.ToString(), + BodyAsBytes = logResponse.BodyAsBytes, + BodyAsFile = logResponse.BodyAsFile, + BodyAsFileIsCached = logResponse.BodyAsFileIsCached, + BodyOriginal = logResponse.BodyOriginal, + BodyEncoding = logResponse.BodyEncoding, + DetectedBodyType = logResponse.DetectedBodyType, + DetectedBodyTypeFromContentType = logResponse.DetectedBodyTypeFromContentType, + FaultType = logResponse.FaultType, + FaultPercentage = logResponse.FaultPercentage + }, + config + })); var result = ftemplate.Render(new TemplateContext(new { data = data @@ -146,4 +168,22 @@ public static string GenerateCSharpCode(LogRequestModel logRequest, LogResponseM return error; } + + private static string GetFullPath(LogRequestModel logRequest) + { + var query = HttpUtility.ParseQueryString(""); + if (logRequest.Query is { } requestQuery) + { + foreach (var p in requestQuery) + { + query[p.Key] = p.Value.ToString(); + } + } + var fullQuery = query.ToString(); + if (string.IsNullOrWhiteSpace(fullQuery) == false) + { + return $"{logRequest.Path}?{fullQuery}"; + } + return logRequest.Path; + } } \ No newline at end of file diff --git a/src/WireMockInspector/CodeGenerators/default_template.liquid b/src/WireMockInspector/CodeGenerators/default_template.liquid index 4973e3b..246529d 100644 --- a/src/WireMockInspector/CodeGenerators/default_template.liquid +++ b/src/WireMockInspector/CodeGenerators/default_template.liquid @@ -48,11 +48,11 @@ mappingBuilder {%- endfor -%} {%- endif -%} {%- if config.IncludeBody %} - {%- if request.Body %} - {%- assign body = request.Body | escape_string_for_csharp %} - .WithBody({{ body }}) - {%- elsif request.BodyAsJson %} + {%- if request.BodyAsJson %} .WithBodyAsJson({{ request.BodyAsJson | format_as_anonymous_object: 2 }}) + {%- elsif request.Body %} + {%- assign body = request.Body | escape_string_for_csharp %} + .WithBody({{ body }}) {%- endif -%} {%- endif -%} ) @@ -66,11 +66,11 @@ mappingBuilder {%- endfor -%} {%- endif -%} {%- if config.IncludeBodyResponse %} - {%- if response.Body %} + {%- if response.BodyAsJson %} + .WithBodyAsJson({{ response.BodyAsJson | format_as_anonymous_object: 2 }}) + {%- elsif response.Body %} {%- assign body = response.Body | escape_string_for_csharp %} .WithBody({{ body }}) - {%- elsif response.BodyAsJson %} - .WithBodyAsJson({{ response.BodyAsJson | format_as_anonymous_object: 2 }}) {%- endif -%} {%- endif %} ); \ No newline at end of file