mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-21 16:01:18 +02:00
Fix WithBody when using Pact and added more nullable annotations (#783)
* More nullable annotations * . * . * FIX * pact * . * p * xxx * ... * auth * array * ...
This commit is contained in:
@@ -140,7 +140,7 @@ internal static class LogEntryMapper
|
||||
MatchDetails = matchResult.MatchDetails.Select(md => new
|
||||
{
|
||||
Name = md.MatcherType.Name.Replace("RequestMessage", string.Empty),
|
||||
Score = md.Score
|
||||
md.Score
|
||||
} as object).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Settings;
|
||||
@@ -116,9 +117,10 @@ internal class MappingConverter
|
||||
mappingModel.Response.Delay = (int?)(response.Delay == Timeout.InfiniteTimeSpan ? TimeSpan.MaxValue.TotalMilliseconds : response.Delay?.TotalMilliseconds);
|
||||
}
|
||||
|
||||
if (mapping.Webhooks?.Length == 1)
|
||||
var nonNullableWebHooks = mapping.Webhooks?.Where(wh => wh != null).ToArray() ?? new IWebhook[0];
|
||||
if (nonNullableWebHooks.Length == 1)
|
||||
{
|
||||
mappingModel.Webhook = WebhookMapper.Map(mapping.Webhooks[0]);
|
||||
mappingModel.Webhook = WebhookMapper.Map(nonNullableWebHooks[0]);
|
||||
}
|
||||
else if (mapping.Webhooks?.Length > 1)
|
||||
{
|
||||
@@ -209,7 +211,7 @@ internal class MappingConverter
|
||||
break;
|
||||
}
|
||||
|
||||
if (response.ResponseMessage.BodyData.Encoding != null && response.ResponseMessage.BodyData.Encoding.WebName != "utf-8")
|
||||
if (response.ResponseMessage.BodyData?.Encoding != null && response.ResponseMessage.BodyData.Encoding.WebName != "utf-8")
|
||||
{
|
||||
mappingModel.Response.BodyEncoding = new EncodingModel
|
||||
{
|
||||
|
||||
@@ -22,10 +22,7 @@ internal class MappingToFileSaver
|
||||
|
||||
public void SaveMappingToFile(IMapping mapping, string? folder = null)
|
||||
{
|
||||
if (folder == null)
|
||||
{
|
||||
folder = _settings.FileSystemHandler.GetMappingFolder();
|
||||
}
|
||||
folder ??= _settings.FileSystemHandler.GetMappingFolder();
|
||||
|
||||
if (!_settings.FileSystemHandler.FolderExists(folder))
|
||||
{
|
||||
|
||||
@@ -208,7 +208,7 @@ internal class MatcherMapper
|
||||
return patternAsStringArray.ToAnyOfPatterns();
|
||||
}
|
||||
|
||||
if (matcher.Patterns?.OfType<string>() is IEnumerable<string> patternsAsStringArray)
|
||||
if (matcher.Patterns?.OfType<string>() is { } patternsAsStringArray)
|
||||
{
|
||||
return patternsAsStringArray.ToAnyOfPatterns();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using DevLab.JmesPath.Interop;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Extensions;
|
||||
using WireMock.Matchers;
|
||||
@@ -76,10 +77,25 @@ internal static class PactMapper
|
||||
{
|
||||
Status = MapStatusCode(response.StatusCode),
|
||||
Headers = MapResponseHeaders(response.Headers),
|
||||
Body = response.BodyAsJson
|
||||
Body = MapBody(response)
|
||||
};
|
||||
}
|
||||
|
||||
private static object? MapBody(ResponseModel? response)
|
||||
{
|
||||
if (response?.BodyAsJson != null)
|
||||
{
|
||||
return response.BodyAsJson;
|
||||
}
|
||||
|
||||
if (response?.Body != null) // In case the body is a string, try to deserialize into object, else just return the string
|
||||
{
|
||||
return JsonUtils.TryDeserializeObject<object?>(response.Body) ?? response.Body;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int MapStatusCode(object? statusCode)
|
||||
{
|
||||
if (statusCode is string statusCodeAsString)
|
||||
@@ -138,13 +154,13 @@ internal static class PactMapper
|
||||
return jsonMatcher?.Pattern;
|
||||
}
|
||||
|
||||
private static string GetPatternAsStringFromMatchers(MatcherModel[]? matchers, string defaultValue)
|
||||
{
|
||||
if (matchers != null && matchers.Any() && matchers[0].Pattern is string patternAsString)
|
||||
{
|
||||
return patternAsString;
|
||||
}
|
||||
//private static string GetPatternAsStringFromMatchers(MatcherModel[]? matchers, string defaultValue)
|
||||
//{
|
||||
// if (matchers != null && matchers.Any() && matchers[0].Pattern is string patternAsString)
|
||||
// {
|
||||
// return patternAsString;
|
||||
// }
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
// return defaultValue;
|
||||
//}
|
||||
}
|
||||
@@ -1,112 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Http;
|
||||
using WireMock.Models;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Serialization
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal static class WebhookMapper
|
||||
{
|
||||
internal static class WebhookMapper
|
||||
public static IWebhook Map(WebhookModel model)
|
||||
{
|
||||
public static IWebhook Map(WebhookModel model)
|
||||
var webhook = new Webhook
|
||||
{
|
||||
var webhook = new Webhook
|
||||
Request = new WebhookRequest
|
||||
{
|
||||
Request = new WebhookRequest
|
||||
{
|
||||
Url = model.Request.Url,
|
||||
Method = model.Request.Method,
|
||||
Headers = model.Request.Headers?.ToDictionary(x => x.Key, x => new WireMockList<string>(x.Value)) ?? new Dictionary<string, WireMockList<string>>()
|
||||
}
|
||||
};
|
||||
Url = model.Request.Url,
|
||||
Method = model.Request.Method,
|
||||
Headers = model.Request.Headers?.ToDictionary(x => x.Key, x => new WireMockList<string>(x.Value)) ?? new Dictionary<string, WireMockList<string>>()
|
||||
}
|
||||
};
|
||||
|
||||
if (model.Request.UseTransformer == true)
|
||||
if (model.Request.UseTransformer == true)
|
||||
{
|
||||
webhook.Request.UseTransformer = true;
|
||||
if (!Enum.TryParse<TransformerType>(model.Request.TransformerType, out var transformerType))
|
||||
{
|
||||
webhook.Request.UseTransformer = true;
|
||||
if (!Enum.TryParse<TransformerType>(model.Request.TransformerType, out var transformerType))
|
||||
{
|
||||
transformerType = TransformerType.Handlebars;
|
||||
}
|
||||
webhook.Request.TransformerType = transformerType;
|
||||
transformerType = TransformerType.Handlebars;
|
||||
}
|
||||
webhook.Request.TransformerType = transformerType;
|
||||
|
||||
if (!Enum.TryParse<ReplaceNodeOptions>(model.Request.TransformerReplaceNodeOptions, out var option))
|
||||
{
|
||||
option = ReplaceNodeOptions.None;
|
||||
}
|
||||
|
||||
webhook.Request.TransformerReplaceNodeOptions = option;
|
||||
if (!Enum.TryParse<ReplaceNodeOptions>(model.Request.TransformerReplaceNodeOptions, out var option))
|
||||
{
|
||||
option = ReplaceNodeOptions.None;
|
||||
}
|
||||
|
||||
IEnumerable<string> contentTypeHeader = null;
|
||||
if (webhook.Request.Headers.Any(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
contentTypeHeader = webhook.Request.Headers.First(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)).Value;
|
||||
}
|
||||
|
||||
if (model.Request.Body != null)
|
||||
{
|
||||
webhook.Request.BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = model.Request.Body,
|
||||
DetectedBodyType = BodyType.String,
|
||||
DetectedBodyTypeFromContentType = BodyParser.DetectBodyTypeFromContentType(contentTypeHeader?.FirstOrDefault())
|
||||
};
|
||||
}
|
||||
else if (model.Request.BodyAsJson != null)
|
||||
{
|
||||
webhook.Request.BodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = model.Request.BodyAsJson,
|
||||
DetectedBodyType = BodyType.Json,
|
||||
DetectedBodyTypeFromContentType = BodyParser.DetectBodyTypeFromContentType(contentTypeHeader?.FirstOrDefault())
|
||||
};
|
||||
}
|
||||
|
||||
return webhook;
|
||||
webhook.Request.TransformerReplaceNodeOptions = option;
|
||||
}
|
||||
|
||||
public static WebhookModel Map(IWebhook webhook)
|
||||
IEnumerable<string>? contentTypeHeader = null;
|
||||
if (webhook.Request.Headers != null && webhook.Request.Headers.Any(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
if (webhook?.Request == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var model = new WebhookModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
{
|
||||
Url = webhook.Request.Url,
|
||||
Method = webhook.Request.Method,
|
||||
Headers = webhook.Request.Headers?.ToDictionary(x => x.Key, x => x.Value.ToString()),
|
||||
UseTransformer = webhook.Request.UseTransformer,
|
||||
TransformerType = webhook.Request.UseTransformer == true ? webhook.Request.TransformerType.ToString() : null,
|
||||
TransformerReplaceNodeOptions = webhook.Request.TransformerReplaceNodeOptions.ToString()
|
||||
}
|
||||
};
|
||||
|
||||
if (webhook.Request.BodyData != null)
|
||||
{
|
||||
switch (webhook.Request.BodyData.DetectedBodyType)
|
||||
{
|
||||
case BodyType.String:
|
||||
model.Request.Body = webhook.Request.BodyData.BodyAsString;
|
||||
break;
|
||||
|
||||
case BodyType.Json:
|
||||
model.Request.BodyAsJson = webhook.Request.BodyData.BodyAsJson;
|
||||
break;
|
||||
|
||||
default:
|
||||
// Empty
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return model;
|
||||
contentTypeHeader = webhook.Request.Headers.First(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)).Value;
|
||||
}
|
||||
|
||||
if (model.Request.Body != null)
|
||||
{
|
||||
webhook.Request.BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = model.Request.Body,
|
||||
DetectedBodyType = BodyType.String,
|
||||
DetectedBodyTypeFromContentType = BodyParser.DetectBodyTypeFromContentType(contentTypeHeader?.FirstOrDefault())
|
||||
};
|
||||
}
|
||||
else if (model.Request.BodyAsJson != null)
|
||||
{
|
||||
webhook.Request.BodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = model.Request.BodyAsJson,
|
||||
DetectedBodyType = BodyType.Json,
|
||||
DetectedBodyTypeFromContentType = BodyParser.DetectBodyTypeFromContentType(contentTypeHeader?.FirstOrDefault())
|
||||
};
|
||||
}
|
||||
|
||||
return webhook;
|
||||
}
|
||||
|
||||
public static WebhookModel Map(IWebhook webhook)
|
||||
{
|
||||
Guard.NotNull(webhook);
|
||||
|
||||
var model = new WebhookModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
{
|
||||
Url = webhook.Request.Url,
|
||||
Method = webhook.Request.Method,
|
||||
Headers = webhook.Request.Headers?.ToDictionary(x => x.Key, x => x.Value.ToString()),
|
||||
UseTransformer = webhook.Request.UseTransformer,
|
||||
TransformerType = webhook.Request.UseTransformer == true ? webhook.Request.TransformerType.ToString() : null,
|
||||
TransformerReplaceNodeOptions = webhook.Request.TransformerReplaceNodeOptions.ToString()
|
||||
}
|
||||
};
|
||||
|
||||
if (webhook.Request.BodyData != null)
|
||||
{
|
||||
switch (webhook.Request.BodyData.DetectedBodyType)
|
||||
{
|
||||
case BodyType.String:
|
||||
model.Request.Body = webhook.Request.BodyData.BodyAsString;
|
||||
break;
|
||||
|
||||
case BodyType.Json:
|
||||
model.Request.BodyAsJson = webhook.Request.BodyData.BodyAsJson;
|
||||
break;
|
||||
|
||||
default:
|
||||
// Empty
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user