Skip to content

Commit

Permalink
Add fix for issue with failing verification of empty array
Browse files Browse the repository at this point in the history
  • Loading branch information
basdijkstra committed Aug 22, 2024
1 parent c09837c commit 3f0462b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
63 changes: 61 additions & 2 deletions RestAssured.Net.Tests/ResponseBodyJsonVerificationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>
namespace RestAssured.Tests
{
using System;
using System.Collections.Generic;
using NUnit.Framework;
using RestAssured.Response;
Expand Down Expand Up @@ -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<ResponseVerificationException>(() =>
Expand All @@ -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}]"));
}

/// <summary>
Expand Down Expand Up @@ -324,6 +325,45 @@ public void JsonArrayResponseBodyElementCollectionCanBeVerifiedUsingNHamcrestMat
.Body("$[0:].text", NHamcrest.Has.Item(NHamcrest.Is.EqualTo("Read the newspaper")));
}

/// <summary>
/// A test demonstrating RestAssuredNet syntax for verifying
/// a JSON array response body element collection using an NHamcrest matcher.
/// </summary>
[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));
}

/// <summary>
/// A test demonstrating RestAssuredNet syntax for verifying
/// a JSON array response body element collection using an NHamcrest matcher.
/// </summary>
[Test]
public void JsonResponseBodyElementEmptyArrayValueMismatchThrowsExpectedException()
{
this.CreateStubForJsonEmptyArrayResponseBody();

var rve = Assert.Throws<ResponseVerificationException>(() =>
{
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 '[]'"));
}

/// <summary>
/// Creates the stub response for the JSON response body example.
/// </summary>
Expand Down Expand Up @@ -379,5 +419,24 @@ private void CreateStubForJsonResponseBodyWithNonMatchingContentTypeHeader()
.WithBodyAsJson(this.location)
.WithStatusCode(200));
}

/// <summary>
/// Creates the stub response for the JSON response body example with an element with
/// an empty array value.
/// </summary>
private void CreateStubForJsonEmptyArrayResponseBody()
{
var responseBody = new
{
success = true,
errors = Array.Empty<string>(),
};

this.Server?.Given(Request.Create().WithPath("/json-empty-array-response-body").UsingGet())
.RespondWith(Response.Create()
.WithHeader("Content-Type", "application/json")
.WithBodyAsJson(responseBody)
.WithStatusCode(200));
}
}
}
16 changes: 14 additions & 2 deletions RestAssured.Net/Response/VerifiableResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,21 @@ public VerifiableResponse Body<T>(string path, IMatcher<T> matcher, VerifyAs ver
this.FailVerification($"JsonPath expression '{path}' did not yield any results.");
}

if (!matcher.Matches(resultingElement!.ToObject<T>() !))
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<ICollection<T>>() !))
{
this.FailVerification($"Expected element selected by '{path}' to match '{matcher}' but was '{resultingElement}'");
}
}
else
{
if (!matcher.Matches(resultingElement!.ToObject<T>() !))
{
this.FailVerification($"Expected element selected by '{path}' to match '{matcher}' but was '{resultingElement}'");
}
}
}
else if (responseMediaType.Contains("xml"))
Expand Down

0 comments on commit 3f0462b

Please sign in to comment.