mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-18 08:07:09 +01:00
0.8.0
This commit is contained in:
@@ -32,7 +32,7 @@ public class ResponseModel
|
||||
public object? BodyAsJson { get; set; }
|
||||
|
||||
/// <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>
|
||||
public bool? BodyAsJsonIndented { get; set; }
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ public interface IBodyData
|
||||
object? BodyAsJson { get; set; }
|
||||
|
||||
/// <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>
|
||||
bool? BodyAsJsonIndented { get; set; }
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<Description>WireMock.Net.Routing extends WireMock.Net with modern, minimal-API-style routing for .NET</Description>
|
||||
<Authors>Gennadii Saltyshchak</Authors>
|
||||
@@ -25,7 +25,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.7.0" />
|
||||
<PackageReference Include="JsonConverter.Abstractions" Version="0.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Matchers.Request;
|
||||
@@ -164,8 +163,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);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using JsonConverter.Abstractions;
|
||||
using JsonConverter.Newtonsoft.Json;
|
||||
using Stef.Validation;
|
||||
using WireMock.Settings;
|
||||
|
||||
@@ -12,12 +13,15 @@ internal class MappingToFileSaver
|
||||
{
|
||||
private readonly WireMockServerSettings _settings;
|
||||
private readonly MappingConverter _mappingConverter;
|
||||
private readonly IJsonConverter _jsonConverter;
|
||||
private readonly MappingFileNameSanitizer _fileNameSanitizer;
|
||||
|
||||
public MappingToFileSaver(WireMockServerSettings settings, MappingConverter mappingConverter)
|
||||
{
|
||||
_settings = Guard.NotNull(settings);
|
||||
_mappingConverter = Guard.NotNull(mappingConverter);
|
||||
|
||||
_jsonConverter = settings.DefaultJsonSerializer ?? new NewtonsoftJsonConverter();
|
||||
_fileNameSanitizer = new MappingFileNameSanitizer(settings);
|
||||
}
|
||||
|
||||
@@ -56,6 +60,8 @@ internal class MappingToFileSaver
|
||||
{
|
||||
_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);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using JetBrains.Annotations;
|
||||
using Newtonsoft.Json;
|
||||
using JsonConverter.Abstractions;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
@@ -826,25 +826,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
|
||||
{
|
||||
BodyData = new BodyData
|
||||
{
|
||||
DetectedBodyType = BodyType.String,
|
||||
BodyAsString = JsonConvert.SerializeObject(result, keepNullValues ? JsonSerializationConstants.JsonSerializerSettingsIncludeNullValues : JsonSerializationConstants.JsonSerializerSettingsDefault)
|
||||
BodyAsString = _settings.DefaultJsonSerializer.Serialize(result!, jsonOptions)
|
||||
},
|
||||
StatusCode = statusCode ?? (int)HttpStatusCode.OK,
|
||||
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)
|
||||
{
|
||||
return new ResponseMessage
|
||||
|
||||
@@ -8,7 +8,6 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Runtime;
|
||||
using System.Threading;
|
||||
using AnyOfTypes;
|
||||
using JetBrains.Annotations;
|
||||
@@ -360,7 +359,7 @@ public partial class WireMockServer : IWireMockServer
|
||||
{
|
||||
_settings = Guard.NotNull(settings);
|
||||
|
||||
_mappingSerializer = new MappingSerializer(settings.MappingJsonSerializer ?? new NewtonsoftJsonConverter());
|
||||
_mappingSerializer = new MappingSerializer(settings.DefaultJsonSerializer ?? new NewtonsoftJsonConverter());
|
||||
|
||||
// Set default values if not provided
|
||||
_settings.Logger = settings.Logger ?? new WireMockNullLogger();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<Description>Minimal version from the lightweight Http Mocking Server for .NET</Description>
|
||||
<AssemblyTitle>WireMock.Net.Minimal</AssemblyTitle>
|
||||
@@ -57,8 +57,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!--<PackageReference Include="JsonConverter.Abstractions" Version="0.7.0" />-->
|
||||
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.7.0" />
|
||||
<!--<PackageReference Include="JsonConverter.Abstractions" Version="0.8.0" />-->
|
||||
<!--<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.8.0" />-->
|
||||
<PackageReference Include="NJsonSchema.Extensions" Version="0.1.0" />
|
||||
<PackageReference Include="NSwag.Core" Version="13.16.1" />
|
||||
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
|
||||
@@ -118,13 +118,13 @@
|
||||
|
||||
<!-- https://github.com/wiremock/WireMock.Net/issues/507 -->
|
||||
<PackageReference Include="Microsoft.AspNetCore.Server.IIS" Version="2.2.6" />
|
||||
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.7.0" />
|
||||
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' or '$(TargetFramework)' == 'net5.0' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0'">
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
<PackageReference Include="Scriban.Signed" Version="5.5.0" />
|
||||
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.7.0" />
|
||||
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.7.0" />
|
||||
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.8.0" />
|
||||
<PackageReference Include="RestEase" Version="1.6.4" />
|
||||
<PackageReference Include="Stef.Validation" Version="0.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using JsonConverter.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
@@ -7,24 +8,30 @@ namespace WireMock.Serialization;
|
||||
|
||||
internal static class JsonSerializationConstants
|
||||
{
|
||||
public static readonly JsonSerializerSettings JsonSerializerSettingsDefault = new()
|
||||
internal static readonly JsonConverterOptions JsonConverterOptionsDefault = new()
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
WriteIndented = true,
|
||||
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,
|
||||
NullValueHandling = NullValueHandling.Include
|
||||
};
|
||||
|
||||
public static readonly JsonSerializerSettings JsonDeserializerSettingsWithDateParsingNone = new()
|
||||
internal static readonly JsonSerializerSettings JsonDeserializerSettingsWithDateParsingNone = new()
|
||||
{
|
||||
DateParseHandling = DateParseHandling.None
|
||||
};
|
||||
|
||||
public static readonly JsonSerializerSettings JsonSerializerSettingsPact = new()
|
||||
internal static readonly JsonSerializerSettings JsonSerializerSettingsPact = new()
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
|
||||
@@ -15,6 +15,7 @@ using WireMock.Types;
|
||||
using System.Globalization;
|
||||
using WireMock.Models;
|
||||
using JsonConverter.Abstractions;
|
||||
using JsonConverter.Newtonsoft.Json;
|
||||
|
||||
#if USE_ASPNETCORE
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -341,12 +342,12 @@ public class WireMockServerSettings
|
||||
public HandlebarsSettings? HandlebarsSettings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the JSON converter used for MappingModel serialization.
|
||||
/// 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.
|
||||
/// If not set, the NewtonsoftJsonConverter will be used.
|
||||
/// Default is <see cref="NewtonsoftJsonConverter"/>.
|
||||
/// </remarks>
|
||||
[PublicAPI]
|
||||
public IJsonConverter? MappingJsonSerializer { get; set; }
|
||||
public IJsonConverter DefaultJsonSerializer { get; set; } = new NewtonsoftJsonConverter();
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<Description>Shared interfaces, models, enumerations and types.</Description>
|
||||
<Authors>Stef Heyenrath</Authors>
|
||||
@@ -48,9 +48,10 @@
|
||||
</PackageReference>
|
||||
<PackageReference Include="Stef.Validation" Version="0.1.1" />
|
||||
<PackageReference Include="AnyOf" Version="0.4.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<!--<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />-->
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="1.1.0" />
|
||||
<PackageReference Include="JsonConverter.Abstractions" Version="0.7.0" />
|
||||
<!--<PackageReference Include="JsonConverter.Abstractions" Version="0.8.0" />-->
|
||||
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -4,6 +4,7 @@ using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using JsonConverter.Newtonsoft.Json;
|
||||
using Moq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NFluent;
|
||||
@@ -316,12 +317,11 @@ public class ResponseWithBodyTests
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request1, _settings).ConfigureAwait(false);
|
||||
|
||||
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]
|
||||
public async Task Response_ProvideResponse_WithBody_IJsonConverter_SystemTextJson()
|
||||
public async Task Response_ProvideResponse_WithBody_NewtonsoftJsonConverter()
|
||||
{
|
||||
// Arrange
|
||||
var requestBody = new BodyData
|
||||
@@ -331,13 +331,34 @@ public class ResponseWithBodyTests
|
||||
};
|
||||
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, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsString.Should().Be(@"{""foo"":""bar"",""n"":42}");
|
||||
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
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsString.Should().Be("""{"foo":"\u003C \u003E \u0026 \u0027 \uD83D\uDE00 \uD83D\uDC4D \u2764\uFE0F","n":42}""");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<Authors>Stef Heyenrath</Authors>
|
||||
@@ -103,7 +103,7 @@
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' != 'net452'">
|
||||
<PackageReference Include="System.Net.Http.Json" Version="8.0.1" />
|
||||
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.7.0" />
|
||||
<!--<PackageReference Include="JsonConverter.System.Text.Json" Version="0.8.0" />-->
|
||||
<PackageReference Include="Google.Protobuf" Version="3.25.1" />
|
||||
<PackageReference Include="Grpc.Net.Client" Version="2.60.0" />
|
||||
<PackageReference Include="Grpc.Tools" Version="2.60.0">
|
||||
|
||||
Reference in New Issue
Block a user