Add mock HTTP response#22677
Open
mwdd146980 wants to merge 3 commits intomwdd146980/httpx-migration-basefrom
Open
Add mock HTTP response#22677mwdd146980 wants to merge 3 commits intomwdd146980/httpx-migration-basefrom
mwdd146980 wants to merge 3 commits intomwdd146980/httpx-migration-basefrom
Conversation
mwdd146980
added a commit
that referenced
this pull request
Feb 18, 2026
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a63b6ce889
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
This comment has been minimized.
This comment has been minimized.
Codecov Report❌ Patch coverage is Additional details and impacted files🚀 New features to boost your workflow:
|
mwdd146980
added a commit
that referenced
this pull request
Feb 18, 2026
Applied skill-based review to PR #22677 and made two improvements: 1. Fix protocol compliance: Update __exit__ signature - Changed from __exit__(self, *args: Any) -> None - To: __exit__(self, exc_type, exc_val, exc_tb) -> bool | None - Now correctly implements HTTPResponseProtocol specification 2. Add behavioral compatibility tests - Added TestMockHTTPResponseCompatibility class with 6 tests - Verifies MockHTTPResponse matches requests.Response behavior - Tests cover: iter_lines (bytes/strings/delimiters), iter_content, context manager, and raise_for_status Test results: 40/40 tests passing (34 existing + 6 new) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
mwdd146980
added a commit
that referenced
this pull request
Feb 18, 2026
Re-export HTTP exceptions and protocol types to enable library-agnostic HTTP client usage across datadog_checks_base and datadog_checks_dev. Changes: - datadog_checks_base/utils/http.py: Re-export HTTP exceptions for single import location. Add TYPE_CHECKING imports for protocol hints. Add docstring to RequestsWrapper indicating HTTPClientProtocol conformance. - datadog_checks_dev/http.py: Re-export MockHTTPResponse for protocol-based testing. Add __all__ to expose both MockResponse and MockHTTPResponse. This enables: from datadog_checks.base.utils.http import HTTPError, HTTPTimeoutError from datadog_checks.dev.http import MockHTTPResponse Part of Phase 1 httpx migration - Integration & Re-exports (Steps 4-5). Depends on PR #22677 (exceptions + testing utilities). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
3 tasks
mwdd146980
added a commit
that referenced
this pull request
Feb 18, 2026
Resolve protocol signature mismatch and getpeercert lambda issue that were introduced during branch merges. Changes: - http_protocol.py: Update iter_lines signature to accept str | bytes | None for delimiter parameter and return Iterator[bytes | str] to match PR #22677 and be compatible with MockHTTPResponse implementation - http_testing.py: Replace broken lambda with proper mock_getpeercert function that accepts self parameter to fix 'multiple values for argument' error These changes align PR #22680 with the working versions from PR #22677. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
mwdd146980
added a commit
that referenced
this pull request
Feb 19, 2026
Re-export HTTP exceptions and protocol types to enable library-agnostic HTTP client usage across datadog_checks_base and datadog_checks_dev. Changes: - datadog_checks_base/utils/http.py: Re-export HTTP exceptions for single import location. Add TYPE_CHECKING imports for protocol hints. Add docstring to RequestsWrapper indicating HTTPClientProtocol conformance. - datadog_checks_dev/http.py: Re-export MockHTTPResponse for protocol-based testing. Add __all__ to expose both MockResponse and MockHTTPResponse. This enables: from datadog_checks.base.utils.http import HTTPError, HTTPTimeoutError from datadog_checks.dev.http import MockHTTPResponse Part of Phase 1 httpx migration - Integration & Re-exports (Steps 4-5). Depends on PR #22677 (exceptions + testing utilities). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
mwdd146980
added a commit
that referenced
this pull request
Feb 19, 2026
Resolve protocol signature mismatch and getpeercert lambda issue that were introduced during branch merges. Changes: - http_protocol.py: Update iter_lines signature to accept str | bytes | None for delimiter parameter and return Iterator[bytes | str] to match PR #22677 and be compatible with MockHTTPResponse implementation - http_testing.py: Replace broken lambda with proper mock_getpeercert function that accepts self parameter to fix 'multiple values for argument' error These changes align PR #22680 with the working versions from PR #22677. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
MockHTTPResponse implements HTTPResponseProtocol without depending on requests or httpx. It supports the full response API (.json(), .text, .content, .status_code, .headers, .cookies, .elapsed), streaming via iter_content() and iter_lines(), raise_for_status() using HTTPStatusError, and context manager usage. Demonstrates usage by migrating test_authtoken.py from MockResponse to MockHTTPResponse — a drop-in replacement with protocol compliance. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
f0e141d to
af33176
Compare
…s_unit.py Also adds three missing API surface members discovered during migration: - encoding attribute (accessed by openmetrics mixin before iter_lines) - close() no-op (called by openmetrics mixin after response processing) - Removes _stream_consumed guard so the same instance can be reused across repeated mock.MagicMock(return_value=...) calls Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Adds
MockHTTPResponse— a library-agnostic mock HTTP response for use in integration tests — as the first concrete implementation on top of theHTTPResponseProtocoland exception hierarchy introduced in the feature branch.MockHTTPResponse(http_testing.py)Implements
HTTPResponseProtocolwithout depending on requests or httpx:.json(),.text,.content,.status_code,.headers,.cookies,.elapsed,.encodingiter_content()anditer_lines()with behavior matchingrequests.Responseraise_for_status()raisesHTTPStatusErrorfor 4xx/5xx responsesclose()is a no-op (matchesrequests.Response— content is already buffered in memory)Migration (
datadog_checks_base/tests/)All three files in
datadog_checks_base/tests/that usedMockResponsefromdatadog_checks.dev.httphave been migrated toMockHTTPResponse. The migration is a minimal import swap — call sites are identical:tests/base/utils/http/test_authtoken.pytests/base/utils/http/test_kerberos_unit.pytests/base/checks/openmetrics/test_legacy/test_openmetrics.pyThe openmetrics migration also validated two additional API surface requirements (
encodingattribute andclose()method) that were added toMockHTTPResponsein this PR.Test coverage (
test_http_testing.py)6 tests covering the logic we own:
json_dataauto-setsContent-Typeheaderraise_for_status()raises for 4xx and 5xxiter_lines()preserves empty lines but strips trailing delimiterMotivation
RFC
Review checklist (to be filled by reviewers)
qa/skip-qalabel if the PR doesn't need to be tested during QA.backport/<branch-name>label to the PR and it will automatically open a backport PR once this one is merged