mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-23 09:30:59 +01:00
Fix BodyParser to correctly check for json (#1297)
* Fix BodyParser to correctly check for json * JsonUtils
This commit is contained in:
@@ -8,18 +8,8 @@ namespace WireMock.Util;
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static class IBodyDataExtensions
|
||||
{
|
||||
public static BodyType GetBodyType(this IBodyData bodyData)
|
||||
public static BodyType GetDetectedBodyType(this IBodyData bodyData)
|
||||
{
|
||||
if (bodyData.DetectedBodyTypeFromContentType is not null and not BodyType.None)
|
||||
{
|
||||
return bodyData.DetectedBodyTypeFromContentType.Value;
|
||||
}
|
||||
|
||||
if (bodyData.DetectedBodyType is not null and not BodyType.None)
|
||||
{
|
||||
return bodyData.DetectedBodyType.Value;
|
||||
}
|
||||
|
||||
return BodyType.None;
|
||||
return bodyData.DetectedBodyType ?? BodyType.None;
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ internal static class HttpRequestMessageHelper
|
||||
}
|
||||
|
||||
var bodyData = requestMessage.BodyData;
|
||||
httpRequestMessage.Content = bodyData?.GetBodyType() switch
|
||||
httpRequestMessage.Content = bodyData?.DetectedBodyType switch
|
||||
{
|
||||
BodyType.Bytes => ByteArrayContentHelper.Create(bodyData.BodyAsBytes!, contentType),
|
||||
BodyType.Json => StringContentHelper.Create(JsonConvert.SerializeObject(bodyData.BodyAsJson), contentType),
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace WireMock.Owin.Mappers
|
||||
}
|
||||
|
||||
var bodyData = responseMessage.BodyData;
|
||||
if (bodyData?.GetBodyType() == BodyType.SseString)
|
||||
if (bodyData?.GetDetectedBodyType() == BodyType.SseString)
|
||||
{
|
||||
await HandleSseStringAsync(responseMessage, response, bodyData);
|
||||
return;
|
||||
@@ -166,7 +166,7 @@ namespace WireMock.Owin.Mappers
|
||||
private async Task<byte[]?> GetNormalBodyAsync(IResponseMessage responseMessage)
|
||||
{
|
||||
var bodyData = responseMessage.BodyData;
|
||||
switch (bodyData?.GetBodyType())
|
||||
switch (bodyData?.GetDetectedBodyType())
|
||||
{
|
||||
case BodyType.String:
|
||||
case BodyType.FormUrlEncoded:
|
||||
|
||||
@@ -177,7 +177,7 @@ internal static class BodyParser
|
||||
}
|
||||
|
||||
// If string is not null or empty, try to deserialize the string to a JObject
|
||||
if (settings.DeserializeJson && !string.IsNullOrEmpty(data.BodyAsString))
|
||||
if (settings.DeserializeJson && JsonUtils.IsJson(data.BodyAsString))
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -197,7 +197,7 @@ internal static class BodyParser
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
private static async Task<(string? ContentType, byte[] Bytes)> ReadBytesAsync(Stream stream, string? contentEncoding = null, bool decompressGZipAndDeflate = true)
|
||||
{
|
||||
using var memoryStream = new MemoryStream();
|
||||
|
||||
@@ -12,17 +12,23 @@ namespace WireMock.Util;
|
||||
|
||||
internal static class JsonUtils
|
||||
{
|
||||
public static bool TryParseAsJObject(string? strInput, [NotNullWhen(true)] out JObject? value)
|
||||
public static bool IsJson(string? value)
|
||||
{
|
||||
value = null;
|
||||
|
||||
if (strInput == null || string.IsNullOrWhiteSpace(strInput))
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
strInput = strInput.Trim();
|
||||
if ((!strInput.StartsWith("{") || !strInput.EndsWith("}")) && (!strInput.StartsWith("[") || !strInput.EndsWith("]")))
|
||||
value = value!.Trim();
|
||||
|
||||
return (value.StartsWith("{") && value.EndsWith("}")) || (value.StartsWith("[") && value.EndsWith("]"));
|
||||
}
|
||||
|
||||
public static bool TryParseAsJObject(string? strInput, [NotNullWhen(true)] out JObject? value)
|
||||
{
|
||||
value = null;
|
||||
|
||||
if (!IsJson(strInput))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -30,7 +36,7 @@ internal static class JsonUtils
|
||||
try
|
||||
{
|
||||
// Try to convert this string into a JToken
|
||||
value = JObject.Parse(strInput);
|
||||
value = JObject.Parse(strInput!);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
|
||||
Reference in New Issue
Block a user