Skip to content

Fix HttpResponseMessage disposal in mock test setups#643

Merged
baywet merged 2 commits into
feat/add-otel-http.routefrom
copilot/sub-pr-640-again
Feb 4, 2026
Merged

Fix HttpResponseMessage disposal in mock test setups#643
baywet merged 2 commits into
feat/add-otel-http.routefrom
copilot/sub-pr-640-again

Conversation

Copilot AI commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

Six test methods in HttpClientRequestAdapterObservabilityTests.cs were creating HttpResponseMessage objects in Moq setups without proper disposal, causing resource leaks.

Changed all instances from direct instantiation to lambda-wrapped factory:

- .ReturnsAsync(new HttpResponseMessage
+ .ReturnsAsync(() => new HttpResponseMessage
  {
      StatusCode = HttpStatusCode.OK,
      Content = new StringContent("{\"id\":\"123\"}")
  });

The lambda wrapper ensures Moq creates a fresh instance per invocation and handles disposal correctly.

Affected tests:

  • SendAsync_CreatesActivityWithUriTemplateTag
  • SendAsync_WithEmptyPath_SetsHttpRouteToRoot
  • SendAsync_SetsHttpRequestMethodTag
  • SendAsync_SetsUrlSchemeAndServerAddressTags
  • SendAsync_WithoutIncludeEUIIAttributes_DoesNotSetUrlFullTag
  • SendAsync_CreatesNestedActivitySpans

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Copilot AI changed the title [WIP] Update to address feedback on http.route attribute Fix HttpResponseMessage disposal in mock test setups Feb 4, 2026
Copilot AI requested a review from baywet February 4, 2026 13:45
@baywet baywet marked this pull request as ready for review February 4, 2026 13:48
@baywet baywet requested a review from a team as a code owner February 4, 2026 13:48
@baywet baywet merged commit 4acdcbe into feat/add-otel-http.route Feb 4, 2026
3 checks passed
@baywet baywet deleted the copilot/sub-pr-640-again branch February 4, 2026 13:48
@github-project-automation github-project-automation Bot moved this to Done ✔️ in Kiota Feb 4, 2026
@sonarqubecloud

sonarqubecloud Bot commented Feb 4, 2026

Copy link
Copy Markdown

gavinbarron added a commit that referenced this pull request Feb 6, 2026
* feat: add http.route attribute to open telemetry on requests

* expanded tests to include some coverage of existing otel instrumentation

* Potential fix for pull request finding 'Missing Dispose call on local IDisposable'

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update tests/http/httpClient/HttpClientRequestAdapterObservabilityTests.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix HttpResponseMessage disposal in Moq test setups (#645)

* Initial plan

* Fix HttpResponseMessage disposal issues in all observability tests

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

* Wrap HttpResponseMessage instantiation with lambda in Moq ReturnsAsync (#644)

* Initial plan

* Fix HttpResponseMessage disposal in all ReturnsAsync calls

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

* Fix HttpResponseMessage disposal in mock test setups (#642)

* Initial plan

* Fix HttpResponseMessage disposal in remaining test methods

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

* Fix HttpResponseMessage disposal in mock test setups (#643)

* Initial plan

* Fix HttpResponseMessage disposal issues in observability tests

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

* Fix mock response content-type in observability tests (#646)

* Initial plan

* Fix empty catch blocks by adding proper content-type to mock responses

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

* chore: seals the test class because it implements IDisposable

* Fix observability test setup to use correct content type (#647)

* Initial plan

* Fix empty catch blocks by adding explanatory comments

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

* Fix test setup to avoid exceptions instead of silencing them

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

* chore: removes try catch from tests

* Deduplicate baseUrlPlaceholder constant in GetNormalizedHttpRoute (#648)

* Initial plan

* Deduplicate baseUrlPlaceholder constant definition

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

* chore: fixes tests definitions

Signed-off-by: Vincent Biret <vibiret@microsoft.com>

* chore: formatting

Signed-off-by: Vincent Biret <vibiret@microsoft.com>

* Mark IDisposable test classes as sealed (#649)

* Initial plan

* refactor: mark IDisposable test classes as sealed

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* cleaned up tests to theories and fixed missing query string cases

---------

Signed-off-by: Vincent Biret <vibiret@microsoft.com>
Co-authored-by: Vincent Biret <vibiret@microsoft.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants