Skip to content

Commit

Permalink
Switching to using puremagic for identifying MIME types. (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
mindflayer authored Oct 19, 2024
1 parent 13b4677 commit e7e8172
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 38 deletions.
2 changes: 1 addition & 1 deletion mocket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

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

__version__ = "3.13.0"
__version__ = "3.13.1"
17 changes: 8 additions & 9 deletions mocket/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import shlex
from typing import Final

import puremagic

ENCODING: Final[str] = os.getenv("MOCKET_ENCODING", "utf-8")

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


def do_the_magic(lib_magic, body): # pragma: no cover
if hasattr(lib_magic, "from_buffer"):
# PyPI python-magic
return lib_magic.from_buffer(body, mime=True)
# file's builtin python wrapper
# used by https://www.archlinux.org/packages/community/any/python-mocket/
_magic = lib_magic.open(lib_magic.MAGIC_MIME_TYPE)
_magic.load()
return _magic.buffer(body)
def do_the_magic(body):
try:
magic = puremagic.magic_string(body)
except puremagic.PureError:
magic = []
return magic[0].mime_type if len(magic) else "application/octet-stream"
15 changes: 3 additions & 12 deletions mocket/mockhttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
from .compat import ENCODING, decode_from_bytes, do_the_magic, encode_to_bytes
from .mocket import Mocket, MocketEntry

try:
import magic
except ImportError:
magic = None


STATUS = {k: v[0] for k, v in BaseHTTPRequestHandler.responses.items()}
CRLF = "\r\n"
ASCII = "ascii"
Expand Down Expand Up @@ -76,10 +70,7 @@ class Response:
headers = None
is_file_object = False

def __init__(self, body="", status=200, headers=None, lib_magic=magic):
# needed for testing libmagic import failure
self.magic = lib_magic

def __init__(self, body="", status=200, headers=None):
headers = headers or {}
try:
# File Objects
Expand Down Expand Up @@ -116,8 +107,8 @@ def set_base_headers(self):
}
if not self.is_file_object:
self.headers["Content-Type"] = f"text/plain; charset={ENCODING}"
elif self.magic:
self.headers["Content-Type"] = do_the_magic(self.magic, self.body)
else:
self.headers["Content-Type"] = do_the_magic(self.body)

def set_extra_headers(self, headers):
r"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ classifiers = [
"License :: OSI Approved :: BSD License",
]
dependencies = [
"python-magic>=0.4.5",
"puremagic",
"decorator>=4.0.0",
"urllib3>=1.25.3",
"h11",
Expand Down
5 changes: 5 additions & 0 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from mocket.compat import do_the_magic


def test_unknown_binary():
assert do_the_magic(b"foobar-binary") == "application/octet-stream"
15 changes: 0 additions & 15 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,6 @@ def test_file_object(self):
self.assertEqual(int(r.headers["Content-Length"]), len(local_content))
self.assertEqual(r.headers["Content-Type"], "image/png")

@mocketize
def test_file_object_with_no_lib_magic(self):
url = "http://github.com/fluidicon.png"
filename = "tests/fluidicon.png"
with open(filename, "rb") as file_obj:
Entry.register(Entry.GET, url, Response(body=file_obj, lib_magic=None))
r = requests.get(url)
remote_content = r.content
with open(filename, "rb") as local_file_obj:
local_content = local_file_obj.read()
self.assertEqual(remote_content, local_content)
self.assertEqual(len(remote_content), len(local_content))
self.assertEqual(int(r.headers["Content-Length"]), len(local_content))
self.assertNotIn("Content-Type", r.headers)

@mocketize
def test_same_url_different_methods(self):
url = "http://bit.ly/fakeurl"
Expand Down

0 comments on commit e7e8172

Please sign in to comment.