requests icon indicating copy to clipboard operation
requests copied to clipboard

Fix MockResponse.getheaders() to return headers list instead of None

Open tboy1337 opened this issue 3 months ago • 0 comments

Summary

Fixes a bug in MockResponse.getheaders() method where it was not returning the headers list, resulting in None being returned instead.

Problem

The getheaders() method in the MockResponse class (used for cookie extraction) was missing a return statement, causing it to always return None instead of the actual headers list.

Solution

  • Added the missing return statement in src/requests/cookies.py line 121
  • Added comprehensive unit tests to verify the fix:
    • Test for successful header retrieval with multiple values
    • Test for empty list when no matching headers exist
    • Mock validation to ensure proper delegation to underlying HTTPMessage

Changes

  • Modified: src/requests/cookies.py - Added return statement to MockResponse.getheaders()
  • Added: Two unit tests in tests/test_requests.py:
    • test_mock_response_getheaders_returns_headers_list()
    • test_mock_response_getheaders_returns_empty_list()

Testing

All new tests pass and verify that:

  • The method correctly returns a list of headers when present
  • The method returns an empty list (not None) when no headers match
  • The underlying HTTPMessage.getheaders() is called with the correct arguments

tboy1337 avatar Oct 20 '25 12:10 tboy1337