HttpRecorder icon indicating copy to clipboard operation
HttpRecorder copied to clipboard

System.ObjectDisposedException: Cannot access a disposed object

Open GeorgopoulosGiannis opened this issue 1 year ago • 0 comments

First, thank you for this awesome library!

Expected Behavior

Expect to be able to use RulesMatcher.MatchMultiple without altering the system under test source code.

Actual Behavior

When using the RulesMatcher.MatchMultiple matcher, if the first interaction disposes the content of the response message, the next interaction will fail with System.ObjectDisposedException.

This means that code which uses httpClient.GetFromJsonAsync<T>() or using (var readStream = await httpResponseMessage.Content.ReadAsStreamAsync()) cannot be used to match multiple interactions. The code will fail with System.ObjectDisposedException in the second interaction.

Steps to Reproduce the Problem

  1. Create an HttpClient:
    HttpClient CreateClient(
        [CallerMemberName] string testName = "",
        [CallerFilePath] string filePath = "")
    {
        var interactionFilePath = Path.Join(
            Path.GetDirectoryName(filePath),
            $"{Path.GetFileNameWithoutExtension(filePath)}Fixtures",
            testName);
        {
            return new HttpClient(new HttpRecorderDelegatingHandler(interactionFilePath, HttpRecorderMode.Auto,
                matcher: RulesMatcher.MatchMultiple)
            {
                InnerHandler = new HttpClientHandler(),
            })
            {
                BaseAddress = new Uri("https://dog.ceo"),
            };
        }
    }
  1. Perform two consecutive calls two it which dispose the response message.
await httpClient.GetFromJsonAsync<ApiResult>("/api/breeds/image/random");

Specifications

  • Version: HttpRecorder 2.0.0
  • Platform: Microsoft Windows 11 Pro, 10.0.22621 Build 22621
  • Subsystem: ?

GeorgopoulosGiannis avatar Jan 08 '24 08:01 GeorgopoulosGiannis