File size: 4,708 Bytes
25834e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | """Tests for the destination meta-tag parser."""
from __future__ import annotations
from app.services.meta_tags.parse_html import parse_meta_tags
BASE = "https://dest.example.com/article/42"
def test_full_og_page():
html = """<!doctype html><html><head>
<title>HTML Title</title>
<meta property="og:title" content="OG Title">
<meta property="og:description" content="OG Desc">
<meta property="og:image" content="https://dest.example.com/og.png">
<meta property="og:site_name" content="Dest">
<meta name="twitter:title" content="TW Title">
<meta name="theme-color" content="#FF5733">
</head><body>ignored</body></html>"""
p = parse_meta_tags(html, BASE)
assert p.title == "OG Title" # og beats twitter beats <title>
assert p.description == "OG Desc"
assert p.image == "https://dest.example.com/og.png"
assert p.color == "#FF5733"
assert p.site_name == "Dest"
assert p.og["title"] == "OG Title"
assert p.twitter["title"] == "TW Title"
def test_fallback_chain_title():
html = "<head><title>Only Title</title></head>"
assert parse_meta_tags(html, BASE).title == "Only Title"
html = '<head><meta name="twitter:title" content="TW"><title>T</title></head>'
assert parse_meta_tags(html, BASE).title == "TW"
def test_property_name_attribute_mixups_accepted():
# Sites swap property=/name= constantly — accept both for both families.
html = """<head>
<meta name="og:title" content="via name">
<meta property="twitter:description" content="via property">
</head>"""
p = parse_meta_tags(html, BASE)
assert p.title == "via name"
assert p.description == "via property"
def test_first_tag_wins():
html = """<head>
<meta property="og:title" content="first">
<meta property="og:title" content="second">
</head>"""
assert parse_meta_tags(html, BASE).title == "first"
def test_relative_image_resolved_against_final_url():
html = '<head><meta property="og:image" content="/img/og.png"></head>'
assert parse_meta_tags(html, BASE).image == "https://dest.example.com/img/og.png"
def test_http_image_dropped():
html = '<head><meta property="og:image" content="http://insecure/og.png"></head>'
assert parse_meta_tags(html, BASE).image is None
def test_bad_theme_color_dropped():
html = '<head><meta name="theme-color" content="rebeccapurple"></head>'
assert parse_meta_tags(html, BASE).color is None
def test_body_meta_ignored():
# Stop at <body>: preview crawlers only read head, and so do we.
html = """<head><title>T</title></head>
<body><meta property="og:title" content="sneaky"></body>"""
p = parse_meta_tags(html, BASE)
assert p.title == "T"
def test_broken_html_tolerated():
html = '<head><meta property="og:title" content="ok"><div><span></head>'
assert parse_meta_tags(html, BASE).title == "ok"
def test_entities_decoded():
html = '<head><meta property="og:title" content="A & B"></head>'
assert parse_meta_tags(html, BASE).title == "A & B"
def test_no_tags_page():
p = parse_meta_tags("<html><head></head><body></body></html>", BASE)
assert p.title is None and p.description is None and p.image is None
def test_html_title_and_description_exposed_raw():
# The audit surface needs the raw <title>/<meta description> even when
# og tags exist and win the normalized picks.
html = """<head>
<title>HTML Title</title>
<meta name="description" content="HTML Desc">
<meta property="og:title" content="OG Title">
<meta property="og:description" content="OG Desc">
</head>"""
p = parse_meta_tags(html, BASE)
assert p.title == "OG Title"
assert p.html_title == "HTML Title"
assert p.html_description == "HTML Desc"
def test_favicon_prefers_apple_touch_icon():
html = """<head>
<link rel="icon" href="/favicon.ico" sizes="16x16">
<link rel="apple-touch-icon" href="/apple-icon.png">
<link rel="shortcut icon" href="/legacy.ico">
</head>"""
p = parse_meta_tags(html, BASE)
assert p.favicon == "https://dest.example.com/apple-icon.png"
def test_favicon_picks_largest_declared_size():
html = """<head>
<link rel="icon" href="/small.png" sizes="16x16">
<link rel="icon" href="/big.png" sizes="64x64">
</head>"""
assert parse_meta_tags(html, BASE).favicon == "https://dest.example.com/big.png"
def test_favicon_defaults_to_convention_path():
p = parse_meta_tags("<head><title>T</title></head>", BASE)
assert p.favicon == "https://dest.example.com/favicon.ico"
|