Commit 83ccd900 authored by Yijie Deng's avatar Yijie Deng
Browse files

support for binary mode in mock

parent 36ec27af
......@@ -15,6 +15,7 @@ CURRENT_TESTS = {
"chapter1-exercise-file-urls-tests.md",
"chapter1-exercise-redirects-tests.md",
"chapter1-exercise-caching-tests.md",
"chapter1-binary-tests.md",
],
"chapter2": ["chapter2-base-tests.md",
"chapter2-exercise-line-breaks-tests.md",
......
......@@ -75,6 +75,16 @@ class socket:
assert len(self.body) == int(content_length), len(self.body)
def makefile(self, mode, encoding, newline):
"""
do not support 'w' mode; 'r' is a synonym of 'rt'
encoding and newline ignored for 'b' mode
implementation note:
copied some from https://github.com/python/cpython/blob/main/Lib/socket.py
"""
if not set(mode) <= {"r", "b"}:
raise ValueError("invalid mode %r (only r, b allowed)" % (mode,))
binary = "b" in mode # 't' or 'b'
assert self.connected and self.host and self.port, \
"You cannot call makefile() on a socket until you call connect() and send()"
if self.port == 80 and self.scheme == "http":
......@@ -95,6 +105,8 @@ class socket:
output = self.URLs[url][1]
if self.URLs[url][2]:
assert self.body == self.URLs[url][2], (self.body, self.URLs[url][2])
if binary:
return io.BytesIO(output)
return io.StringIO(output.decode(encoding).replace(newline, "\n"), newline)
def close(self):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment