diff --git a/test/Flurl.Test/Flurl.Test.csproj b/test/Flurl.Test/Flurl.Test.csproj index fbdc4a13..a11ac9f7 100644 --- a/test/Flurl.Test/Flurl.Test.csproj +++ b/test/Flurl.Test/Flurl.Test.csproj @@ -10,7 +10,6 @@ all - @@ -19,6 +18,7 @@ + diff --git a/test/Flurl.Test/Http/HttpTestFixtureBase.cs b/test/Flurl.Test/Http/HttpTestFixtureBase.cs index 5cadba8a..1050ea2d 100644 --- a/test/Flurl.Test/Http/HttpTestFixtureBase.cs +++ b/test/Flurl.Test/Http/HttpTestFixtureBase.cs @@ -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(); } } diff --git a/test/Flurl.Test/Http/NewtonsoftJsonSerializer.cs b/test/Flurl.Test/Http/NewtonsoftJsonSerializer.cs deleted file mode 100644 index d998ae4a..00000000 --- a/test/Flurl.Test/Http/NewtonsoftJsonSerializer.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System.IO; -using Newtonsoft.Json; - -namespace Flurl.Http.Configuration -{ - /// - /// ISerializer implementation based on Newtonsoft.Json. - /// Default serializer used in calls to GetJsonAsync, PostJsonAsync, etc. - /// - public class NewtonsoftJsonSerializer : ISerializer - { - private readonly JsonSerializerSettings _settings; - - /// - /// Initializes a new instance of the class. - /// - /// Settings to control (de)serialization behavior. - public NewtonsoftJsonSerializer(JsonSerializerSettings settings = null) { - _settings = settings; - } - - /// - /// Serializes the specified object to a JSON string. - /// - /// The object to serialize. - public string Serialize(object obj) => JsonConvert.SerializeObject(obj, _settings); - - /// - /// Deserializes the specified JSON string to an object of type T. - /// - /// The JSON string to deserialize. - public T Deserialize(string s) => JsonConvert.DeserializeObject(s, _settings); - - /// - /// Deserializes the specified stream to an object of type T. - /// - /// The stream to deserialize. - public T Deserialize(Stream stream) { - // https://www.newtonsoft.com/json/help/html/Performance.htm#MemoryUsage - using var sr = new StreamReader(stream); - using var jr = new JsonTextReader(sr); - return JsonSerializer.CreateDefault(_settings).Deserialize(jr); - } - } -} \ No newline at end of file diff --git a/test/Flurl.Test/Http/NewtonsoftTests.cs b/test/Flurl.Test/Http/NewtonsoftTests.cs new file mode 100644 index 00000000..6f01fa3f --- /dev/null +++ b/test/Flurl.Test/Http/NewtonsoftTests.cs @@ -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); + } + } +}