Skip to content

Commit e7e8172

Browse files
authored
Switching to using puremagic for identifying MIME types. (#255)
1 parent 13b4677 commit e7e8172

File tree

6 files changed

+18
-38
lines changed

6 files changed

+18
-38
lines changed

mocket/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
__all__ = ("async_mocketize", "mocketize", "Mocket", "MocketEntry", "Mocketizer")
55

6-
__version__ = "3.13.0"
6+
__version__ = "3.13.1"

mocket/compat.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import shlex
66
from typing import Final
77

8+
import puremagic
9+
810
ENCODING: Final[str] = os.getenv("MOCKET_ENCODING", "utf-8")
911

1012
text_type = str
@@ -29,12 +31,9 @@ def shsplit(s: str | bytes) -> list[str]:
2931
return shlex.split(s)
3032

3133

32-
def do_the_magic(lib_magic, body): # pragma: no cover
33-
if hasattr(lib_magic, "from_buffer"):
34-
# PyPI python-magic
35-
return lib_magic.from_buffer(body, mime=True)
36-
# file's builtin python wrapper
37-
# used by https://www.archlinux.org/packages/community/any/python-mocket/
38-
_magic = lib_magic.open(lib_magic.MAGIC_MIME_TYPE)
39-
_magic.load()
40-
return _magic.buffer(body)
34+
def do_the_magic(body):
35+
try:
36+
magic = puremagic.magic_string(body)
37+
except puremagic.PureError:
38+
magic = []
39+
return magic[0].mime_type if len(magic) else "application/octet-stream"

mocket/mockhttp.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
from .compat import ENCODING, decode_from_bytes, do_the_magic, encode_to_bytes
1111
from .mocket import Mocket, MocketEntry
1212

13-
try:
14-
import magic
15-
except ImportError:
16-
magic = None
17-
18-
1913
STATUS = {k: v[0] for k, v in BaseHTTPRequestHandler.responses.items()}
2014
CRLF = "\r\n"
2115
ASCII = "ascii"
@@ -76,10 +70,7 @@ class Response:
7670
headers = None
7771
is_file_object = False
7872

79-
def __init__(self, body="", status=200, headers=None, lib_magic=magic):
80-
# needed for testing libmagic import failure
81-
self.magic = lib_magic
82-
73+
def __init__(self, body="", status=200, headers=None):
8374
headers = headers or {}
8475
try:
8576
# File Objects
@@ -116,8 +107,8 @@ def set_base_headers(self):
116107
}
117108
if not self.is_file_object:
118109
self.headers["Content-Type"] = f"text/plain; charset={ENCODING}"
119-
elif self.magic:
120-
self.headers["Content-Type"] = do_the_magic(self.magic, self.body)
110+
else:
111+
self.headers["Content-Type"] = do_the_magic(self.body)
121112

122113
def set_extra_headers(self, headers):
123114
r"""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ classifiers = [
2727
"License :: OSI Approved :: BSD License",
2828
]
2929
dependencies = [
30-
"python-magic>=0.4.5",
30+
"puremagic",
3131
"decorator>=4.0.0",
3232
"urllib3>=1.25.3",
3333
"h11",

tests/test_compat.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from mocket.compat import do_the_magic
2+
3+
4+
def test_unknown_binary():
5+
assert do_the_magic(b"foobar-binary") == "application/octet-stream"

tests/test_http.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -258,21 +258,6 @@ def test_file_object(self):
258258
self.assertEqual(int(r.headers["Content-Length"]), len(local_content))
259259
self.assertEqual(r.headers["Content-Type"], "image/png")
260260

261-
@mocketize
262-
def test_file_object_with_no_lib_magic(self):
263-
url = "http://github.com/fluidicon.png"
264-
filename = "tests/fluidicon.png"
265-
with open(filename, "rb") as file_obj:
266-
Entry.register(Entry.GET, url, Response(body=file_obj, lib_magic=None))
267-
r = requests.get(url)
268-
remote_content = r.content
269-
with open(filename, "rb") as local_file_obj:
270-
local_content = local_file_obj.read()
271-
self.assertEqual(remote_content, local_content)
272-
self.assertEqual(len(remote_content), len(local_content))
273-
self.assertEqual(int(r.headers["Content-Length"]), len(local_content))
274-
self.assertNotIn("Content-Type", r.headers)
275-
276261
@mocketize
277262
def test_same_url_different_methods(self):
278263
url = "http://bit.ly/fakeurl"

0 commit comments

Comments
 (0)