wkeeling / selenium-wire

Extends Selenium's Python bindings to give you the ability to inspect requests made by the browser.
MIT License
1.86k stars 240 forks source link

Error handling request in HAR with large timestamp in cookie #730

Open LightBit opened 7 months ago

LightBit commented 7 months ago

When cookie has expiration time set to 9999-12-31T23:59:59+00:00, I get exception OSError Invalid argument on Windows:

Error handling request
Traceback (most recent call last):
  File "C:\Python\Lib\site-packages\seleniumwire\handler.py", line 133, in response
    self.proxy.storage.save_har_entry(flow.request.id, har.create_har_entry(flow))
                                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\seleniumwire\har.py", line 85, in create_har_entry
    "cookies": _format_response_cookies(flow.response.cookies.fields),
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\seleniumwire\har.py", line 154, in _format_response_cookies
    return _format_cookies((c[0], c[1][0], c[1][1]) for c in fields)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\seleniumwire\har.py", line 142, in _format_cookies
    cookie_har["expires"] = datetime.fromtimestamp(expire_ts, timezone.utc).isoformat()
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument

datetime.fromtimestamp() on Windows does not work with large timestamps like 253402300799.

Workaround is to use timedelta:

expire = datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(seconds=expire_ts)
cookie_har["expires"] = expire.isoformat()