Skip to content

fix(http): handle null request, keep span open over call, and isolate options in HeadersInspectionHandler - #821

Open
Hardik K (hardikkaurani) wants to merge 2 commits into
microsoft:mainfrom
hardikkaurani:fix/headers-inspection-handler-lifecycle-options
Open

Hardik K (hardikkaurani) wants to merge 2 commits into
microsoft:mainfrom
hardikkaurani:fix/headers-inspection-handler-lifecycle-options

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Description

This PR brings HeadersInspectionHandler into full alignment with the middleware patterns and error handling across Kiota HTTP:

  1. Null Request Check:
    Validates if request is None: raise TypeError("request cannot be null") matching BodyInspectionHandler, preventing an unexpected AttributeError on invalid requests.
  2. Span Lifecycle & Timing:
    Keeps the observability span open over the downstream super().send(request, transport) call and ensures span.end() is invoked within a try ... finally block (mirroring the fix in UrlReplaceHandler and BodyInspectionHandler), ensuring correct timing and preventing dropped spans on transport failures.
  3. Per-Request Options Clearing:
    Clears captured request and response headers on each call on whichever option object is active (current_options), preventing headers from previous requests or retry attempts from accumulating when passing custom HeadersInspectionHandlerOption per request.
  4. Preserve Multi-Valued Headers:
    Uses request.headers.get_list(header) and response.headers.get_list(header) when feeding into HeadersCollection.add, accurately preserving discrete multi-valued headers (e.g. multiple cookies or accepts) without comma concatenation.

Testing

Added comprehensive unit tests in packages/http/httpx/tests/middleware_tests/test_headers_inspection_handler.py:

  • test_send_request_none_raises_type_error
  • test_headers_inspection_handler_request_options_isolation_and_clearing
  • test_headers_inspection_handler_multi_valued_headers
  • test_span_ends_on_transport_failure

cc Vincent Biret (@baywet)

@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner October 21, 2026 05:21
Copilot AI balanced review requested due to automatic review settings October 21, 2026 05:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet), I've created this PR to resolve the span lifecycle, per-request option isolation, multi-valued header preservation, and null checks for HeadersInspectionHandler, complete with comprehensive regression unit tests. Ready for your review whenever convenient!

@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants