Skip to content

Commit

Permalink
Newtonsoft tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd committed Dec 5, 2023
1 parent 3ef55a3 commit c4dec2a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 49 deletions.
2 changes: 1 addition & 1 deletion test/Flurl.Test/Flurl.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NUnit" Version="3.13.3" />

<!--careful - 4.4 and above don't support net461-->
Expand All @@ -19,6 +18,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Flurl.Http.Newtonsoft\Flurl.Http.Newtonsoft.csproj" />
<ProjectReference Include="..\..\src\Flurl.Http\Flurl.Http.csproj" />
</ItemGroup>

Expand Down
8 changes: 5 additions & 3 deletions test/Flurl.Test/Http/HttpTestFixtureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ public abstract class HttpTestFixtureBase
protected HttpTest HttpTest { get; private set; }

[SetUp]
public void CreateHttpTest() {
HttpTest = new HttpTest();
public void SetUp() {
HttpTest = CreateHttpTest();
}

[TearDown]
public void DisposeHttpTest() {
public void TearTown() {
HttpTest.Dispose();
}

protected virtual HttpTest CreateHttpTest() => new HttpTest();
}
}
45 changes: 0 additions & 45 deletions test/Flurl.Test/Http/NewtonsoftJsonSerializer.cs

This file was deleted.

76 changes: 76 additions & 0 deletions test/Flurl.Test/Http/NewtonsoftTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using NUnit.Framework;
using Flurl.Http;
using Flurl.Http.Newtonsoft;
using Flurl.Http.Testing;
using System.Threading.Tasks;

namespace Flurl.Test.Http
{
// These inherit from GetTests and PostTests and swap out the JSON serializer
// in play, which gets us a lot of free tests but also a lot of redundant ones.
// Maybe worth refactoring someday, but they're fast so it's tolerable for now.

[TestFixture, Parallelizable]
public class NewtonsoftGetTests : GetTests
{
protected override HttpTest CreateHttpTest() => base.CreateHttpTest()
.WithSettings(settings => settings.JsonSerializer = new NewtonsoftJsonSerializer());

[Test]
public async Task can_get_dynamic() {
HttpTest.RespondWithJson(new { id = 1, name = "Frank" });

var data = await "http://some-api.com".GetJsonAsync();

Assert.AreEqual(1, data.id);
Assert.AreEqual("Frank", data.name);
}

[Test]
public async Task can_get_dynamic_list() {
HttpTest.RespondWithJson(new[] {
new { id = 1, name = "Frank" },
new { id = 2, name = "Claire" }
});

var data = await "http://some-api.com".GetJsonListAsync();

Assert.AreEqual(1, data[0].id);
Assert.AreEqual("Frank", data[0].name);
Assert.AreEqual(2, data[1].id);
Assert.AreEqual("Claire", data[1].name);
}
}

[TestFixture, Parallelizable]
public class NewtonsofPostTests : PostTests
{
protected override HttpTest CreateHttpTest() => base.CreateHttpTest()
.WithSettings(settings => settings.JsonSerializer = new NewtonsoftJsonSerializer());

[Test]
public async Task can_receive_json_dynamic() {
HttpTest.RespondWithJson(new { id = 1, name = "Frank" });

var data = await "http://some-api.com".PostJsonAsync(new { a = 1, b = 2 }).ReceiveJson();

Assert.AreEqual(1, data.id);
Assert.AreEqual("Frank", data.name);
}

[Test]
public async Task can_receive_json_dynamic_list() {
HttpTest.RespondWithJson(new[] {
new { id = 1, name = "Frank" },
new { id = 2, name = "Claire" }
});

var data = await "http://some-api.com".PostJsonAsync(new { a = 1, b = 2 }).ReceiveJsonList();

Assert.AreEqual(1, data[0].id);
Assert.AreEqual("Frank", data[0].name);
Assert.AreEqual(2, data[1].id);
Assert.AreEqual("Claire", data[1].name);
}
}
}

0 comments on commit c4dec2a

Please sign in to comment.