mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-21 08:21:53 +02:00
mm
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<VersionPrefix>1.25.0</VersionPrefix>
|
<VersionPrefix>2.0.0-preview-02</VersionPrefix>
|
||||||
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
|
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
|
||||||
<PackageProjectUrl>https://github.com/wiremock/WireMock.Net</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/wiremock/WireMock.Net</PackageProjectUrl>
|
||||||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
||||||
@@ -51,8 +51,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" PrivateAssets="All" />
|
<PackageReference Include="JetBrains.Annotations" Version="2025.2.4" PrivateAssets="All" />
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
|
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.103" PrivateAssets="All" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
|
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class ResponseModel
|
|||||||
public object? BodyAsJson { get; set; }
|
public object? BodyAsJson { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.
|
/// Gets or sets a value indicating whether the Json Body String needs to be indented.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool? BodyAsJsonIndented { get; set; }
|
public bool? BodyAsJsonIndented { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ public interface IBodyData
|
|||||||
object? BodyAsJson { get; set; }
|
object? BodyAsJson { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.
|
/// Gets or sets a value indicating whether the Json Body String needs to be indented.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
bool? BodyAsJsonIndented { get; set; }
|
bool? BodyAsJsonIndented { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
<Description>Commonly used interfaces, models, enumerations and types.</Description>
|
<Description>Commonly used interfaces, models, enumerations and types.</Description>
|
||||||
<AssemblyTitle>WireMock.Net.Abstractions</AssemblyTitle>
|
<AssemblyTitle>WireMock.Net.Abstractions</AssemblyTitle>
|
||||||
<Authors>Stef Heyenrath</Authors>
|
<Authors>Stef Heyenrath</Authors>
|
||||||
<!--<TargetFrameworks>net45;net451;net462;netstandard1.3;netstandard2.0;netstandard2.1</TargetFrameworks>-->
|
|
||||||
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
|
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<NoWarn>$(NoWarn);1591;8603</NoWarn>
|
<NoWarn>$(NoWarn);1591;8603</NoWarn>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Description>WireMock.Net.Routing extends WireMock.Net with modern, minimal-API-style routing for .NET</Description>
|
<Description>WireMock.Net.Routing extends WireMock.Net with modern, minimal-API-style routing for .NET</Description>
|
||||||
<Authors>Gennadii Saltyshchak</Authors>
|
<Authors>Gennadii Saltyshchak</Authors>
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.7.2" />
|
<PackageReference Include="JsonConverter.Abstractions" Version="0.8.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
<Description>GraphQL support for WireMock.Net</Description>
|
<Description>GraphQL support for WireMock.Net</Description>
|
||||||
<AssemblyTitle>WireMock.Net.Matchers.GraphQL</AssemblyTitle>
|
<AssemblyTitle>WireMock.Net.Matchers.GraphQL</AssemblyTitle>
|
||||||
<Authors>Stef Heyenrath</Authors>
|
<Authors>Stef Heyenrath</Authors>
|
||||||
<!--<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0;net8.0</TargetFrameworks>-->
|
|
||||||
<TargetFrameworks>net462;netstandard2.1;net8.0</TargetFrameworks>
|
<TargetFrameworks>net462;netstandard2.1;net8.0</TargetFrameworks>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<PackageTags>wiremock;matchers;matcher;graphql</PackageTags>
|
<PackageTags>wiremock;matchers;matcher;graphql</PackageTags>
|
||||||
|
|||||||
@@ -38,7 +38,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CS-Script" Version="4.11.0" />
|
<PackageReference Include="CS-Script" Version="4.13.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
@@ -164,8 +161,8 @@ public class MappingBuilder : IMappingBuilder
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string ToJson(object value)
|
private string ToJson(object value)
|
||||||
{
|
{
|
||||||
return JsonConvert.SerializeObject(value, JsonSerializationConstants.JsonSerializerSettingsDefault);
|
return _settings.DefaultJsonSerializer.Serialize(value, JsonSerializationConstants.JsonConverterOptionsDefault);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
49
src/WireMock.Net.Minimal/Serialization/MappingSerializer.cs
Normal file
49
src/WireMock.Net.Minimal/Serialization/MappingSerializer.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using JsonConverter.Abstractions;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER || NET461
|
||||||
|
using System.Text.Json;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace WireMock.Serialization;
|
||||||
|
|
||||||
|
internal class MappingSerializer(IJsonConverter jsonConverter)
|
||||||
|
{
|
||||||
|
internal T[] DeserializeJsonToArray<T>(string value)
|
||||||
|
{
|
||||||
|
return DeserializeObjectToArray<T>(jsonConverter.Deserialize<object>(value)!);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static T[] DeserializeObjectToArray<T>(object value)
|
||||||
|
{
|
||||||
|
if (value is JArray jArray)
|
||||||
|
{
|
||||||
|
return jArray.ToObject<T[]>()!;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value is JObject jObject)
|
||||||
|
{
|
||||||
|
var singleResult = jObject.ToObject<T>();
|
||||||
|
return [singleResult!];
|
||||||
|
}
|
||||||
|
|
||||||
|
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER || NET461
|
||||||
|
if (value is JsonElement jElement)
|
||||||
|
{
|
||||||
|
if (jElement.ValueKind == JsonValueKind.Array)
|
||||||
|
{
|
||||||
|
return jElement.Deserialize<T[]>()!;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jElement.ValueKind == JsonValueKind.Object)
|
||||||
|
{
|
||||||
|
var singleResult = jElement.Deserialize<T>();
|
||||||
|
return [singleResult!];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
throw new InvalidOperationException("Cannot deserialize the provided value to an array or object.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Newtonsoft.Json;
|
using JsonConverter.Abstractions;
|
||||||
|
using JsonConverter.Newtonsoft.Json;
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
|
|
||||||
@@ -12,12 +13,15 @@ internal class MappingToFileSaver
|
|||||||
{
|
{
|
||||||
private readonly WireMockServerSettings _settings;
|
private readonly WireMockServerSettings _settings;
|
||||||
private readonly MappingConverter _mappingConverter;
|
private readonly MappingConverter _mappingConverter;
|
||||||
|
private readonly IJsonConverter _jsonConverter;
|
||||||
private readonly MappingFileNameSanitizer _fileNameSanitizer;
|
private readonly MappingFileNameSanitizer _fileNameSanitizer;
|
||||||
|
|
||||||
public MappingToFileSaver(WireMockServerSettings settings, MappingConverter mappingConverter)
|
public MappingToFileSaver(WireMockServerSettings settings, MappingConverter mappingConverter)
|
||||||
{
|
{
|
||||||
_settings = Guard.NotNull(settings);
|
_settings = Guard.NotNull(settings);
|
||||||
_mappingConverter = Guard.NotNull(mappingConverter);
|
_mappingConverter = Guard.NotNull(mappingConverter);
|
||||||
|
|
||||||
|
_jsonConverter = settings.DefaultJsonSerializer ?? new NewtonsoftJsonConverter();
|
||||||
_fileNameSanitizer = new MappingFileNameSanitizer(settings);
|
_fileNameSanitizer = new MappingFileNameSanitizer(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,6 +60,8 @@ internal class MappingToFileSaver
|
|||||||
{
|
{
|
||||||
_settings.Logger.Info("Saving Mapping file {0}", path);
|
_settings.Logger.Info("Saving Mapping file {0}", path);
|
||||||
|
|
||||||
_settings.FileSystemHandler.WriteMappingFile(path, JsonConvert.SerializeObject(value, JsonSerializationConstants.JsonSerializerSettingsDefault));
|
var json = _jsonConverter.Serialize(value, JsonSerializationConstants.JsonConverterOptionsDefault);
|
||||||
|
|
||||||
|
_settings.FileSystemHandler.WriteMappingFile(path, json);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,8 +5,8 @@ using System.Linq;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
using JsonConverter.Abstractions;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
@@ -235,7 +235,7 @@ public partial class WireMockServer
|
|||||||
|
|
||||||
if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value))
|
if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value))
|
||||||
{
|
{
|
||||||
var mappingModels = DeserializeJsonToArray<MappingModel>(value);
|
var mappingModels = _mappingSerializer.DeserializeJsonToArray<MappingModel>(value);
|
||||||
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out var guidFromFilename))
|
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out var guidFromFilename))
|
||||||
{
|
{
|
||||||
ConvertMappingAndRegisterAsRespondProvider(mappingModels[0], guidFromFilename, path);
|
ConvertMappingAndRegisterAsRespondProvider(mappingModels[0], guidFromFilename, path);
|
||||||
@@ -823,25 +823,31 @@ public partial class WireMockServer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Encoding? ToEncoding(EncodingModel? encodingModel)
|
private ResponseMessage ToJson<T>(T result, bool keepNullValues = false, object? statusCode = null)
|
||||||
{
|
{
|
||||||
return encodingModel != null ? Encoding.GetEncoding(encodingModel.CodePage) : null;
|
var jsonOptions = new JsonConverterOptions
|
||||||
}
|
{
|
||||||
|
WriteIndented = true,
|
||||||
|
IgnoreNullValues = !keepNullValues
|
||||||
|
};
|
||||||
|
|
||||||
private static ResponseMessage ToJson<T>(T result, bool keepNullValues = false, object? statusCode = null)
|
|
||||||
{
|
|
||||||
return new ResponseMessage
|
return new ResponseMessage
|
||||||
{
|
{
|
||||||
BodyData = new BodyData
|
BodyData = new BodyData
|
||||||
{
|
{
|
||||||
DetectedBodyType = BodyType.String,
|
DetectedBodyType = BodyType.String,
|
||||||
BodyAsString = JsonConvert.SerializeObject(result, keepNullValues ? JsonSerializationConstants.JsonSerializerSettingsIncludeNullValues : JsonSerializationConstants.JsonSerializerSettingsDefault)
|
BodyAsString = _settings.DefaultJsonSerializer.Serialize(result!, jsonOptions)
|
||||||
},
|
},
|
||||||
StatusCode = statusCode ?? (int)HttpStatusCode.OK,
|
StatusCode = statusCode ?? (int)HttpStatusCode.OK,
|
||||||
Headers = new Dictionary<string, WireMockList<string>> { { HttpKnownHeaderNames.ContentType, new WireMockList<string>(WireMockConstants.ContentTypeJson) } }
|
Headers = new Dictionary<string, WireMockList<string>> { { HttpKnownHeaderNames.ContentType, new WireMockList<string>(WireMockConstants.ContentTypeJson) } }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Encoding? ToEncoding(EncodingModel? encodingModel)
|
||||||
|
{
|
||||||
|
return encodingModel != null ? Encoding.GetEncoding(encodingModel.CodePage) : null;
|
||||||
|
}
|
||||||
|
|
||||||
private static ResponseMessage ToResponseMessage(string text)
|
private static ResponseMessage ToResponseMessage(string text)
|
||||||
{
|
{
|
||||||
return new ResponseMessage
|
return new ResponseMessage
|
||||||
@@ -856,6 +862,18 @@ public partial class WireMockServer
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static T[] DeserializeRequestMessageToArray<T>(IRequestMessage requestMessage)
|
||||||
|
{
|
||||||
|
if (requestMessage.BodyData?.DetectedBodyType == BodyType.Json && requestMessage.BodyData.BodyAsJson != null)
|
||||||
|
{
|
||||||
|
var bodyAsJson = requestMessage.BodyData.BodyAsJson!;
|
||||||
|
|
||||||
|
return MappingSerializer.DeserializeObjectToArray<T>(bodyAsJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new NotSupportedException();
|
||||||
|
}
|
||||||
|
|
||||||
private static T DeserializeObject<T>(IRequestMessage requestMessage) where T : new()
|
private static T DeserializeObject<T>(IRequestMessage requestMessage) where T : new()
|
||||||
{
|
{
|
||||||
switch (requestMessage.BodyData?.DetectedBodyType)
|
switch (requestMessage.BodyData?.DetectedBodyType)
|
||||||
@@ -871,32 +889,4 @@ public partial class WireMockServer
|
|||||||
throw new NotSupportedException();
|
throw new NotSupportedException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static T[] DeserializeRequestMessageToArray<T>(IRequestMessage requestMessage)
|
|
||||||
{
|
|
||||||
if (requestMessage.BodyData?.DetectedBodyType == BodyType.Json && requestMessage.BodyData.BodyAsJson != null)
|
|
||||||
{
|
|
||||||
var bodyAsJson = requestMessage.BodyData.BodyAsJson;
|
|
||||||
|
|
||||||
return DeserializeObjectToArray<T>(bodyAsJson);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static T[] DeserializeJsonToArray<T>(string value)
|
|
||||||
{
|
|
||||||
return DeserializeObjectToArray<T>(JsonUtils.DeserializeObject(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static T[] DeserializeObjectToArray<T>(object value)
|
|
||||||
{
|
|
||||||
if (value is JArray jArray)
|
|
||||||
{
|
|
||||||
return jArray.ToObject<T[]>()!;
|
|
||||||
}
|
|
||||||
|
|
||||||
var singleResult = ((JObject)value).ToObject<T>();
|
|
||||||
return [singleResult!];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ public partial class WireMockServer
|
|||||||
|
|
||||||
if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value))
|
if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value))
|
||||||
{
|
{
|
||||||
var mappings = DeserializeJsonToArray<OrgMapping>(value);
|
var mappings = _mappingSerializer.DeserializeJsonToArray<OrgMapping>(value);
|
||||||
foreach (var mapping in mappings)
|
foreach (var mapping in mappings)
|
||||||
{
|
{
|
||||||
if (mappings.Length == 1 && Guid.TryParse(filenameWithoutExtension, out var guidFromFilename))
|
if (mappings.Length == 1 && Guid.TryParse(filenameWithoutExtension, out var guidFromFilename))
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ using System.Net.Http;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using AnyOfTypes;
|
using AnyOfTypes;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
using JsonConverter.Newtonsoft.Json;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
@@ -47,6 +48,7 @@ public partial class WireMockServer : IWireMockServer
|
|||||||
private readonly MappingBuilder _mappingBuilder;
|
private readonly MappingBuilder _mappingBuilder;
|
||||||
private readonly IGuidUtils _guidUtils = new GuidUtils();
|
private readonly IGuidUtils _guidUtils = new GuidUtils();
|
||||||
private readonly IDateTimeUtils _dateTimeUtils = new DateTimeUtils();
|
private readonly IDateTimeUtils _dateTimeUtils = new DateTimeUtils();
|
||||||
|
private readonly MappingSerializer _mappingSerializer;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
@@ -357,6 +359,8 @@ public partial class WireMockServer : IWireMockServer
|
|||||||
{
|
{
|
||||||
_settings = Guard.NotNull(settings);
|
_settings = Guard.NotNull(settings);
|
||||||
|
|
||||||
|
_mappingSerializer = new MappingSerializer(settings.DefaultJsonSerializer ?? new NewtonsoftJsonConverter());
|
||||||
|
|
||||||
// Set default values if not provided
|
// Set default values if not provided
|
||||||
_settings.Logger = settings.Logger ?? new WireMockNullLogger();
|
_settings.Logger = settings.Logger ?? new WireMockNullLogger();
|
||||||
_settings.FileSystemHandler = settings.FileSystemHandler ?? new LocalFileSystemHandler();
|
_settings.FileSystemHandler = settings.FileSystemHandler ?? new LocalFileSystemHandler();
|
||||||
@@ -631,7 +635,7 @@ public partial class WireMockServer : IWireMockServer
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public IWireMockServer WithMapping(string mappings)
|
public IWireMockServer WithMapping(string mappings)
|
||||||
{
|
{
|
||||||
var mappingModels = DeserializeJsonToArray<MappingModel>(mappings);
|
var mappingModels = _mappingSerializer.DeserializeJsonToArray<MappingModel>(mappings);
|
||||||
foreach (var mappingModel in mappingModels)
|
foreach (var mappingModel in mappingModels)
|
||||||
{
|
{
|
||||||
ConvertMappingAndRegisterAsRespondProvider(mappingModel, mappingModel.Guid ?? Guid.NewGuid());
|
ConvertMappingAndRegisterAsRespondProvider(mappingModel, mappingModel.Guid ?? Guid.NewGuid());
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
<Description>ProtoBuf and gRPC support for WireMock.Net</Description>
|
<Description>ProtoBuf and gRPC support for WireMock.Net</Description>
|
||||||
<AssemblyTitle>WireMock.Net.ProtoBuf</AssemblyTitle>
|
<AssemblyTitle>WireMock.Net.ProtoBuf</AssemblyTitle>
|
||||||
<Authors>Stef Heyenrath</Authors>
|
<Authors>Stef Heyenrath</Authors>
|
||||||
<!--<TargetFrameworks>netstandard2.1;net462;net6.0;net8.0</TargetFrameworks>-->
|
|
||||||
<TargetFrameworks>net462;netstandard2.1</TargetFrameworks>
|
<TargetFrameworks>net462;netstandard2.1</TargetFrameworks>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<PackageTags>wiremock;matchers;matcher;protobuf;grpc</PackageTags>
|
<PackageTags>wiremock;matchers;matcher;protobuf;grpc</PackageTags>
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.7.2" />
|
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.8.0" />
|
||||||
<PackageReference Include="RestEase" Version="1.6.4" />
|
<PackageReference Include="RestEase" Version="1.6.4" />
|
||||||
<PackageReference Include="Stef.Validation" Version="0.2.0" />
|
<PackageReference Include="Stef.Validation" Version="0.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
|
using JsonConverter.Abstractions;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
|
|
||||||
@@ -7,24 +8,30 @@ namespace WireMock.Serialization;
|
|||||||
|
|
||||||
internal static class JsonSerializationConstants
|
internal static class JsonSerializationConstants
|
||||||
{
|
{
|
||||||
public static readonly JsonSerializerSettings JsonSerializerSettingsDefault = new()
|
internal static readonly JsonConverterOptions JsonConverterOptionsDefault = new()
|
||||||
{
|
{
|
||||||
Formatting = Formatting.Indented,
|
WriteIndented = true,
|
||||||
NullValueHandling = NullValueHandling.Ignore
|
IgnoreNullValues = true
|
||||||
};
|
};
|
||||||
|
|
||||||
public static readonly JsonSerializerSettings JsonSerializerSettingsIncludeNullValues = new()
|
//internal static readonly JsonSerializerSettings JsonSerializerSettingsDefault = new()
|
||||||
|
//{
|
||||||
|
// Formatting = Formatting.Indented,
|
||||||
|
// NullValueHandling = NullValueHandling.Ignore
|
||||||
|
//};
|
||||||
|
|
||||||
|
internal static readonly JsonSerializerSettings JsonSerializerSettingsIncludeNullValues = new()
|
||||||
{
|
{
|
||||||
Formatting = Formatting.Indented,
|
Formatting = Formatting.Indented,
|
||||||
NullValueHandling = NullValueHandling.Include
|
NullValueHandling = NullValueHandling.Include
|
||||||
};
|
};
|
||||||
|
|
||||||
public static readonly JsonSerializerSettings JsonDeserializerSettingsWithDateParsingNone = new()
|
internal static readonly JsonSerializerSettings JsonDeserializerSettingsWithDateParsingNone = new()
|
||||||
{
|
{
|
||||||
DateParseHandling = DateParseHandling.None
|
DateParseHandling = DateParseHandling.None
|
||||||
};
|
};
|
||||||
|
|
||||||
public static readonly JsonSerializerSettings JsonSerializerSettingsPact = new()
|
internal static readonly JsonSerializerSettings JsonSerializerSettingsPact = new()
|
||||||
{
|
{
|
||||||
Formatting = Formatting.Indented,
|
Formatting = Formatting.Indented,
|
||||||
NullValueHandling = NullValueHandling.Ignore,
|
NullValueHandling = NullValueHandling.Ignore,
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System;
|
using System.Globalization;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using HandlebarsDotNet;
|
using HandlebarsDotNet;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
using JsonConverter.Abstractions;
|
||||||
|
using JsonConverter.Newtonsoft.Json;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Handlers;
|
using WireMock.Handlers;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
|
using WireMock.Models;
|
||||||
using WireMock.RegularExpressions;
|
using WireMock.RegularExpressions;
|
||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
using System.Globalization;
|
|
||||||
using WireMock.Models;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
|
|
||||||
namespace WireMock.Settings;
|
namespace WireMock.Settings;
|
||||||
|
|
||||||
@@ -148,7 +148,7 @@ public class WireMockServerSettings
|
|||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public Action<object>? PostWireMockMiddlewareInit { get; set; }
|
public Action<object>? PostWireMockMiddlewareInit { get; set; }
|
||||||
|
|
||||||
//#if USE_ASPNETCORE
|
//#if USE_ASPNETCORE
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Action which is called with IServiceCollection when ASP.NET Core DI is being configured. [Optional]
|
/// Action which is called with IServiceCollection when ASP.NET Core DI is being configured. [Optional]
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -161,7 +161,7 @@ public class WireMockServerSettings
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public CorsPolicyOptions? CorsPolicyOptions { get; set; }
|
public CorsPolicyOptions? CorsPolicyOptions { get; set; }
|
||||||
//#endif
|
//#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The IWireMockLogger which logs Debug, Info, Warning or Error
|
/// The IWireMockLogger which logs Debug, Info, Warning or Error
|
||||||
@@ -246,7 +246,7 @@ public class WireMockServerSettings
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public bool CustomCertificateDefined => CertificateSettings?.IsDefined == true;
|
public bool CustomCertificateDefined => CertificateSettings?.IsDefined == true;
|
||||||
|
|
||||||
//#if USE_ASPNETCORE
|
//#if USE_ASPNETCORE
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Client certificate mode for the server
|
/// Client certificate mode for the server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -257,7 +257,7 @@ public class WireMockServerSettings
|
|||||||
/// Whether to accept any client certificate
|
/// Whether to accept any client certificate
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool AcceptAnyClientCertificate { get; set; }
|
public bool AcceptAnyClientCertificate { get; set; }
|
||||||
//#endif
|
//#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines the global IWebhookSettings to use.
|
/// Defines the global IWebhookSettings to use.
|
||||||
@@ -347,6 +347,16 @@ public class WireMockServerSettings
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public ActivityTracingOptions? ActivityTracingOptions { get; set; }
|
public ActivityTracingOptions? ActivityTracingOptions { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the default JSON converter used for serialization.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Set this property to customize how objects are serialized to and deserialized from JSON during mapping.
|
||||||
|
/// Default is <see cref="NewtonsoftJsonConverter"/>.
|
||||||
|
/// </remarks>
|
||||||
|
[PublicAPI]
|
||||||
|
public IJsonConverter DefaultJsonSerializer { get; set; } = new NewtonsoftJsonConverter();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// WebSocket settings.
|
/// WebSocket settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Description>Shared interfaces, models, enumerations and types.</Description>
|
<Description>Shared interfaces, models, enumerations and types.</Description>
|
||||||
<Authors>Stef Heyenrath</Authors>
|
<Authors>Stef Heyenrath</Authors>
|
||||||
<!--<TargetFrameworks>net451;net452;net46;net462;netstandard1.3;netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>-->
|
|
||||||
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0</TargetFrameworks>
|
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0</TargetFrameworks>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<PackageTags>tdd;mock;http;wiremock;test;server;shared</PackageTags>
|
<PackageTags>tdd;mock;http;wiremock;test;server;shared</PackageTags>
|
||||||
@@ -30,9 +29,8 @@
|
|||||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.9" />
|
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.9" />
|
||||||
<PackageReference Include="Stef.Validation" Version="0.2.0" />
|
<PackageReference Include="Stef.Validation" Version="0.2.0" />
|
||||||
<PackageReference Include="AnyOf" Version="0.5.0.1" />
|
<PackageReference Include="AnyOf" Version="0.5.0.1" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
||||||
<PackageReference Include="JsonConverter.Abstractions" Version="0.7.2" />
|
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.8.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
<Description>Lightweight StandAlone Http Mocking Server for .Net.</Description>
|
<Description>Lightweight StandAlone Http Mocking Server for .Net.</Description>
|
||||||
<AssemblyTitle>WireMock.Net.StandAlone</AssemblyTitle>
|
<AssemblyTitle>WireMock.Net.StandAlone</AssemblyTitle>
|
||||||
<Authors>Stef Heyenrath</Authors>
|
<Authors>Stef Heyenrath</Authors>
|
||||||
<!--<TargetFrameworks>net451;net452;net46;net462;netstandard1.3;netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>-->
|
|
||||||
<TargetFrameworks>netstandard2.1;net8.0</TargetFrameworks>
|
<TargetFrameworks>netstandard2.1;net8.0</TargetFrameworks>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<AssemblyName>WireMock.Net.StandAlone</AssemblyName>
|
<AssemblyName>WireMock.Net.StandAlone</AssemblyName>
|
||||||
@@ -29,8 +28,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|
||||||
<!-- Enable OpenTelemetry exporter support for .NET 6+ -->
|
<!-- Enable OpenTelemetry exporter support for .NET 8+ -->
|
||||||
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0'">
|
<PropertyGroup Condition="' $(TargetFramework)' == 'net8.0' ">
|
||||||
<DefineConstants>$(DefineConstants);OPENTELEMETRY_SUPPORTED</DefineConstants>
|
<DefineConstants>$(DefineConstants);OPENTELEMETRY_SUPPORTED</DefineConstants>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
@@ -45,8 +44,8 @@
|
|||||||
<ProjectReference Include="..\WireMock.Net\WireMock.Net.csproj" />
|
<ProjectReference Include="..\WireMock.Net\WireMock.Net.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- OpenTelemetry exporter for .NET 6+ -->
|
<!-- OpenTelemetry exporter for .NET 8+ -->
|
||||||
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0'">
|
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
|
||||||
<ProjectReference Include="..\WireMock.Net.OpenTelemetry\WireMock.Net.OpenTelemetry.csproj" />
|
<ProjectReference Include="..\WireMock.Net.OpenTelemetry\WireMock.Net.OpenTelemetry.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -3,7 +3,6 @@
|
|||||||
<Description>Lightweight Http Mocking Server for .NET, inspired by WireMock from the Java landscape.</Description>
|
<Description>Lightweight Http Mocking Server for .NET, inspired by WireMock from the Java landscape.</Description>
|
||||||
<AssemblyTitle>WireMock.Net</AssemblyTitle>
|
<AssemblyTitle>WireMock.Net</AssemblyTitle>
|
||||||
<Authors>Stef Heyenrath</Authors>
|
<Authors>Stef Heyenrath</Authors>
|
||||||
<!--<TargetFrameworks>net451;net452;net46;net462;netstandard1.3;netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>-->
|
|
||||||
<TargetFrameworks>net462;netstandard2.1;net8.0</TargetFrameworks>
|
<TargetFrameworks>net462;netstandard2.1;net8.0</TargetFrameworks>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<AssemblyName>WireMock.Net</AssemblyName>
|
<AssemblyName>WireMock.Net</AssemblyName>
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
|
using JsonConverter.Newtonsoft.Json;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
@@ -14,7 +13,6 @@ using WireMock.ResponseBuilders;
|
|||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace WireMock.Net.Tests.ResponseBuilders;
|
namespace WireMock.Net.Tests.ResponseBuilders;
|
||||||
|
|
||||||
@@ -317,12 +315,11 @@ public class ResponseWithBodyTests
|
|||||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request1, _settings);
|
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request1, _settings);
|
||||||
|
|
||||||
Check.That(response.Message.StatusCode).IsEqualTo(200);
|
Check.That(response.Message.StatusCode).IsEqualTo(200);
|
||||||
Check.That(response.Message.BodyData.BodyAsString).Contains("File deleted.");
|
Check.That(response.Message.BodyData?.BodyAsString).Contains("File deleted.");
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !(NET451 || NET452)
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_WithBody_IJsonConverter_SystemTextJson()
|
public async Task Response_ProvideResponse_WithBody_NewtonsoftJsonConverter()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var requestBody = new BodyData
|
var requestBody = new BodyData
|
||||||
@@ -332,13 +329,34 @@ public class ResponseWithBodyTests
|
|||||||
};
|
};
|
||||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, requestBody);
|
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, requestBody);
|
||||||
|
|
||||||
var responseBuilder = Response.Create().WithBody(new { foo = "bar", n = 42 }, new JsonConverter.System.Text.Json.SystemTextJsonConverter());
|
var responseBuilder = Response.Create().WithBody(new { foo = "< > & ' 😀 👍 ❤️", n = 42 }, new NewtonsoftJsonConverter());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
response.Message.BodyData!.BodyAsString.Should().Be("""{"foo":"< > & ' 😀 👍 ❤️","n":42}""");
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !(NET451 || NET452 || NET461)
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponse_WithBody_SystemTextJsonConverter()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var requestBody = new BodyData
|
||||||
|
{
|
||||||
|
DetectedBodyType = BodyType.String,
|
||||||
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, requestBody);
|
||||||
|
|
||||||
|
var responseBuilder = Response.Create().WithBody(new { foo = "< > & ' 😀 👍 ❤️", n = 42 }, new JsonConverter.System.Text.Json.SystemTextJsonConverter());
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.Message.BodyData!.BodyAsString.Should().Be(@"{""foo"":""bar"",""n"":42}");
|
response.Message.BodyData!.BodyAsString.Should().Be("""{"foo":"\u003C \u003E \u0026 \u0027 \uD83D\uDE00 \uD83D\uDC4D \u2764\uFE0F","n":42}""");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
326
test/WireMock.Net.Tests/Serialization/MappingSerializerTests.cs
Normal file
326
test/WireMock.Net.Tests/Serialization/MappingSerializerTests.cs
Normal file
@@ -0,0 +1,326 @@
|
|||||||
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using FluentAssertions;
|
||||||
|
using JsonConverter.Newtonsoft.Json;
|
||||||
|
using WireMock.Admin.Mappings;
|
||||||
|
using WireMock.Serialization;
|
||||||
|
using Xunit;
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
using JsonConverter.System.Text.Json;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace WireMock.Net.Tests.Serialization;
|
||||||
|
|
||||||
|
public class MappingSerializerTests
|
||||||
|
{
|
||||||
|
private const string SingleMappingJson =
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"Guid": "12345678-1234-1234-1234-123456789012",
|
||||||
|
"Priority": 1,
|
||||||
|
"Request": {
|
||||||
|
"Path": "/test"
|
||||||
|
},
|
||||||
|
"Response": {
|
||||||
|
"StatusCode": 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
private const string ArrayMappingJson =
|
||||||
|
"""
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"Guid": "12345678-1234-1234-1234-123456789012",
|
||||||
|
"Priority": 1,
|
||||||
|
"Request": {
|
||||||
|
"Path": "/test1"
|
||||||
|
},
|
||||||
|
"Response": {
|
||||||
|
"StatusCode": 200
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Guid": "87654321-4321-4321-4321-210987654321",
|
||||||
|
"Priority": 2,
|
||||||
|
"Request": {
|
||||||
|
"Path": "/test2"
|
||||||
|
},
|
||||||
|
"Response": {
|
||||||
|
"StatusCode": 404
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
""";
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithNewtonsoftJson_SingleObject_ShouldReturnArray()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new NewtonsoftJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = serializer.DeserializeJsonToArray<MappingModel>(SingleMappingJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().NotBeNull();
|
||||||
|
result.Should().HaveCount(1);
|
||||||
|
result[0].Guid.Should().Be(Guid.Parse("12345678-1234-1234-1234-123456789012"));
|
||||||
|
result[0].Priority.Should().Be(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithNewtonsoftJson_Array_ShouldReturnArray()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new NewtonsoftJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = serializer.DeserializeJsonToArray<MappingModel>(ArrayMappingJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().NotBeNull();
|
||||||
|
result.Should().HaveCount(2);
|
||||||
|
result[0].Guid.Should().Be(Guid.Parse("12345678-1234-1234-1234-123456789012"));
|
||||||
|
result[0].Priority.Should().Be(1);
|
||||||
|
result[1].Guid.Should().Be(Guid.Parse("87654321-4321-4321-4321-210987654321"));
|
||||||
|
result[1].Priority.Should().Be(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithNewtonsoftJson_EmptyArray_ShouldReturnEmptyArray()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new NewtonsoftJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
var emptyArrayJson = "[]";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = serializer.DeserializeJsonToArray<MappingModel>(emptyArrayJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().NotBeNull();
|
||||||
|
result.Should().BeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithNewtonsoftJson_InvalidJson_ShouldThrowException()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new NewtonsoftJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
var invalidJson = "not valid json";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
Action act = () => serializer.DeserializeJsonToArray<MappingModel>(invalidJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
act.Should().Throw<Exception>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithNewtonsoftJson_ComplexMapping_ShouldDeserializeCorrectly()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new NewtonsoftJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
var complexJson =
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"Guid": "12345678-1234-1234-1234-123456789012",
|
||||||
|
"Title": "Test Mapping",
|
||||||
|
"Description": "A test mapping",
|
||||||
|
"Priority": 10,
|
||||||
|
"Request": {
|
||||||
|
"Path": "/api/test",
|
||||||
|
"Methods": ["GET", "POST"]
|
||||||
|
},
|
||||||
|
"Response": {
|
||||||
|
"StatusCode": 201,
|
||||||
|
"Body": "Test Response"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = serializer.DeserializeJsonToArray<MappingModel>(complexJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().NotBeNull();
|
||||||
|
result.Should().HaveCount(1);
|
||||||
|
result[0].Guid.Should().Be(Guid.Parse("12345678-1234-1234-1234-123456789012"));
|
||||||
|
result[0].Title.Should().Be("Test Mapping");
|
||||||
|
result[0].Description.Should().Be("A test mapping");
|
||||||
|
result[0].Priority.Should().Be(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithNewtonsoftJson_NullValue_ShouldThrowException()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new NewtonsoftJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
var nullJson = "null";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
Action act = () => serializer.DeserializeJsonToArray<MappingModel>(nullJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
act.Should().Throw<Exception>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithNewtonsoftJson_PrimitiveValue_ShouldThrowInvalidOperationException()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new NewtonsoftJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
var primitiveJson = "\"string value\"";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
Action act = () => serializer.DeserializeJsonToArray<MappingModel>(primitiveJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
act.Should().Throw<InvalidOperationException>()
|
||||||
|
.WithMessage("Cannot deserialize the provided value to an array or object.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithSystemTextJson_SingleObject_ShouldReturnArray()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new SystemTextJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = serializer.DeserializeJsonToArray<MappingModel>(SingleMappingJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().NotBeNull();
|
||||||
|
result.Should().HaveCount(1);
|
||||||
|
result[0].Guid.Should().Be(Guid.Parse("12345678-1234-1234-1234-123456789012"));
|
||||||
|
result[0].Priority.Should().Be(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithSystemTextJson_Array_ShouldReturnArray()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new SystemTextJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = serializer.DeserializeJsonToArray<MappingModel>(ArrayMappingJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().NotBeNull();
|
||||||
|
result.Should().HaveCount(2);
|
||||||
|
result[0].Guid.Should().Be(Guid.Parse("12345678-1234-1234-1234-123456789012"));
|
||||||
|
result[0].Priority.Should().Be(1);
|
||||||
|
result[1].Guid.Should().Be(Guid.Parse("87654321-4321-4321-4321-210987654321"));
|
||||||
|
result[1].Priority.Should().Be(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithSystemTextJson_EmptyArray_ShouldReturnEmptyArray()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new SystemTextJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
var emptyArrayJson = "[]";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = serializer.DeserializeJsonToArray<MappingModel>(emptyArrayJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().NotBeNull();
|
||||||
|
result.Should().BeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithSystemTextJson_InvalidJson_ShouldThrowException()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new SystemTextJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
var invalidJson = "not valid json";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
Action act = () => serializer.DeserializeJsonToArray<MappingModel>(invalidJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
act.Should().Throw<Exception>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithSystemTextJson_ComplexMapping_ShouldDeserializeCorrectly()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new SystemTextJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
var complexJson =
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"Guid": "12345678-1234-1234-1234-123456789012",
|
||||||
|
"Title": "Test Mapping",
|
||||||
|
"Description": "A test mapping",
|
||||||
|
"Priority": 10,
|
||||||
|
"Request": {
|
||||||
|
"Path": "/api/test",
|
||||||
|
"Methods": ["GET", "POST"]
|
||||||
|
},
|
||||||
|
"Response": {
|
||||||
|
"StatusCode": 201,
|
||||||
|
"Body": "Test Response"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = serializer.DeserializeJsonToArray<MappingModel>(complexJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().NotBeNull();
|
||||||
|
result.Should().HaveCount(1);
|
||||||
|
result[0].Guid.Should().Be(Guid.Parse("12345678-1234-1234-1234-123456789012"));
|
||||||
|
result[0].Title.Should().Be("Test Mapping");
|
||||||
|
result[0].Description.Should().Be("A test mapping");
|
||||||
|
result[0].Priority.Should().Be(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithSystemTextJson_NullValue_ShouldThrowException()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new SystemTextJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
var nullJson = "null";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
Action act = () => serializer.DeserializeJsonToArray<MappingModel>(nullJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
act.Should().Throw<Exception>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MappingSerializer_DeserializeJsonToArray_WithSystemTextJson_PrimitiveValue_ShouldThrowInvalidOperationException()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var jsonConverter = new SystemTextJsonConverter();
|
||||||
|
var serializer = new MappingSerializer(jsonConverter);
|
||||||
|
var primitiveJson = "\"string value\"";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
Action act = () => serializer.DeserializeJsonToArray<MappingModel>(primitiveJson);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
act.Should().Throw<InvalidOperationException>()
|
||||||
|
.WithMessage("Cannot deserialize the provided value to an array or object.");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
@@ -1,21 +1,15 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
#if !NET452
|
#if !NET452
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace WireMock.Net.Tests;
|
namespace WireMock.Net.Tests;
|
||||||
|
|
||||||
@@ -35,11 +29,10 @@ public partial class WireMockServerTests
|
|||||||
Request.Create()
|
Request.Create()
|
||||||
.WithPath("/a")
|
.WithPath("/a")
|
||||||
.WithBody(
|
.WithBody(
|
||||||
new IMatcher[]
|
[
|
||||||
{
|
|
||||||
new JmesPathMatcher("requestId == '1'"),
|
new JmesPathMatcher("requestId == '1'"),
|
||||||
new JmesPathMatcher("value == 'A'")
|
new JmesPathMatcher("value == 'A'")
|
||||||
},
|
],
|
||||||
MatchOperator.And
|
MatchOperator.And
|
||||||
)
|
)
|
||||||
.UsingPost()
|
.UsingPost()
|
||||||
@@ -50,11 +43,10 @@ public partial class WireMockServerTests
|
|||||||
Request.Create()
|
Request.Create()
|
||||||
.WithPath("/a")
|
.WithPath("/a")
|
||||||
.WithBody(
|
.WithBody(
|
||||||
new IMatcher[]
|
[
|
||||||
{
|
|
||||||
new JmesPathMatcher("requestId == '2'"),
|
new JmesPathMatcher("requestId == '2'"),
|
||||||
new JmesPathMatcher("value == 'A'")
|
new JmesPathMatcher("value == 'A'")
|
||||||
},
|
],
|
||||||
MatchOperator.And
|
MatchOperator.And
|
||||||
)
|
)
|
||||||
.UsingPost()
|
.UsingPost()
|
||||||
@@ -82,12 +74,11 @@ public partial class WireMockServerTests
|
|||||||
Request.Create()
|
Request.Create()
|
||||||
.WithPath("/a")
|
.WithPath("/a")
|
||||||
.WithBody(
|
.WithBody(
|
||||||
new IMatcher[]
|
[
|
||||||
{
|
|
||||||
new JmesPathMatcher("extra == 'X'"),
|
new JmesPathMatcher("extra == 'X'"),
|
||||||
new JmesPathMatcher("requestId == '1'"),
|
new JmesPathMatcher("requestId == '1'"),
|
||||||
new JmesPathMatcher("value == 'A'")
|
new JmesPathMatcher("value == 'A'")
|
||||||
},
|
],
|
||||||
MatchOperator.And
|
MatchOperator.And
|
||||||
)
|
)
|
||||||
.UsingPost()
|
.UsingPost()
|
||||||
@@ -99,11 +90,10 @@ public partial class WireMockServerTests
|
|||||||
Request.Create()
|
Request.Create()
|
||||||
.WithPath("/a")
|
.WithPath("/a")
|
||||||
.WithBody(
|
.WithBody(
|
||||||
new IMatcher[]
|
[
|
||||||
{
|
|
||||||
new JmesPathMatcher("requestId == '1'"),
|
new JmesPathMatcher("requestId == '1'"),
|
||||||
new JmesPathMatcher("value == 'A'")
|
new JmesPathMatcher("value == 'A'")
|
||||||
},
|
],
|
||||||
MatchOperator.And
|
MatchOperator.And
|
||||||
)
|
)
|
||||||
.UsingPost()
|
.UsingPost()
|
||||||
@@ -189,6 +179,7 @@ public partial class WireMockServerTests
|
|||||||
{"jsonrpc":"2.0","id":"{{request.bodyAsJson.id}}","result":{"protocolVersion":"2024-11-05","capabilities":{"logging":{},"prompts":{"listChanged":true},"resources":{"subscribe":true,"listChanged":true},"tools":{"listChanged":true}},"serverInfo":{"name":"ExampleServer","version":"1.0.0"}}}
|
{"jsonrpc":"2.0","id":"{{request.bodyAsJson.id}}","result":{"protocolVersion":"2024-11-05","capabilities":{"logging":{},"prompts":{"listChanged":true},"resources":{"subscribe":true,"listChanged":true},"tools":{"listChanged":true}},"serverInfo":{"name":"ExampleServer","version":"1.0.0"}}}
|
||||||
""")
|
""")
|
||||||
.WithStatusCode(200)
|
.WithStatusCode(200)
|
||||||
|
.WithTransformer(true)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
@@ -200,15 +191,47 @@ public partial class WireMockServerTests
|
|||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
|
||||||
var responseText = await response.RequestMessage!.Content!.ReadAsStringAsync();
|
var responseText = await response.Content.ReadAsStringAsync();
|
||||||
responseText.Should().Contain("ec475f56d4694b48bc737500ba575b35-1");
|
responseText.Should().Contain("ec475f56d4694b48bc737500ba575b35-1");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if NET6_0_OR_GREATER
|
||||||
|
[Fact]
|
||||||
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_And_SystemTextJson_ShouldMatch()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var server = WireMockServer.Start(x => x.DefaultJsonSerializer = new JsonConverter.System.Text.Json.SystemTextJsonConverter() );
|
||||||
|
|
||||||
|
var matcher = new JsonPartialWildcardMatcher(new { id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
||||||
|
server.Given(Request.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json*")
|
||||||
|
.UsingPost()
|
||||||
|
.WithPath("/system-text-json")
|
||||||
|
.WithBody(matcher)
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBody("OK")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var content = """{"id":"ec475f56d4694b48bc737500ba575b35-1"}""";
|
||||||
|
var response = await new HttpClient()
|
||||||
|
.PostAsync($"{server.Url}/system-text-json", new StringContent(content, Encoding.UTF8, "application/json"))
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
|
||||||
|
var responseText = await response.Content.ReadAsStringAsync();
|
||||||
|
responseText.Should().Contain("OK");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFunc()
|
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFunc()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create()
|
Request.Create()
|
||||||
.UsingPost()
|
.UsingPost()
|
||||||
@@ -220,7 +243,7 @@ public partial class WireMockServerTests
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("key1", "value1") });
|
var content = new FormUrlEncodedContent([new KeyValuePair<string, string>("key1", "value1")]);
|
||||||
var response = await new HttpClient()
|
var response = await new HttpClient()
|
||||||
.PostAsync($"{server.Url}/foo", content)
|
.PostAsync($"{server.Url}/foo", content)
|
||||||
;
|
;
|
||||||
|
|||||||
Reference in New Issue
Block a user