Skip to content

Commit

Permalink
Merge pull request #222 from ento/true-socket-host-and-port
Browse files Browse the repository at this point in the history
When creating true socket connection, use host and port stored in instance variables, not in the Mocket class's variable
  • Loading branch information
mindflayer authored Feb 5, 2024
2 parents 182be1a + 0fce044 commit f497448
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mocket/mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def true_sendall(self, data, *args, **kwargs):
encoded_response = hexload(response_dict["response"])
# if not available, call the real sendall
except KeyError:
host, port = Mocket._address
host, port = self._host, self._port
host = true_gethostbyname(host)

if isinstance(self.true_socket, true_socket) and self._secure_socket:
Expand Down
30 changes: 30 additions & 0 deletions tests/main/test_mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,36 @@ def test_makefile(self):
self.assertEqual(fp.read().strip(), encode_to_bytes("Show me."))
self.assertEqual(len(Mocket.request_list()), 1)

@pytest.mark.skipif(
'os.getenv("SKIP_TRUE_HTTP", False) or os.getenv("SKIP_TRUE_REDIS", False)'
)
def test_multiple_socket_connections(self):
redis_addr = ("localhost", 6379)
httpbin_addr = ("localhost", 80)

redis_buffer = io.BytesIO()
httpbin_buffer = io.BytesIO()

with Mocketizer():
redis_so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
redis_so.connect(redis_addr)

httpbin_so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
httpbin_so.connect(httpbin_addr)

# Creating another socket that connects to a different address
# should not cause the first connection to go awry.
redis_so.sendall(b"ping\r\n")
redis_so.recv_into(redis_buffer)

httpbin_so.sendall(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")
httpbin_so.recv_into(httpbin_buffer)

redis_buffer.seek(0)
assert redis_buffer.read() == b"+PONG\r\n"
httpbin_buffer.seek(0)
assert httpbin_buffer.read().startswith(b"HTTP/1.1 200 OK\r\n")

def test_socket_as_context_manager(self):
addr = ("localhost", 80)
Mocket.register(MocketEntry(addr, ["Show me.\r\n"]))
Expand Down

0 comments on commit f497448

Please sign in to comment.