Files
WireMock.Net-wiremock/test/WireMock.Net.Tests/Settings/WireMockServerSettingsJsonTests.cs
T

82 lines
3.6 KiB
C#

// Copyright © WireMock.Net
using JsonConverter.Newtonsoft.Json;
using JsonConverter.System.Text.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WireMock.Settings;
using WireMock.Transformers;
namespace WireMock.Net.Tests.Settings;
public class WireMockServerSettingsJsonTests
{
[Fact]
public void NewtonsoftJson_Serialize_WireMockServerSettings_HandlesSpecialProperties_AndKeepsOtherPropertiesAsIs()
{
// Arrange
var settings = new WireMockServerSettings
{
Port = 1234,
AdminPath = "/custom-admin",
StartAdminInterface = true,
DefaultJsonSerializer = new SystemTextJsonConverter()
};
settings.DefaultJsonBodyTransformer = new SystemTextJsonBodyTransformer(settings);
// Act
var json = JsonConvert.SerializeObject(settings);
var jsonObject = JObject.Parse(json);
// Assert
jsonObject[nameof(WireMockServerSettings.Port)]!.Value<int>().Should().Be(1234);
jsonObject[nameof(WireMockServerSettings.AdminPath)]!.Value<string>().Should().Be("/custom-admin");
jsonObject[nameof(WireMockServerSettings.StartAdminInterface)]!.Value<bool>().Should().BeTrue();
jsonObject[nameof(WireMockServerSettings.DefaultJsonSerializer)]!.Type.Should().Be(JTokenType.String);
jsonObject[nameof(WireMockServerSettings.DefaultJsonSerializer)]!.Value<string>().Should().Be(typeof(SystemTextJsonConverter).FullName);
jsonObject[nameof(WireMockServerSettings.DefaultJsonBodyTransformer)]!.Type.Should().Be(JTokenType.String);
jsonObject[nameof(WireMockServerSettings.DefaultJsonBodyTransformer)]!.Value<string>().Should().Be(typeof(SystemTextJsonBodyTransformer).FullName);
}
[Fact]
public void NewtonsoftJson_Deserialize_WireMockServerSettings_HandlesSpecialProperties_AndKeepsOtherPropertiesAsIs()
{
// Arrange
var json =
$"{{\"{nameof(WireMockServerSettings.Port)}\":4321," +
$"\"{nameof(WireMockServerSettings.AdminPath)}\":\"/custom-admin\"," +
$"\"{nameof(WireMockServerSettings.StartAdminInterface)}\":true," +
$"\"{nameof(WireMockServerSettings.DefaultJsonSerializer)}\":\"{typeof(SystemTextJsonConverter).FullName}\"," +
$"\"{nameof(WireMockServerSettings.DefaultJsonBodyTransformer)}\":\"{typeof(SystemTextJsonBodyTransformer).FullName}\"}}";
// Act
var settings = JsonConvert.DeserializeObject<WireMockServerSettings>(json);
// Assert
settings.Should().NotBeNull();
settings!.Port.Should().Be(4321);
settings.AdminPath.Should().Be("/custom-admin");
settings.StartAdminInterface.Should().BeTrue();
settings.DefaultJsonSerializer.Should().BeOfType<SystemTextJsonConverter>();
settings.DefaultJsonBodyTransformer.Should().BeOfType<SystemTextJsonBodyTransformer>();
}
[Fact]
public void NewtonsoftJson_Deserialize_WireMockServerSettings_LegacyObjects_AreIgnored_AndOtherPropertiesStayUntouched()
{
// Arrange
var json =
$"{{\"{nameof(WireMockServerSettings.Port)}\":9876," +
$"\"{nameof(WireMockServerSettings.DefaultJsonSerializer)}\":{{}}," +
$"\"{nameof(WireMockServerSettings.DefaultJsonBodyTransformer)}\":{{}}}}";
// Act
var settings = JsonConvert.DeserializeObject<WireMockServerSettings>(json);
// Assert
settings.Should().NotBeNull();
settings!.Port.Should().Be(9876);
settings.DefaultJsonSerializer.Should().BeOfType<NewtonsoftJsonConverter>();
settings.DefaultJsonBodyTransformer.Should().BeOfType<NewtonsoftJsonBodyTransformer>();
}
}