Skip to content

Commit

Permalink
Add test case where socket connections and data transfer are intermixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ento committed Feb 5, 2024
1 parent 182be1a commit fcfc0d0
Showing 1 changed file with 30 additions and 0 deletions.
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 fcfc0d0

Please sign in to comment.