Skip to content

Commit

Permalink
Fix request body presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Jul 15, 2023
1 parent e206b19 commit cda0ffa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
22 changes: 13 additions & 9 deletions src/WireMockInspector/ViewModels/MainWindowViewModel.cs
Expand Up @@ -574,28 +574,32 @@ private static MappingDetails GetMappingDetails(RequestViewModel req, MappingMod
Matched = isPerfectMatch,
ActualValue = new MarkdownActualValue
{
Value = req.Raw.Response?.DetectedBodyType.ToString() switch
{
"String" or "FormUrlEncoded" => WrapBodyInMarkdown( req.Raw.Response.Body?? string.Empty),
"Json" => new Markdown("json",req.Raw.Response.BodyAsJson?.ToString() ?? string.Empty),
"Bytes" => new Markdown("plaintext", req.Raw.Response.BodyAsBytes?.ToString()?? string.Empty),
"File" => new Markdown("plaintext","[FileContent]"),
_ => new Markdown("plaintext","[FileContent]"),
}
Value = GetActualForRequestBody(req)
},
Expectations = expectations.Response switch
{
{Body: {} bodyResponse} => WrapBodyInMarkdown(bodyResponse),
{BodyAsJson: {} bodyAsJson} => new Markdown("json", bodyAsJson.ToString()!),
{BodyAsBytes: {} bodyAsBytes} => new Markdown("plaintext", bodyAsBytes.ToString()?? string.Empty),
{BodyAsFile: {} bodyAsFile} => new Markdown("plaintext",bodyAsFile),
_ => new Markdown("plaintext","[FileContent]")
_ => new Markdown("plaintext",string.Empty)
}
}
}
};
}

private static Markdown GetActualForRequestBody(RequestViewModel req)
{
return req.Raw.Response?.DetectedBodyType.ToString() switch
{
"Json" => new Markdown("json",req.Raw.Response.BodyAsJson?.ToString() ?? string.Empty),
"Bytes" => new Markdown("plaintext", req.Raw.Response.BodyAsBytes?.ToString()?? string.Empty),
"File" => new Markdown("plaintext",req.Raw.Response.BodyAsFile?.ToString() ?? string.Empty),
_ => WrapBodyInMarkdown( req.Raw.Response?.Body?? string.Empty),
};
}

private static Markdown WrapBodyInMarkdown(string bodyResponse)
{
var cleanBody = bodyResponse.Trim();
Expand Down
7 changes: 6 additions & 1 deletion src/WireMockInspector/ViewModels/MatchDetailsViewModel.cs
Expand Up @@ -133,7 +133,12 @@ public record Markdown(string lang, string rawValue)
{
public string AsMarkdownSyntax()
{
return $"```{lang}\r\n{rawValue}\r\n```";
if (string.IsNullOrWhiteSpace(rawValue) == false)
{
return $"```{lang}\r\n{rawValue}\r\n```";
}

return string.Empty;
}

public override string ToString() => AsMarkdownSyntax();
Expand Down

0 comments on commit cda0ffa

Please sign in to comment.