mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-18 15:23:47 +01:00
* Version 2.x * Setup .NET 9 * 12 * cleanup some #if for NETSTANDARD1_3 * cleanup + fix tests for net8 * openapi * NO ConfigureAwait(false) + cleanup * . * #endif * HashSet * WireMock.Net.NUnit * HttpContext * Add WebSockets (#1423) * Add WebSockets * Add tests * fix * more tests * Add tests * ... * remove IOwin * - * tests * fluent * ok * match * . * byte[] * x * func * func * byte * trans * ... * frameworks......... * jmes * xxx * sc * using var httpClient = new HttpClient(); * usings * maxRetries * up * xunit v3 * ct * --- * ct * ct2 * T Unit * WireMock.Net.TUnitTests / 10 * t unit first * --project * no tunit * t2 * --project * --project * ci - --project * publish ./test/wiremock-coverage.xml * windows * . * log * ... * log * goed * BodyType * . * . * --scenario * ... * pact * ct * . * WireMock.Net.RestClient.AwesomeAssertions (#1427) * WireMock.Net.RestClient.AwesomeAssertions * ok * atpath * fix test * sonar fixes * ports * proxy test * FIX? * --- * await Task.Delay(100, _ct); * ? * --project * Aspire: use IDistributedApplicationEventingSubscriber (#1428) * broadcast * ok * more tsts * . * Collection * up * . * 2 * remove nfluent * <VersionPrefix>2.0.0-preview-02</VersionPrefix> * ... * . * nuget icon * . * <PackageReference Include="JmesPath.Net" Version="1.1.0" /> * x * 500 * . * fix some warnings * ws
196 lines
5.6 KiB
C#
196 lines
5.6 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using System.Linq.Dynamic.Core;
|
|
using System.Reflection;
|
|
using FluentAssertions;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using WireMock.Util;
|
|
using Xunit;
|
|
|
|
namespace WireMock.Net.Tests.Util;
|
|
|
|
public class JsonUtilsTests
|
|
{
|
|
[Fact]
|
|
public void JsonUtils_ParseJTokenToObject_For_String()
|
|
{
|
|
// Assign
|
|
object value = "test";
|
|
|
|
// Act
|
|
string result = JsonUtils.ParseJTokenToObject<string>(value);
|
|
|
|
// Assert
|
|
result.Should().Be("test");
|
|
}
|
|
|
|
[Fact]
|
|
public void JsonUtils_ParseJTokenToObject_For_Int()
|
|
{
|
|
// Assign
|
|
object value = 123;
|
|
|
|
// Act
|
|
var result = JsonUtils.ParseJTokenToObject<int>(value);
|
|
|
|
// Assert
|
|
result.Should().Be(123);
|
|
}
|
|
|
|
[Fact]
|
|
public void JsonUtils_ParseJTokenToObject_For_Invalid_Throws()
|
|
{
|
|
// Assign
|
|
object value = "{ }";
|
|
|
|
// Act
|
|
Action action = () => JsonUtils.ParseJTokenToObject<int>(value);
|
|
|
|
// Assert
|
|
action.Should().Throw<NotSupportedException>();
|
|
}
|
|
|
|
[Fact]
|
|
public void JsonUtils_GenerateDynamicLinqStatement_JToken()
|
|
{
|
|
// Assign
|
|
JToken instance = "Test";
|
|
|
|
// Act
|
|
string line = JsonUtils.GenerateDynamicLinqStatement(instance);
|
|
|
|
// Assert
|
|
var queryable = new[] { instance }.AsQueryable().Select(line);
|
|
bool result = queryable.Any("it == \"Test\"");
|
|
result.Should().BeTrue();
|
|
|
|
line.Should().Be("string(it)");
|
|
}
|
|
|
|
[Fact]
|
|
public void JsonUtils_GenerateDynamicLinqStatement_JArray_Indexer()
|
|
{
|
|
// Assign
|
|
var instance = new JObject
|
|
{
|
|
{ "Items", new JArray(new JValue(4), new JValue(8)) }
|
|
};
|
|
|
|
// Act
|
|
string line = JsonUtils.GenerateDynamicLinqStatement(instance);
|
|
|
|
// Assert 1
|
|
line.Should().Be("new ((new [] { long(Items[0]), long(Items[1])}) as Items)");
|
|
|
|
// Assert 2
|
|
var queryable = new[] { instance }.AsQueryable().Select(line);
|
|
bool result = queryable.Any("Items != null");
|
|
result.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void JsonUtils_GenerateDynamicLinqStatement_JObject2()
|
|
{
|
|
// Assign
|
|
var instance = new JObject
|
|
{
|
|
{"U", new JValue(new Uri("http://localhost:80/abc?a=5"))},
|
|
{"N", new JValue((object?) null)},
|
|
{"G", new JValue(Guid.NewGuid())},
|
|
{"Flt", new JValue(10.0f)},
|
|
{"Dbl", new JValue(Math.PI)},
|
|
{"Check", new JValue(true)},
|
|
{
|
|
"Child", new JObject
|
|
{
|
|
{"ChildId", new JValue(4)},
|
|
{"ChildDateTime", new JValue(new DateTime(2018, 2, 17))},
|
|
{"TS", new JValue(TimeSpan.FromMilliseconds(999))}
|
|
}
|
|
},
|
|
{"I", new JValue(9)},
|
|
{"L", new JValue(long.MaxValue)},
|
|
{"Name", new JValue("Test")}
|
|
};
|
|
|
|
// Act
|
|
string line = JsonUtils.GenerateDynamicLinqStatement(instance);
|
|
|
|
// Assert 1
|
|
line.Should().Be("new (Uri(U) as U, null as N, Guid(G) as G, double(Flt) as Flt, double(Dbl) as Dbl, bool(Check) as Check, new (long(Child.ChildId) as ChildId, DateTime(Child.ChildDateTime) as ChildDateTime, TimeSpan(Child.TS) as TS) as Child, long(I) as I, long(L) as L, string(Name) as Name)");
|
|
|
|
// Assert 2
|
|
var queryable = new[] { instance }.AsQueryable().Select(line);
|
|
bool result = queryable.Any("I > 1 && L > 1");
|
|
result.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void JsonUtils_GenerateDynamicLinqStatement_Throws()
|
|
{
|
|
// Assign
|
|
var instance = new JObject
|
|
{
|
|
{ "B", new JValue(new byte[] {48, 49}) }
|
|
};
|
|
|
|
// Act and Assert
|
|
Check.ThatCode(() => JsonUtils.GenerateDynamicLinqStatement(instance)).Throws<NotSupportedException>();
|
|
}
|
|
|
|
[Fact]
|
|
public void JsonUtils_CreateTypeFromJObject()
|
|
{
|
|
// Assign
|
|
var instance = new JObject
|
|
{
|
|
{"U", new JValue(new Uri("http://localhost:80/abc?a=5"))},
|
|
{"N", new JValue((object?) null)},
|
|
{"G", new JValue(Guid.NewGuid())},
|
|
{"Flt", new JValue(10.0f)},
|
|
{"Dbl", new JValue(Math.PI)},
|
|
{"Check", new JValue(true)},
|
|
{
|
|
"Child", new JObject
|
|
{
|
|
{"ChildId", new JValue(4)},
|
|
{"ChildDateTime", new JValue(new DateTime(2018, 2, 17))},
|
|
{"ChildTimeSpan", new JValue(TimeSpan.FromMilliseconds(999))}
|
|
}
|
|
},
|
|
{"I", new JValue(9)},
|
|
{"L", new JValue(long.MaxValue)},
|
|
{"S", new JValue("Test")},
|
|
{"C", new JValue('c')}
|
|
};
|
|
|
|
// Act
|
|
var type = JsonUtils.CreateTypeFromJObject(instance);
|
|
|
|
// Assert
|
|
var setProperties = type
|
|
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
|
.Where(pi => pi.GetMethod != null).Select(pi => $"{pi.GetMethod}")
|
|
.ToArray();
|
|
|
|
setProperties.Should().HaveCount(11);
|
|
setProperties.Should().BeEquivalentTo(new[]
|
|
{
|
|
"System.String get_U()",
|
|
"System.Object get_N()",
|
|
"System.Guid get_G()",
|
|
"Single get_Flt()",
|
|
"Single get_Dbl()",
|
|
"Boolean get_Check()",
|
|
"Child get_Child()",
|
|
"Int64 get_I()",
|
|
"Int64 get_L()",
|
|
"System.String get_S()",
|
|
"System.String get_C()"
|
|
});
|
|
}
|
|
}
|