Widen except clauses and align MockHTTPResponse for fixture swap#22864
Open
mwdd146980 wants to merge 25 commits intomwdd146980/httpx-migration-basefrom
Open
Widen except clauses and align MockHTTPResponse for fixture swap#22864mwdd146980 wants to merge 25 commits intomwdd146980/httpx-migration-basefrom
mwdd146980 wants to merge 25 commits intomwdd146980/httpx-migration-basefrom
Conversation
Replace mock.patch('...requests.Session') + assert_called_with pattern
with the new http_client_session fixture. The full _request() option-merging
flow now runs, and the assertion verifies the timeout kwarg forwarded to
session.get().
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace mock.patch('...requests.Session') + assert_called_with pattern
with the new http_client_session fixture. Also replace third-party mock
import with unittest.mock throughout the file.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace mock.patch('...requests.Session') with the http_client_session
fixture in test__get_data (error-handling) and test_config (config
assertion).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace mock.patch('...requests.Session') patterns with the
http_client_session fixture in test_consul_request (error-handling)
and test_config (config assertion).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Extend _CanonicalMock with call_count, call_args, and call_args_list properties to support tests that introspect individual calls beyond assert_called_with. Then migrate test_get_models to use the fixture instead of patching requests.Session directly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…lient_session Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… and mock_http
- Remove http_client_session fixture, _CanonicalMock, and supporting code from
http_testing.py; update __all__ to ['MockHTTPResponse', 'mock_http']
- Config tests (couch, gitlab_runner, marathon, rabbitmq, consul, etcd,
ecs_fargate): drop http_client_session + check.check() + URL assertion;
assert check.http.options[key] == value directly — no HTTP call needed
- Functional tests (rabbitmq test__get_data, consul test_consul_request,
mesos_master test_can_connect_service_check, torchserve test_get_models):
replace http_client_session with mock_http
- mesos_master: set mock_http.options = {'verify': True} before check
instantiation since MesosMaster.__init__ reads self.http.options['verify']
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
create_autospec does not expose Protocol class-variable annotations at
runtime, so mock_http now explicitly sets client.options = MagicMock(spec=dict).
This provides a proper dict-like mock for any check that reads http.options
in __init__ (mesos_slave, mesos_master), removing the per-test workaround
of assigning mock_http.options = {'verify': True} before instantiation.
With options always present on the mock, the hasattr guard in
base_scraper.py is also removed.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…fixture mock_http was moved to datadog_checks_dev/plugin/pytest.py in PR #22710 and is auto-available to all tests via the plugin. Remove the duplicate definition from http_testing.py and drop the now-unnecessary noqa: F401 conftest imports. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Protocol annotation-only attributes (options: dict[str, Any]) are not included in dir() and thus not auto-mocked by create_autospec. Checks like mesos_master that access self.http.options in __init__ raised AttributeError when using the mock_http fixture. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
MagicMock does not preserve dict item assignment, so any check that writes to self.http.options in __init__ would silently discard the mutation when tested with mock_http. Use a real dict with the standard RequestsWrapper keys so item assignment and retrieval work correctly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…st_http_testing.py Match the dominant repo convention of top-level test functions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Both requests and httpx treat headers case-insensitively, so the API should too. Case folding happens inside the methods; options['headers'] remains a plain dict. set_header preserves the original key casing when overwriting an existing entry. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
MockHTTPResponse alignment (http_testing.py): - Add textwrap.dedent() to string content normalization - Add .ok and .reason properties Production except clause widening (only clauses exercised by mock_http_response error tests): - openmetrics/v2/base.py: add HTTPRequestError, HTTPStatusError - traefik_mesh/check.py: add HTTPStatusError, HTTPConnectionError Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ndling Add ok/reason properties to HTTPResponseProtocol to match production usage, widen traefik_mesh timeout handler with HTTPTimeoutError, add transitional comment to OM V2 except clause, and add tests for ok/reason on MockHTTPResponse. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This was referenced Mar 20, 2026
…rsion fix - Add test exercising dedent with indented multi-line content (item 2) - Add transitional comment to MockHTTPResponse.ok property (item 3) - Add tests for library-agnostic exception handling in _get_json (item 7) - Remove unused url parameter from traefik_mesh get_version (item 9) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
3 tasks
lavigne958
approved these changes
Mar 26, 2026
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.
Motivation
Before swapping the
mock_http_responsefixture fromMockResponse(requests-based) toMockHTTPResponse(library-agnostic), two things need to be true: MockHTTPResponse must match MockResponse behavior for properties tests rely on, and production except clauses must catch the library-agnostic exceptions raised byMockHTTPResponse.raise_for_status().Approach
Aligned MockHTTPResponse with MockResponse behavior, then widened the 2 production except clauses exercised by error-path tests: OM V2
base.pyandtraefik_mesh_get_json(). Addedok/reasontoHTTPResponseProtocolto match production usage. Individual integration except clauses will follow in a separate PR.See plan for more details.
Review follow-ups
Addressed agint-review feedback:
dedentwith indented multi-line contentMockHTTPResponse.ok_get_json()urlparameter fromtraefik_mesh.get_version()See triage doc for full rationale on addressed/dismissed items.
Verification