Skip to content

Commit

Permalink
Added GetJsonAsync tests with NewtonsoftJson
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Apr 10, 2023
1 parent 812c344 commit 71c73f8
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 66 deletions.
1 change: 1 addition & 0 deletions RestSharp.sln.DotSettings
Expand Up @@ -80,6 +80,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=advisor/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Advisors/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Deserilization/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=formattable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=instagram/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Migrator/@EntryIndexedValue">True</s:Boolean>
Expand Down
2 changes: 1 addition & 1 deletion src/RestSharp/RestSharp.csproj
Expand Up @@ -37,6 +37,6 @@
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\gen\SourceGenerator\SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="$(RepoRoot)\gen\SourceGenerator\SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions test/Directory.Build.props
Expand Up @@ -5,6 +5,7 @@
<IsPackable>false</IsPackable>
<TargetFrameworks>net472;net6.0;net7.0</TargetFrameworks>
<Nullable>disable</Nullable>
<NoWarn>xUnit1033</NoWarn>
</PropertyGroup>

<ItemGroup Condition="$(IsTestProject) == 'true'">
Expand Down
Expand Up @@ -2,10 +2,10 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<IsTestProject>false</IsTestProject>
<TargetFrameworks>net6</TargetFrameworks>
<TargetFramework>net6</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj" />
<ProjectReference Include="$(RepoRoot)\src\RestSharp\RestSharp.csproj" />
<ProjectReference Include="..\RestSharp.Tests.Shared\RestSharp.Tests.Shared.csproj" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion test/RestSharp.Tests.Integrated/JsonBodyTests.cs
Expand Up @@ -4,7 +4,8 @@

namespace RestSharp.Tests.Integrated;

public class JsonBodyTests : IClassFixture<RequestBodyFixture> {
#pragma warning disable xUnit1033
public sealed class JsonBodyTests : IClassFixture<RequestBodyFixture> {
readonly SimpleServer _server;
readonly ITestOutputHelper _output;
readonly RestClient _client;
Expand Down
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\RestSharp.Serializers.Xml\RestSharp.Serializers.Xml.csproj" />
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj" />
<ProjectReference Include="$(RepoRoot)\src\RestSharp.Serializers.Xml\RestSharp.Serializers.Xml.csproj" />
<ProjectReference Include="$(RepoRoot)\src\RestSharp\RestSharp.csproj" />
<ProjectReference Include="..\RestSharp.Tests.Shared\RestSharp.Tests.Shared.csproj" />
</ItemGroup>
<ItemGroup>
Expand Down
@@ -1,8 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\src\RestSharp.Serializers.CsvHelper\RestSharp.Serializers.CsvHelper.csproj" />
<ProjectReference Include="..\RestSharp.Tests.Shared\RestSharp.Tests.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\RestSharp.Serializers.CsvHelper\RestSharp.Serializers.CsvHelper.csproj"/>
<ProjectReference Include="..\RestSharp.Tests.Shared\RestSharp.Tests.Shared.csproj"/>
</ItemGroup>
</Project>
@@ -1,62 +1,17 @@
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp.Serializers.NewtonsoftJson;
using RestSharp.Tests.Shared.Extensions;
using RestSharp.Tests.Shared.Fixtures;

namespace RestSharp.Tests.Serializers.Json;

public class NewtonsoftJsonTests {
static readonly Fixture Fixture = new();
namespace RestSharp.Tests.Serializers.Json.NewtonsoftJson;

public class IntegratedSimpleTests {
string _body;

readonly JsonSerializerSettings _jsonSerializerSettings = new() {
ContractResolver = new DefaultContractResolver {
NamingStrategy = new CamelCaseNamingStrategy()
},
Formatting = Formatting.None
};

void CaptureBody(HttpListenerRequest request, HttpListenerResponse response) => _body = request.InputStream.StreamToString();

[Fact]
public void Serialize_multiple_objects_within_one_thread() {
var serializer = new JsonNetSerializer();
var dummy1 = Fixture.Create<TestClass>();
var dummy2 = Fixture.Create<TestClass>();
var dummy3 = Fixture.Create<TestClass>();
var expectedSerialization1 = JsonConvert.SerializeObject(dummy1, _jsonSerializerSettings);
var expectedSerialization2 = JsonConvert.SerializeObject(dummy2, _jsonSerializerSettings);
var expectedSerialization3 = JsonConvert.SerializeObject(dummy3, _jsonSerializerSettings);

var actualSerialization1 = serializer.Serialize(dummy1);
var actualSerialization2 = serializer.Serialize(dummy2);
var actualSerialization3 = serializer.Serialize(dummy3);

actualSerialization1.Should().Be(expectedSerialization1);
actualSerialization2.Should().Be(expectedSerialization2);
actualSerialization3.Should().Be(expectedSerialization3);
}

[Fact]
public void Serialize_within_multiple_threads() {
var serializer = new JsonNetSerializer();

Parallel.For(
0,
100,
_ => {
var dummy = Fixture.Create<TestClass>();
var expectedSerialization = JsonConvert.SerializeObject(dummy, _jsonSerializerSettings);
var actualSerialization = serializer.Serialize(dummy);
actualSerialization.Should().Be(expectedSerialization);
}
);
}
static readonly Fixture Fixture = new();

[Fact]
public async Task Use_JsonNet_For_Requests() {
Expand Down
@@ -0,0 +1,47 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestMockCore;
using RestSharp.Serializers.NewtonsoftJson;

namespace RestSharp.Tests.Serializers.Json.NewtonsoftJson;

public class IntegratedTests {
static readonly Fixture Fixture = new();

const int Port = 5001;

[Fact]
public async Task Use_with_GetJsonAsync() {
var data = Fixture.Create<TestClass>();
var serialized = JsonConvert.SerializeObject(data, JsonNetSerializer.DefaultSettings);

using var server = new HttpServer(Port);
server.Config.Get("/test").Send(serialized);
server.Run();

using var client = new RestClient($"http://localhost:{Port}", configureSerialization: cfg => cfg.UseNewtonsoftJson());

var response = await client.GetJsonAsync<TestClass>("/test");

response.Should().BeEquivalentTo(data);
}

[Fact]
public async Task Use_with_GetJsonAsync_custom_settings() {
var settings = new JsonSerializerSettings {
ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() }
};
var data = Fixture.Create<TestClass>();
var serialized = JsonConvert.SerializeObject(data, settings);

using var server = new HttpServer(Port);
server.Config.Get("/test").Send(serialized);
server.Run();

using var client = new RestClient($"http://localhost:{Port}", configureSerialization: cfg => cfg.UseNewtonsoftJson(settings));

var response = await client.GetJsonAsync<TestClass>("/test");

response.Should().BeEquivalentTo(data);
}
}
@@ -0,0 +1,54 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp.Serializers.NewtonsoftJson;

namespace RestSharp.Tests.Serializers.Json.NewtonsoftJson;

public class SerializationTests {
static readonly Fixture Fixture = new();

readonly JsonSerializerSettings _jsonSerializerSettings = new() {
ContractResolver = new DefaultContractResolver {
NamingStrategy = new CamelCaseNamingStrategy()
},
Formatting = Formatting.None
};

[Fact]
public void Serialize_multiple_objects_within_one_thread() {
var serializer = new JsonNetSerializer();
var dummy1 = Fixture.Create<TestClass>();
var dummy2 = Fixture.Create<TestClass>();
var dummy3 = Fixture.Create<TestClass>();

var expectedSerialization1 = JsonConvert.SerializeObject(dummy1, _jsonSerializerSettings);
var expectedSerialization2 = JsonConvert.SerializeObject(dummy2, _jsonSerializerSettings);
var expectedSerialization3 = JsonConvert.SerializeObject(dummy3, _jsonSerializerSettings);

var actualSerialization1 = serializer.Serialize(dummy1);
var actualSerialization2 = serializer.Serialize(dummy2);
var actualSerialization3 = serializer.Serialize(dummy3);

actualSerialization1.Should().Be(expectedSerialization1);
actualSerialization2.Should().Be(expectedSerialization2);
actualSerialization3.Should().Be(expectedSerialization3);
}

[Fact]
public void Serialize_within_multiple_threads() {
var serializer = new JsonNetSerializer();

Parallel.For(
0,
100,
_ => {
var dummy = Fixture.Create<TestClass>();
var expectedSerialization = JsonConvert.SerializeObject(dummy, _jsonSerializerSettings);
var actualSerialization = serializer.Serialize(dummy);
actualSerialization.Should().Be(expectedSerialization);
}
);
}

}
@@ -1,7 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\src\RestSharp.Serializers.NewtonsoftJson\RestSharp.Serializers.NewtonsoftJson.csproj" />
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj" />
<ProjectReference Include="$(RepoRoot)\src\RestSharp.Serializers.NewtonsoftJson\RestSharp.Serializers.NewtonsoftJson.csproj" />
<ProjectReference Include="$(RepoRoot)\src\RestSharp\RestSharp.csproj" />
<ProjectReference Include="..\RestSharp.Tests.Shared\RestSharp.Tests.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Remove="NewtonsoftJson\IntegratedTests.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="rest-mock-core" Version="0.7.12" />
<Compile Include="NewtonsoftJson\IntegratedTests.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="rest-mock-core" Version="0.7.12" />
<Compile Include="NewtonsoftJson\IntegratedTests.cs" />
</ItemGroup>
</Project>
Expand Up @@ -4,7 +4,7 @@
using RestSharp.Tests.Shared.Extensions;
using RestSharp.Tests.Shared.Fixtures;

namespace RestSharp.Tests.Serializers.Json;
namespace RestSharp.Tests.Serializers.Json.SystemTextJson;

public class SystemTextJsonTests {
static readonly Fixture Fixture = new();
Expand Down
Expand Up @@ -3,8 +3,8 @@
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\RestSharp.Serializers.Xml\RestSharp.Serializers.Xml.csproj" />
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj" />
<ProjectReference Include="$(RepoRoot)\src\RestSharp.Serializers.Xml\RestSharp.Serializers.Xml.csproj" />
<ProjectReference Include="$(RepoRoot)\src\RestSharp\RestSharp.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="SampleData\boolean_from_number.xml" CopyToOutputDirectory="PreserveNewest" />
Expand Down
2 changes: 1 addition & 1 deletion test/RestSharp.Tests/RestSharp.Tests.csproj
Expand Up @@ -5,7 +5,7 @@
<PackageReference Include="System.Net.Http.Json" Version="7.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj" />
<ProjectReference Include="$(RepoRoot)\src\RestSharp\RestSharp.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="SampleData\4sq.json" CopyToOutputDirectory="PreserveNewest" />
Expand Down

0 comments on commit 71c73f8

Please sign in to comment.