requests icon indicating copy to clipboard operation
requests copied to clipboard

Fix and optimize RequestsCookieJar.multiple_domains() method

Open tboy1337 opened this issue 3 months ago • 0 comments

Summary

Refactors the multiple_domains() method in RequestsCookieJar to fix a logic bug and improve performance by using a set for domain tracking instead of a list.

Changes

  • Fixed bug: The previous implementation incorrectly returned True when encountering duplicate domains. The method now correctly checks for multiple unique domains
  • Performance improvement: Changed from list to set for O(1) domain lookups instead of O(n)
  • Simplified logic: More readable implementation that counts unique domains and checks if count > 1
  • Comprehensive tests: Added 8 new unit tests covering various scenarios:
    • Empty jar
    • Single domain with multiple cookies
    • Multiple unique domains
    • Duplicate domains (should count as single)
    • Mixed duplicates and unique domains
    • Edge cases with empty string domains

Files Changed

  • src/requests/cookies.py: Refactored multiple_domains() method (4 lines modified)
  • tests/test_requests.py: Added 65 lines of comprehensive unit tests

Testing

All new tests pass and verify the correct behavior of the method across various domain scenarios.

Type of Change

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [x] Performance improvement
  • [x] Test coverage improvement

demonstrate_bug.py

tboy1337 avatar Oct 20 '25 12:10 tboy1337