Skip to content

Commit

Permalink
fix: avoid null exception when received body is empty but recorded bo…
Browse files Browse the repository at this point in the history
…dy isn't
  • Loading branch information
jhonny-apiiro committed Feb 2, 2024
1 parent 5d4227f commit 1cf90b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions EasyVCR.Tests/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<VCRException>(async () => await client.PostAsync(url, emptyContent), "No interaction found for request POST https://httpbin.org/post");

Check warning on line 535 in EasyVCR.Tests/ClientTest.cs

View workflow job for this annotation

GitHub Actions / Coverage_Requirements

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 535 in EasyVCR.Tests/ClientTest.cs

View workflow job for this annotation

GitHub Actions / NET_Tests (net462)

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 535 in EasyVCR.Tests/ClientTest.cs

View workflow job for this annotation

GitHub Actions / NET_Tests (netstandard2.0)

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 535 in EasyVCR.Tests/ClientTest.cs

View workflow job for this annotation

GitHub Actions / NET_Tests (netcoreapp3.1)

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 535 in EasyVCR.Tests/ClientTest.cs

View workflow job for this annotation

GitHub Actions / NET_Tests (net5.0)

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 535 in EasyVCR.Tests/ClientTest.cs

View workflow job for this annotation

GitHub Actions / NET_Tests (net6.0)

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 535 in EasyVCR.Tests/ClientTest.cs

View workflow job for this annotation

GitHub Actions / NET_Tests (net7.0)

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 535 in EasyVCR.Tests/ClientTest.cs

View workflow job for this annotation

GitHub Actions / NET_Tests (net8.0)

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
}



[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion EasyVCR/MatchRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public MatchRules ByBody(List<CensorElement>? 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;
}
Expand Down

0 comments on commit 1cf90b8

Please sign in to comment.