aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks/test_discovery.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tasks/test_discovery.py')
-rw-r--r--tests/tasks/test_discovery.py108
1 files changed, 68 insertions, 40 deletions
diff --git a/tests/tasks/test_discovery.py b/tests/tasks/test_discovery.py
index 7c28108..ab621e7 100644
--- a/tests/tasks/test_discovery.py
+++ b/tests/tasks/test_discovery.py
@@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: BSD-3-Clause
+from http import HTTPStatus
from types import ModuleType
from unittest.mock import Mock
@@ -425,9 +426,11 @@ def test_discovery(
) -> None:
def handler(request: httpx.Request) -> httpx.Response:
if request.method == "HEAD":
- return httpx.Response(200, headers=headers)
+ return httpx.Response(HTTPStatus.OK, headers=headers)
- return httpx.Response(200, headers={"Content-Type": "text/html"}, text=html)
+ return httpx.Response(
+ HTTPStatus.OK, headers={"Content-Type": "text/html"}, text=html
+ )
with (
app.app_context(),
@@ -446,9 +449,9 @@ def test_discovery(
pytest.param(
"https://example.com/test/23/page",
{
- "/test/23/page": (302, {"Location": "page/redirect-key"}),
+ "/test/23/page": (HTTPStatus.FOUND, {"Location": "page/redirect-key"}),
"/test/23/page/redirect-key": (
- 200,
+ HTTPStatus.OK,
{"Link": ("<webmention-endpoint/endpoint-key>; rel=webmention")},
),
},
@@ -489,7 +492,9 @@ def test_discovery_stops_after_head(app: Flask, sender_module: ModuleType) -> No
def handler(request: httpx.Request) -> httpx.Response:
requests.append(request)
- return httpx.Response(200, headers={"Link": "</webmention>; rel=webmention"})
+ return httpx.Response(
+ HTTPStatus.OK, headers={"Link": "</webmention>; rel=webmention"}
+ )
with (
app.app_context(),
@@ -512,10 +517,10 @@ def test_discovery_falls_back_to_get(app: Flask, sender_module: ModuleType) -> N
requests.append(request)
if request.method == "HEAD":
- return httpx.Response(200)
+ return httpx.Response(HTTPStatus.OK)
return httpx.Response(
- 200,
+ HTTPStatus.OK,
headers={"Content-Type": "text/html"},
text="""
<link
@@ -557,7 +562,9 @@ def test_discovery_falls_back_to_get_after_head_request_error(
if request.method == "HEAD":
raise failure
- return httpx.Response(200, headers={"Link": "</webmention>; rel=webmention"})
+ return httpx.Response(
+ HTTPStatus.OK, headers={"Link": "</webmention>; rel=webmention"}
+ )
with (
app.app_context(),
@@ -580,7 +587,9 @@ def test_discovery_does_not_suppress_unexpected_head_error(
def handler(request: httpx.Request) -> httpx.Response:
methods.append(request.method)
- return httpx.Response(200, headers={"Link": "</webmention>; rel=webmention"})
+ return httpx.Response(
+ HTTPStatus.OK, headers={"Link": "</webmention>; rel=webmention"}
+ )
with (
app.app_context(),
@@ -628,10 +637,10 @@ def test_discovery_skips_invalid_html_candidate(
) -> None:
def handler(request: httpx.Request) -> httpx.Response:
if request.method == "HEAD":
- return httpx.Response(200)
+ return httpx.Response(HTTPStatus.OK)
return httpx.Response(
- 200,
+ HTTPStatus.OK,
headers={"Content-Type": "text/html"},
text=(
f'<link rel="webmention" href="{invalid_href}">'
@@ -653,7 +662,7 @@ def test_discovery_skips_invalid_link_header_candidate(
) -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(
- 200,
+ HTTPStatus.OK,
headers=[
("Link", "<mailto:invalid@example.com>; rel=webmention"),
("Link", "<http://[invalid>; rel=webmention"),
@@ -675,10 +684,12 @@ def test_discovery_returns_none_when_all_html_candidates_are_invalid(
) -> None:
def handler(request: httpx.Request) -> httpx.Response:
if request.method == "HEAD":
- return httpx.Response(200, headers={"Link": "<mailto:bad>; rel=webmention"})
+ return httpx.Response(
+ HTTPStatus.OK, headers={"Link": "<mailto:bad>; rel=webmention"}
+ )
return httpx.Response(
- 200,
+ HTTPStatus.OK,
headers={"Content-Type": "text/html"},
text='<link rel="webmention" href="javascript:void(0)">',
)
@@ -697,7 +708,7 @@ def test_resolve_endpoint_rejects_invalid_url(
) -> None:
is_http_url = Mock(return_value=False)
monkeypatch.setattr(discovery, "is_http_url", is_http_url)
- response = httpx.Response(200, request=httpx.Request("GET", TARGET_URL))
+ response = httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", TARGET_URL))
with pytest.raises(
sender_module.PermanentSenderError, match="Invalid Webmention endpoint"
):
@@ -709,7 +720,7 @@ def test_endpoint_from_headers_ignores_malformed_link(
sender_module: ModuleType,
) -> None:
response = httpx.Response(
- 200,
+ HTTPStatus.OK,
headers=[("Link", "not-a-link"), ("Link", "</webmention>; rel=webmention")],
request=httpx.Request("GET", TARGET_URL),
)
@@ -717,14 +728,14 @@ def test_endpoint_from_headers_ignores_malformed_link(
def test_endpoint_from_html_accepts_string_rel(sender_module: ModuleType) -> None:
- response = httpx.Response(200, request=httpx.Request("GET", TARGET_URL))
+ response = httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", TARGET_URL))
body = b'<link rel="webmention alternate" href="/webmention">'
assert sender_module.endpoint_from_html(response, body) == ENDPOINT_URL
def test_endpoint_from_html_accepts_uppercase_rel(sender_module: ModuleType) -> None:
- response = httpx.Response(200, request=httpx.Request("GET", TARGET_URL))
+ response = httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", TARGET_URL))
body = b'<LINK REL="ALTERNATE WEBMENTION" HREF="/webmention">'
assert sender_module.endpoint_from_html(response, body) == ENDPOINT_URL
@@ -733,7 +744,7 @@ def test_endpoint_from_html_accepts_uppercase_rel(sender_module: ModuleType) ->
def test_endpoint_from_html_ignores_elements_without_matching_rel_or_href(
sender_module: ModuleType,
) -> None:
- response = httpx.Response(200, request=httpx.Request("GET", TARGET_URL))
+ response = httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", TARGET_URL))
body = b'<link href="/ignored"><a rel="webmention">No endpoint</a>'
assert sender_module.endpoint_from_html(response, body) is None
@@ -744,7 +755,9 @@ def test_read_target_body_rejects_declared_oversized_body(
) -> None:
app.config["MAX_TARGET_BYTES"] = 10
- response = httpx.Response(200, headers={"Content-Length": "11"}, content=b"x" * 11)
+ response = httpx.Response(
+ HTTPStatus.OK, headers={"Content-Length": "11"}, content=b"x" * 11
+ )
with (
app.app_context(),
@@ -768,14 +781,14 @@ def test_read_target_body_does_not_mask_unexpected_value_error(
app.app_context(),
pytest.raises(ValueError, match="unrelated processing error"),
):
- sender_module.read_target_body(httpx.Response(200))
+ sender_module.read_target_body(httpx.Response(HTTPStatus.OK))
def test_read_target_body_ignores_invalid_content_length(
app: Flask, sender_module: ModuleType
) -> None:
response = httpx.Response(
- 200, headers={"Content-Length": "invalid"}, content=b"Document"
+ HTTPStatus.OK, headers={"Content-Length": "invalid"}, content=b"Document"
)
with app.app_context():
@@ -794,7 +807,7 @@ def test_read_target_body_rejects_streamed_oversized_body(
yield b"x" * 6
yield b"x" * 6
- response = httpx.Response(200, stream=BodyStream())
+ response = httpx.Response(HTTPStatus.OK, stream=BodyStream())
with (
app.app_context(),
@@ -814,9 +827,11 @@ def test_discovery_uses_link_header_from_get(
requests.append(request)
if request.method == "HEAD":
- return httpx.Response(200)
+ return httpx.Response(HTTPStatus.OK)
- return httpx.Response(200, headers={"Link": "</webmention>; rel=webmention"})
+ return httpx.Response(
+ HTTPStatus.OK, headers={"Link": "</webmention>; rel=webmention"}
+ )
with (
app.app_context(),
@@ -833,8 +848,10 @@ def test_discovery_uses_link_header_from_get(
@pytest.mark.parametrize(
("status_code", "exception_name"),
[
- pytest.param(503, "TemporarySenderError", id="temporary"),
- pytest.param(404, "PermanentSenderError", id="permanent"),
+ pytest.param(
+ HTTPStatus.SERVICE_UNAVAILABLE, "TemporarySenderError", id="temporary"
+ ),
+ pytest.param(HTTPStatus.NOT_FOUND, "PermanentSenderError", id="permanent"),
],
)
def test_discovery_maps_get_status_to_exception(
@@ -842,7 +859,7 @@ def test_discovery_maps_get_status_to_exception(
) -> None:
def handler(request: httpx.Request) -> httpx.Response:
if request.method == "HEAD":
- return httpx.Response(200)
+ return httpx.Response(HTTPStatus.OK)
return httpx.Response(status_code)
@@ -861,10 +878,10 @@ def test_discovery_returns_none_for_unsupported_media_type(
) -> None:
def handler(request: httpx.Request) -> httpx.Response:
if request.method == "HEAD":
- return httpx.Response(200)
+ return httpx.Response(HTTPStatus.OK)
return httpx.Response(
- 200, headers={"Content-Type": "application/json"}, content=b"{}"
+ HTTPStatus.OK, headers={"Content-Type": "application/json"}, content=b"{}"
)
with (
@@ -880,9 +897,12 @@ def test_discovery_returns_none_for_unsupported_media_type(
("status_code", "location", "expected_status_url"),
[
pytest.param(
- 201, "/webmention/status/123", STATUS_URL, id="created-with-location"
+ HTTPStatus.CREATED,
+ "/webmention/status/123",
+ STATUS_URL,
+ id="created-with-location",
),
- pytest.param(202, None, None, id="accepted"),
+ pytest.param(HTTPStatus.ACCEPTED, None, None, id="accepted"),
],
)
def test_post_webmention(
@@ -925,14 +945,22 @@ def test_post_webmention(
@pytest.mark.parametrize(
("code", "location", "expected"),
[
- (201, "/webmention/status/123", "https://example.com/webmention/status/123"),
- (201, "https://status.example.org/123", "https://status.example.org/123"),
- (201, "status/123", "https://example.com/status/123"),
- (201, "javascript:alert(1)", None),
- (201, "data:text/html,evil", None),
- (201, "ftp://example.com/status", None),
- (201, "http://[invalid", None),
- (202, "https://example.com/status", None),
+ (
+ HTTPStatus.CREATED,
+ "/webmention/status/123",
+ "https://example.com/webmention/status/123",
+ ),
+ (
+ HTTPStatus.CREATED,
+ "https://status.example.org/123",
+ "https://status.example.org/123",
+ ),
+ (HTTPStatus.CREATED, "status/123", "https://example.com/status/123"),
+ (HTTPStatus.CREATED, "javascript:alert(1)", None),
+ (HTTPStatus.CREATED, "data:text/html,evil", None),
+ (HTTPStatus.CREATED, "ftp://example.com/status", None),
+ (HTTPStatus.CREATED, "http://[invalid", None),
+ (HTTPStatus.ACCEPTED, "https://example.com/status", None),
],
)
def test_post_webmention_validates_location(