Fix oEmbed CI: accept any host in URL parsing

This commit is contained in:
Zed 2026-08-15 00:23:53 +07:00
commit 06e59f0615
2 changed files with 17 additions and 2 deletions

View file

@ -34,6 +34,16 @@ proc parseTweetUrl*(url: string; cfg: Config): tuple[username, id: string] =
if path.startsWith(nitterPrefix): if path.startsWith(nitterPrefix):
return parseTweetPath(path[nitterPrefix.len..^1]) return parseTweetPath(path[nitterPrefix.len..^1])
# Fall back: strip any hostname and try to parse as a tweet path.
# Handles requests where the URL's host differs from cfg.hostname
# (e.g. localhost in dev/CI, or a reverse proxy with a different domain).
let slashPos = path.find('/')
if slashPos > 0:
let afterHost = path[slashPos + 1..^1]
let parsed = parseTweetPath(afterHost)
if parsed.username.len > 0:
return parsed
return ("", "") return ("", "")
proc createEmbedRouter*(cfg: Config) = proc createEmbedRouter*(cfg: Config) =

View file

@ -230,13 +230,14 @@ class OEmbedApiTest(BaseTestCase):
class OEmbedDiscoveryTest(BaseTestCase): class OEmbedDiscoveryTest(BaseTestCase):
"""Test oEmbed discovery link tags on tweet pages.""" """Test oEmbed discovery link tags on tweet pages."""
base_url = 'http://localhost:8080'
def test_tweet_page_has_oembed_link_tag(self): def test_tweet_page_has_oembed_link_tag(self):
self.open_nitter('elonmusk/status/1141367104702038016') self.open_nitter('elonmusk/status/1141367104702038016')
self.assert_element_present('link[type="application/json+oembed"]') self.assert_element_present('link[type="application/json+oembed"]')
def test_oembed_link_tag_points_to_api(self): def test_oembed_link_tag_points_to_api(self):
resp = requests.get('http://localhost:8080/elonmusk/status/1141367104702038016') resp = requests.get(f'{self.base_url}/elonmusk/status/1141367104702038016')
self.assertIn('application/json+oembed', resp.text) self.assertIn('application/json+oembed', resp.text)
self.assertIn('/api/oembed?url=', resp.text) self.assertIn('/api/oembed?url=', resp.text)
self.assertIn('1141367104702038016', resp.text) self.assertIn('1141367104702038016', resp.text)
@ -244,11 +245,15 @@ class OEmbedDiscoveryTest(BaseTestCase):
def test_oembed_discovery_roundtrip(self): def test_oembed_discovery_roundtrip(self):
"""Fetch a tweet page, extract oEmbed URL, call it, verify response.""" """Fetch a tweet page, extract oEmbed URL, call it, verify response."""
import re import re
resp = requests.get('http://localhost:8080/elonmusk/status/1141367104702038016') from urllib.parse import urlparse
resp = requests.get(f'{self.base_url}/elonmusk/status/1141367104702038016')
match = re.search( match = re.search(
r'type="application/json\+oembed"\s+href="([^"]*)"', resp.text) r'type="application/json\+oembed"\s+href="([^"]*)"', resp.text)
self.assertIsNotNone(match, "No oEmbed discovery link found in page") self.assertIsNotNone(match, "No oEmbed discovery link found in page")
oembed_url = match.group(1).replace('&', '&') oembed_url = match.group(1).replace('&', '&')
# Rewrite host to base_url in case cfg.hostname differs (e.g. CI)
parsed = urlparse(oembed_url)
oembed_url = f'{self.base_url}{parsed.path}?{parsed.query}'
oembed_resp = requests.get(oembed_url) oembed_resp = requests.get(oembed_url)
self.assertEqual(oembed_resp.status_code, 200) self.assertEqual(oembed_resp.status_code, 200)
data = oembed_resp.json() data = oembed_resp.json()