From 3f0462b99a22d739e7c2cff9ecbabf665624f02b Mon Sep 17 00:00:00 2001 From: basdijkstra Date: Thu, 22 Aug 2024 21:09:33 +0200 Subject: [PATCH] Add fix for issue with failing verification of empty array --- .../ResponseBodyJsonVerificationTests.cs | 63 ++++++++++++++++++- .../Response/VerifiableResponse.cs | 16 ++++- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/RestAssured.Net.Tests/ResponseBodyJsonVerificationTests.cs b/RestAssured.Net.Tests/ResponseBodyJsonVerificationTests.cs index 3f349c3..b063da9 100644 --- a/RestAssured.Net.Tests/ResponseBodyJsonVerificationTests.cs +++ b/RestAssured.Net.Tests/ResponseBodyJsonVerificationTests.cs @@ -15,6 +15,7 @@ // namespace RestAssured.Tests { + using System; using System.Collections.Generic; using NUnit.Framework; using RestAssured.Response; @@ -274,7 +275,7 @@ public void JsonResponseBodyElementCollectionNHamcrestMatcherMisMatchThrowsTheEx { this.CreateStubForJsonResponseBody(); - string placeNamesInLocation = this.placeName + ", " + this.location.Places[1].Name; + string placeNamesInLocation = $"{this.placeName}, {this.location.Places[1].Name}"; string mismatchCityName = Faker.Address.UkCounty(); var rve = Assert.Throws(() => @@ -287,7 +288,7 @@ public void JsonResponseBodyElementCollectionNHamcrestMatcherMisMatchThrowsTheEx .Body("$.Places[0:].Name", NHamcrest.Has.Item(NHamcrest.Is.EqualTo(mismatchCityName))); }); - Assert.That(rve?.Message, Is.EqualTo($"Expected elements selected by '$.Places[0:].Name' to match 'a collection containing \"" + mismatchCityName + "\"', but was [" + placeNamesInLocation + "]")); + Assert.That(rve?.Message, Is.EqualTo($"Expected elements selected by '$.Places[0:].Name' to match 'a collection containing \"{mismatchCityName}\"', but was [{placeNamesInLocation}]")); } /// @@ -324,6 +325,45 @@ public void JsonArrayResponseBodyElementCollectionCanBeVerifiedUsingNHamcrestMat .Body("$[0:].text", NHamcrest.Has.Item(NHamcrest.Is.EqualTo("Read the newspaper"))); } + /// + /// A test demonstrating RestAssuredNet syntax for verifying + /// a JSON array response body element collection using an NHamcrest matcher. + /// + [Test] + public void JsonResponseBodyElementEmptyArrayValueCanBeVerifiedUsingNHamcrestMatcher() + { + this.CreateStubForJsonEmptyArrayResponseBody(); + + Given() + .When() + .Get($"{MOCK_SERVER_BASE_URL}/json-empty-array-response-body") + .Then() + .StatusCode(200) + .Body("$.errors", NHamcrest.Is.OfLength(0)); + } + + /// + /// A test demonstrating RestAssuredNet syntax for verifying + /// a JSON array response body element collection using an NHamcrest matcher. + /// + [Test] + public void JsonResponseBodyElementEmptyArrayValueMismatchThrowsExpectedException() + { + this.CreateStubForJsonEmptyArrayResponseBody(); + + var rve = Assert.Throws(() => + { + Given() + .When() + .Get($"{MOCK_SERVER_BASE_URL}/json-empty-array-response-body") + .Then() + .StatusCode(200) + .Body("$.errors", NHamcrest.Is.OfLength(1)); + }); + + Assert.That(rve?.Message, Is.EqualTo($"Expected element selected by '$.errors' to match 'NHamcrest.Core.LengthMatcher`1[System.Collections.ICollection]' but was '[]'")); + } + /// /// Creates the stub response for the JSON response body example. /// @@ -379,5 +419,24 @@ private void CreateStubForJsonResponseBodyWithNonMatchingContentTypeHeader() .WithBodyAsJson(this.location) .WithStatusCode(200)); } + + /// + /// Creates the stub response for the JSON response body example with an element with + /// an empty array value. + /// + private void CreateStubForJsonEmptyArrayResponseBody() + { + var responseBody = new + { + success = true, + errors = Array.Empty(), + }; + + this.Server?.Given(Request.Create().WithPath("/json-empty-array-response-body").UsingGet()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson(responseBody) + .WithStatusCode(200)); + } } } \ No newline at end of file diff --git a/RestAssured.Net/Response/VerifiableResponse.cs b/RestAssured.Net/Response/VerifiableResponse.cs index 8ce0590..abc3dce 100644 --- a/RestAssured.Net/Response/VerifiableResponse.cs +++ b/RestAssured.Net/Response/VerifiableResponse.cs @@ -346,9 +346,21 @@ public VerifiableResponse Body(string path, IMatcher matcher, VerifyAs ver this.FailVerification($"JsonPath expression '{path}' did not yield any results."); } - if (!matcher.Matches(resultingElement!.ToObject() !)) + bool useCollectionMatcher = resultingElement!.GetType().Equals(typeof(JArray)); + + if (useCollectionMatcher) { - this.FailVerification($"Expected element selected by '{path}' to match '{matcher}' but was '{resultingElement}'"); + if (!matcher.Matches((T)resultingElement!.ToObject>() !)) + { + this.FailVerification($"Expected element selected by '{path}' to match '{matcher}' but was '{resultingElement}'"); + } + } + else + { + if (!matcher.Matches(resultingElement!.ToObject() !)) + { + this.FailVerification($"Expected element selected by '{path}' to match '{matcher}' but was '{resultingElement}'"); + } } } else if (responseMediaType.Contains("xml"))