Skip to content

Commit

Permalink
refactor: prepare for removal of read and write from MocketSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
betaboon committed Nov 18, 2024
1 parent 0eff8f1 commit 90eb5db
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
10 changes: 7 additions & 3 deletions mocket/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,13 @@ def recv_into(
flags: int | None = None,
) -> int:
if hasattr(buffer, "write"):
return buffer.write(self.read(buffersize))
return buffer.write(self.recv(buffersize))

# buffer is a memoryview
data = self.read(buffersize)
if buffersize is None:
buffersize = len(buffer)

data = self.recv(buffersize)
if data:
buffer[: len(data)] = data
return len(data)
Expand All @@ -280,7 +284,7 @@ def recv(self, buffersize: int, flags: int | None = None) -> bytes:
r_fd, _ = Mocket.get_pair((self._host, self._port))
if r_fd:
return os.read(r_fd, buffersize)
data = self.read(buffersize)
data = self.io.read(buffersize)
if data:
return data
# used by Redis mock
Expand Down
12 changes: 6 additions & 6 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,12 @@ def test_sockets(self):
sock = socket.socket(address[0], address[1], address[2])

sock.connect(address[-1])
sock.write(f"{method} {path} HTTP/1.0\r\n")
sock.write(f"Host: {host}\r\n")
sock.write("Content-Type: application/json\r\n")
sock.write("Content-Length: %d\r\n" % len(data))
sock.write("Connection: close\r\n\r\n")
sock.write(data)
sock.send(f"{method} {path} HTTP/1.0\r\n".encode())
sock.send(f"Host: {host}\r\n".encode())
sock.send(b"Content-Type: application/json\r\n")
sock.send(b"Content-Length: %d\r\n" % len(data))
sock.send(b"Connection: close\r\n\r\n")
sock.send(data.encode())
sock.close()

# Proof that worked.
Expand Down

0 comments on commit 90eb5db

Please sign in to comment.