Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added GetJsonAsync tests with NewtonsoftJson
- Loading branch information
1 parent
812c344
commit 71c73f8
Showing
14 changed files
with
134 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 4 additions & 6 deletions
10
test/RestSharp.Tests.Serializers.Csv/RestSharp.Tests.Serializers.Csv.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
51 changes: 3 additions & 48 deletions
51
...s.Serializers.Json/NewtonsoftJsonTests.cs → ...n/NewtonsoftJson/IntegratedSimpleTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
test/RestSharp.Tests.Serializers.Json/NewtonsoftJson/IntegratedTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
test/RestSharp.Tests.Serializers.Json/NewtonsoftJson/SerializationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
); | ||
} | ||
|
||
} |
15 changes: 13 additions & 2 deletions
15
test/RestSharp.Tests.Serializers.Json/RestSharp.Tests.Serializers.Json.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters