Skip to content

Commit

Permalink
- Use shortcut for checking null bodies in matching
Browse files Browse the repository at this point in the history
  • Loading branch information
nwithan8 committed Feb 2, 2024
1 parent 183f0b7 commit 17aa458
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions EasyVCR/InternalUtilities/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EasyVCR.InternalUtilities
{
public static class Extensions
{
public static bool IsEmptyStringOrNull(this string? str) => str == null || string.IsNullOrWhiteSpace(str);
}
}
18 changes: 16 additions & 2 deletions EasyVCR/MatchRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EasyVCR.InternalUtilities;
using EasyVCR.RequestElements;
using JsonSerialization = EasyVCR.InternalUtilities.JSON.Serialization;
using XmlSerialization = EasyVCR.InternalUtilities.XML.Serialization;
Expand Down Expand Up @@ -132,7 +133,7 @@ public MatchRules ByBody(List<CensorElement>? ignoredElements = null)
return true;
if (received.Body == null || recorded.Body == null)
// one has a null body, so they don't match
// one has a null body, the other does not, so they don't match
return false;
var receivedBody = received.Body;
Expand All @@ -155,11 +156,24 @@ public MatchRules ByBody(List<CensorElement>? ignoredElements = null)
// not JSON, using the string as it is
}
if (receivedBody.IsEmptyStringOrNull())
// short-cut if the received body is empty
receivedBody = null;
if (recordedBody.IsEmptyStringOrNull())
// short-cut if the recorded body is empty
recordedBody = null;
if (receivedBody == null && recordedBody == null)
// both have empty string bodies, so they match
return true;
return (receivedBody ?? "").Equals(recordedBody, StringComparison.OrdinalIgnoreCase);
if (receivedBody == null || recordedBody == null)
// one has a null body, the other does not, so they don't match
return false;
// if the bodies are not null, then we can compare them
return receivedBody.Equals(recordedBody, StringComparison.OrdinalIgnoreCase);
});
return this;
}
Expand Down

0 comments on commit 17aa458

Please sign in to comment.