Skip to content

Commit

Permalink
Fix code generator for request and response body
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Jan 22, 2024
1 parent 51b249d commit 94dc1a0
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 48 deletions.
16 changes: 16 additions & 0 deletions src/WireMockInspector/CodeGenerators/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace WireMockInspector.CodeGenerators;

internal static class CollectionExtensions
{
public static IDictionary<string, T>? OrNullWhenEmpty<T>(this IDictionary<string, T>? @this)
{
if (@this == null || @this.Count == 0)
{
return null;
}

return @this;
}
}
122 changes: 81 additions & 41 deletions src/WireMockInspector/CodeGenerators/MappingCodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
14 changes: 7 additions & 7 deletions src/WireMockInspector/CodeGenerators/default_template.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
)
Expand All @@ -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 %}
);

0 comments on commit 94dc1a0

Please sign in to comment.