diff --git a/swh/web/common/swh_templatetags.py b/swh/web/common/swh_templatetags.py --- a/swh/web/common/swh_templatetags.py +++ b/swh/web/common/swh_templatetags.py @@ -57,14 +57,10 @@ The text as is otherwise. """ - links = text.split(",") - ret = "" - for i, link in enumerate(links): - ret += re.sub(r"<(http.*)>", r'<\1>', link) - # add one link per line and align them - if i != len(links) - 1: - ret += "\n " - return ret + ret = re.sub( + r'<(http[^<>]+)>; rel="([^,]+)"', r'<\1>; rel="\2"\n', text + ).replace("\n,", "\n") + return ret[:-1] @register.filter diff --git a/swh/web/tests/common/test_templatetags.py b/swh/web/tests/common/test_templatetags.py --- a/swh/web/tests/common/test_templatetags.py +++ b/swh/web/tests/common/test_templatetags.py @@ -3,6 +3,9 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information +import pytest + +from swh.web.api.apiresponse import compute_link_header from swh.web.common.swh_templatetags import ( docstring_display, urlize_header_links, @@ -24,19 +27,30 @@ assert urlize_links_and_mails(email) == expected_content -def test_urlize_header_links(): +@pytest.mark.parametrize( + "next_link, prev_link", + [ + ("https://example.org/api/1/abc/", "https://example.org/api/1/def/"), + ("https://example.org/api/1/0,5/", "https://example.org/api/1/5,10/"), + ], +) +def test_urlize_header_links(next_link, prev_link): - next_link = "https://example.com/api/1/abc/" - prev_link = "https://example.com/api/1/def/" + link_header = f'<{next_link}>; rel="next",<{prev_link}>; rel="previous"' - content = f'<{next_link}>; rel="next"\n<{prev_link}>; rel="prev"' + assert ( + link_header + == compute_link_header( + {"headers": {"link-next": next_link, "link-prev": prev_link}}, options={} + )["Link"] + ) expected_content = ( f'<{next_link}>; rel="next"\n' - f'<{prev_link}>; rel="prev"' + f'<{prev_link}>; rel="previous"' ) - assert urlize_header_links(content) == expected_content + assert urlize_header_links(link_header) == expected_content def test_docstring_display():