make linter happy

This commit is contained in:
Daniel Bauer
2024-11-25 15:40:12 +01:00
parent 69862a4134
commit 9f1b7cb59f
2 changed files with 21 additions and 32 deletions

View File

@@ -41,6 +41,7 @@ class SignpostingMiddleware:
response["Link"] = " , ".join(link_snippets) response["Link"] = " , ".join(link_snippets)
class HtmlSignpostingMiddleware(SignpostingMiddleware): class HtmlSignpostingMiddleware(SignpostingMiddleware):
def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]): def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]):
self.get_response = get_response self.get_response = get_response
@@ -67,7 +68,6 @@ class HtmlSignpostingMiddleware(SignpostingMiddleware):
head_tag = soup.new_tag("head") head_tag = soup.new_tag("head")
soup.html.insert(0, head_tag) soup.html.insert(0, head_tag)
# BUild links for each signpost and add them to the html # BUild links for each signpost and add them to the html
for signpost in response._signposts: for signpost in response._signposts:
link_tag = soup.new_tag("link") link_tag = soup.new_tag("link")
@@ -79,13 +79,12 @@ class HtmlSignpostingMiddleware(SignpostingMiddleware):
# Override the original content with the new HTML # Override the original content with the new HTML
response.content = soup.prettify().encode("utf-8") response.content = soup.prettify().encode("utf-8")
response['Content-Length'] = len(response.content) response["Content-Length"] = len(response.content)
return response return response
class JsonLdSignpostingParserMiddleware(MiddlewareMixin): class JsonLdSignpostingParserMiddleware(MiddlewareMixin):
def process_response( def process_response(
self, request: HttpRequest, response: HttpResponse self, request: HttpRequest, response: HttpResponse
) -> HttpResponse: ) -> HttpResponse:

View File

@@ -4,11 +4,11 @@ from django_signposting.middleware import HtmlSignpostingMiddleware
from signposting import LinkRel, Signpost from signposting import LinkRel, Signpost
import pytest import pytest
def assert_links_exist(response: HttpResponse, signposts: list[Signpost] = None): def assert_links_exist(response: HttpResponse, signposts: list[Signpost] = None):
if signposts is None: if signposts is None:
signposts = response._signposts signposts = response._signposts
soup = BeautifulSoup(response.content.decode("utf-8"), "html.parser") soup = BeautifulSoup(response.content.decode("utf-8"), "html.parser")
for signpost in signposts: for signpost in signposts:
links = soup.find_all("link", href=signpost.target, rel=signpost.rel) links = soup.find_all("link", href=signpost.target, rel=signpost.rel)
@@ -16,6 +16,7 @@ def assert_links_exist(response: HttpResponse, signposts: list[Signpost]=None):
assert len(soup.find_all("link")) == len(signposts) assert len(soup.find_all("link")) == len(signposts)
def test_middleware_no_signposting(): def test_middleware_no_signposting():
response = HttpResponse("<html><head></head><body></body></html>") response = HttpResponse("<html><head></head><body></body></html>")
response.status_code = 200 response.status_code = 200
@@ -29,48 +30,44 @@ def test_middleware_no_signposting():
def test_middleware_no_html(): def test_middleware_no_html():
response = JsonResponse({"hello": "world"}) response = JsonResponse({"hello": "world"})
response.status_code = 200 response.status_code = 200
response._signpost = [ response._signpost = [Signpost(LinkRel.author, "http://example.com")]
Signpost(LinkRel.author, "http://example.com")
]
middleware = HtmlSignpostingMiddleware(lambda request: response) middleware = HtmlSignpostingMiddleware(lambda request: response)
middleware(None) middleware(None)
assert_links_exist(response, []) assert_links_exist(response, [])
def test_middleware_malformed_html(): def test_middleware_malformed_html():
response = HttpResponse("Hello world") response = HttpResponse("Hello world")
response.status_code = 200 response.status_code = 200
response.content_type = "text/html" response.content_type = "text/html"
response._signposts = [ response._signposts = [Signpost(LinkRel.author, "http://example.com")]
Signpost(LinkRel.author, "http://example.com")
]
middleware = HtmlSignpostingMiddleware(lambda request: response) middleware = HtmlSignpostingMiddleware(lambda request: response)
with pytest.raises(Exception): with pytest.raises(Exception):
middleware(None) middleware(None)
def test_middleware_signposting_without_head(): def test_middleware_signposting_without_head():
response = HttpResponse("<html><body></body></html>") response = HttpResponse("<html><body></body></html>")
response.status_code = 200 response.status_code = 200
response._signposts = [ response._signposts = [Signpost(LinkRel.author, "http://example.com")]
Signpost(LinkRel.author, "http://example.com")
]
middleware = HtmlSignpostingMiddleware(lambda request: response) middleware = HtmlSignpostingMiddleware(lambda request: response)
middleware(None) middleware(None)
assert_links_exist(response) assert_links_exist(response)
def test_middleware_signposting(): def test_middleware_signposting():
response = HttpResponse("<html><head></head><body></body></html>") response = HttpResponse("<html><head></head><body></body></html>")
response.status_code = 200 response.status_code = 200
response._signposts = [ response._signposts = [Signpost(LinkRel.author, "http://example.com")]
Signpost(LinkRel.author, "http://example.com")
]
middleware = HtmlSignpostingMiddleware(lambda request: response) middleware = HtmlSignpostingMiddleware(lambda request: response)
middleware(None) middleware(None)
assert_links_exist(response) assert_links_exist(response)
def test_middleware_multiple_signposts(): def test_middleware_multiple_signposts():
response = HttpResponse("<html><head></head><body></body></html>") response = HttpResponse("<html><head></head><body></body></html>")
response.status_code = 200 response.status_code = 200
@@ -88,9 +85,7 @@ def test_middleware_multiple_signposts():
def test_middleware_signpost_with_content_type(): def test_middleware_signpost_with_content_type():
response = HttpResponse("<html><head></head><body></body></html>") response = HttpResponse("<html><head></head><body></body></html>")
response.status_code = 200 response.status_code = 200
response._signposts = [ response._signposts = [Signpost(LinkRel.item, "http://example.com", "text/json")]
Signpost(LinkRel.item, "http://example.com", "text/json")
]
middleware = HtmlSignpostingMiddleware(lambda request: response) middleware = HtmlSignpostingMiddleware(lambda request: response)
response = middleware(None) response = middleware(None)
@@ -100,9 +95,7 @@ def test_middleware_signpost_with_content_type():
def test_middleware_ignore_error_responses(): def test_middleware_ignore_error_responses():
response = HttpResponse("<html><head></head><body></body></html>") response = HttpResponse("<html><head></head><body></body></html>")
response.status_code = 400 response.status_code = 400
response._signposts = [ response._signposts = [Signpost(LinkRel.author, "http://example.com")]
Signpost(LinkRel.author, "http://example.com")
]
middleware = HtmlSignpostingMiddleware(lambda request: response) middleware = HtmlSignpostingMiddleware(lambda request: response)
response = middleware(None) response = middleware(None)
@@ -112,11 +105,8 @@ def test_middleware_ignore_error_responses():
def test_middleware_type_link(): def test_middleware_type_link():
response = HttpResponse("<html><head></head><body></body></html>") response = HttpResponse("<html><head></head><body></body></html>")
response.status_code = 200 response.status_code = 200
response._signposts = [ response._signposts = [Signpost(LinkRel.type, "http://schema.org/Dataset")]
Signpost(LinkRel.type, "http://schema.org/Dataset")
]
middleware = HtmlSignpostingMiddleware(lambda request: response) middleware = HtmlSignpostingMiddleware(lambda request: response)
response = middleware(None) response = middleware(None)
assert_links_exist(response) assert_links_exist(response)