소스 검색

Fix oEmbed CI: accept any host in URL parsing

Zed 3 주 전
부모
커밋
06e59f0615
2개의 변경된 파일17개의 추가작업 그리고 2개의 파일을 삭제
  1. 10 0
      src/routes/embed.nim
  2. 7 2
      tests/test_embed.py

+ 10 - 0
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) =

+ 7 - 2
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()