Browse Source

test(rewrite): fix flaky test case by sorting query string keys

Frédéric Guillot 8 months ago
parent
commit
0f3c04a98a

+ 8 - 9
internal/reader/rewrite/content_rewrite_functions.go

@@ -331,20 +331,19 @@ func addInvidiousVideo(entryURL, entryContent string) string {
 		return entryContent
 	}
 
-	v := u.Query().Get("v")
-	if v == "" {
+	qs := u.Query()
+	videoID := qs.Get("v")
+	if videoID == "" {
 		return entryContent
 	}
+	qs.Del("v")
 
-	src := "https://" + u.Hostname() + `/embed/` + v
-	for key, val := range u.Query() {
-		if key == "v" || len(val) != 1 {
-			continue
-		}
-		src += "&" + key + "=" + val[0]
+	embedVideoURL := "https://" + u.Hostname() + `/embed/` + videoID
+	if len(qs) > 0 {
+		embedVideoURL += "?" + qs.Encode()
 	}
 
-	return addVideoPlayerIframe(src, entryContent)
+	return addVideoPlayerIframe(embedVideoURL, entryContent)
 }
 
 func addPDFLink(entryURL, entryContent string) string {

+ 2 - 2
internal/reader/rewrite/content_rewrite_test.go

@@ -284,13 +284,13 @@ func TestAddInvidiousVideo(t *testing.T) {
 		// Test with query parameters
 		"https://invidious.io/watch?v=dQw4w9WgXcQ&t=30s": {
 			"Video with timestamp",
-			`<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ&t=30s" allowfullscreen></iframe><br>Video with timestamp`,
+			`<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ?t=30s" allowfullscreen></iframe><br>Video with timestamp`,
 		},
 
 		// Test with more complex query parameters
 		"https://invidious.io/watch?v=dQw4w9WgXcQ&t=30s&autoplay=1": {
 			"Video with multiple parameters",
-			`<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ&t=30s&autoplay=1" allowfullscreen></iframe><br>Video with multiple parameters`,
+			`<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ?autoplay=1&t=30s" allowfullscreen></iframe><br>Video with multiple parameters`,
 		},
 
 		// Test with non-matching URLs (should return content unchanged)