Fix HTTPS httpx mocking in wrap_bio() plus regressions occurred#324
Conversation
wrap_bio() for the Python 3.10 build
There was a problem hiding this comment.
Pull request overview
This PR adjusts Mocket’s fake SSLContext.wrap_bio() behavior to better match how HTTPS clients (notably httpx on Python 3.10) provide/expect server_hostname, and adds a focused regression test to ensure the SSL object reflects the currently connected Mocket address.
Changes:
- Decode
server_hostnamewhen it is passed asbytes(UTF-8 with replacement) so internal host handling stays string-based. - When
server_hostnameisNone, derive the host/port (and_address) fromMocket._addressto preserve expected HTTPS behavior. - Add a parametrized test covering
bytes/str/None/empty/invalid-byte hostnames forwrap_bio().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/test_socket.py | Adds a regression test asserting wrap_bio() populates _host, _port, and _address consistently (including bytes hostname decoding). |
| mocket/ssl/context.py | Updates MocketSSLContext.wrap_bio() to normalize server_hostname and to use Mocket._address for host/port derivation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #324 +/- ##
==========================================
- Coverage 99.06% 98.09% -0.98%
==========================================
Files 22 22
Lines 1075 1207 +132
==========================================
+ Hits 1065 1184 +119
- Misses 10 23 +13 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| effective_host = hostname if hostname is not None else current_host | ||
| ssl_obj._host = effective_host | ||
| if current_port is not None: | ||
| ssl_obj._port = current_port | ||
| if effective_host is not None and current_port is not None: | ||
| ssl_obj._address = (effective_host, current_port) |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…indflayer/python-mocket into copilot/fix-failing-github-actions-job
wrap_bio() for the Python 3.10 buildwrap_bio() plus regressions occurred
|



What this MR fixes
This MR resolves async networking regressions introduced while fixing large-response behavior, and stabilizes HTTP/SSL mocking across
httpxandaiohttp.1) Large async response hang (
test_async_case_high_number)2)
aiohttpSSL failures (test_aiohttp)ServerDisconnectedError/ SSL read-loop instability by decoupling SSL reads from shared socket cursor state.MocketSSLSocketnow keeps a private pending SSL read buffer snapshot after writes, so protocol reads are deterministic across runtimes.SSLWantReadErrorbefore first write after handshake).3) Multi-socket regression during full test run
TypeError: 'NoneType' object cannot be interpreted as an integerinrecv()when called viarecv_into()withbuffersize=Noneand pipe-backed reads.recv()now normalizesNoneto the socket default buffer size.4) PyPy compatibility regression (
BufferError)BufferError: Existing exports of data: object cannot be re-sizedseen in true-send HTTP flows on PyPy.BytesIO.getbuffer()exports remained active while later writes resized the buffer.getbuffer()reads withgetvalue()-based logic in socket buffering paths.5) Coverage recovery for new logic
server_hostnamehandling in SSL bio wrapping,getpeercert()fallback toMocket._addresswhen host/port are unset.Result
make test).httpxhigh payload +aiohttpSSL flow) now pass consistently.mocket/socket.pyandmocket/ssl/socket.pyat 100%).228 passed, 3 xfailed(+tests/test_pook.pypassed).