diff --git a/EasyVCR.Tests/ClientTest.cs b/EasyVCR.Tests/ClientTest.cs index 38a9a4c..7c29a53 100644 --- a/EasyVCR.Tests/ClientTest.cs +++ b/EasyVCR.Tests/ClientTest.cs @@ -512,6 +512,29 @@ public async Task TestMatchNonJsonBody() var response = await client.PostAsync(url, content); Assert.IsNotNull(response); } + + [TestMethod] + public async Task TestMatchEmptyStringBodyToNonEmptyStringBody() + { + var cassette = TestUtils.GetCassette("test_match_empty_body"); + cassette.Erase(); // Erase cassette before recording + + const string url = "https://httpbin.org/post"; + + // record baseline request first + var client = HttpClients.NewHttpClient(cassette, Mode.Record); + var someContent = new ByteArrayContent(Encoding.UTF8.GetBytes("whatevs")); + _ = await client.PostAsync(url, someContent); + + // try to replay the request with match by body enforcement + client = HttpClients.NewHttpClient(cassette, Mode.Replay, new AdvancedSettings + { + MatchRules = new MatchRules().ByBody() + }); + var emptyContent = new ByteArrayContent(Encoding.UTF8.GetBytes(string.Empty)); + Assert.ThrowsExceptionAsync(async () => await client.PostAsync(url, emptyContent), "No interaction found for request POST https://httpbin.org/post"); + } + [TestMethod] diff --git a/EasyVCR/MatchRules.cs b/EasyVCR/MatchRules.cs index 2984e4e..1e6288a 100644 --- a/EasyVCR/MatchRules.cs +++ b/EasyVCR/MatchRules.cs @@ -159,7 +159,7 @@ public MatchRules ByBody(List? ignoredElements = null) // both have empty string bodies, so they match return true; - return receivedBody!.Equals(recordedBody, StringComparison.OrdinalIgnoreCase); + return (receivedBody ?? "").Equals(recordedBody, StringComparison.OrdinalIgnoreCase); }); return this; }