Repository: almarklein/asgineer

Issue:
Asset handler should not return a body for HEAD requests.

Discussion:

Patch:
diff --git a/asgineer/utils.py b/asgineer/utils.py
index fffe0fd..893d0e7 100644
--- a/asgineer/utils.py
+++ b/asgineer/utils.py
@@ -140,6 +140,11 @@ def make_asset_handler(assets, max_age=0, min_compress_size=256):
         else:
             body = assets[path]
 
+        # The response to a head request should not include a body
+        if request.method == "HEAD":
+            headers["content-length"] = str(len(body))
+            body = b""
+
         # Note that we always return bytes, not a stream-response. The
         # assets used with this utility are assumed to be small-ish,
         # since they are in-memory.


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 1409149..042fac3 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -54,7 +54,8 @@ def test_make_asset_handler():
     r2b = server.get("foo.png")
 
     assert r1a.status == 200 and r1b.status == 200
-    assert len(r1a.body) == 3 and len(r1a.body) == 3
+    assert len(r1a.body) == 3
+    assert len(r1b.body) == 0  # HEAD's have no body
 
     for r in (r1a, r1b):
         assert r.headers["content-type"] == "text/html"