diff --git a/src/routes/embed.nim b/src/routes/embed.nim index d1531e4..24bba2d 100644 --- a/src/routes/embed.nim +++ b/src/routes/embed.nim @@ -34,6 +34,16 @@ proc parseTweetUrl*(url: string; cfg: Config): tuple[username, id: string] = if path.startsWith(nitterPrefix): 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 ("", "") proc createEmbedRouter*(cfg: Config) = diff --git a/tests/test_embed.py b/tests/test_embed.py index 77b69e6..e44473a 100644 --- a/tests/test_embed.py +++ b/tests/test_embed.py @@ -230,13 +230,14 @@ class OEmbedApiTest(BaseTestCase): class OEmbedDiscoveryTest(BaseTestCase): """Test oEmbed discovery link tags on tweet pages.""" + base_url = 'http://localhost:8080' def test_tweet_page_has_oembed_link_tag(self): self.open_nitter('elonmusk/status/1141367104702038016') self.assert_element_present('link[type="application/json+oembed"]') 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('/api/oembed?url=', resp.text) self.assertIn('1141367104702038016', resp.text) @@ -244,11 +245,15 @@ class OEmbedDiscoveryTest(BaseTestCase): def test_oembed_discovery_roundtrip(self): """Fetch a tweet page, extract oEmbed URL, call it, verify response.""" 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( r'type="application/json\+oembed"\s+href="([^"]*)"', resp.text) self.assertIsNotNone(match, "No oEmbed discovery link found in page") 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) self.assertEqual(oembed_resp.status_code, 200) data = oembed_resp.json()