package api import "testing" func TestEmbedVideoURL(t *testing.T) { good := map[string]string{ "": "", "https://www.youtube.com/embed/iZ-PIqFOY0M": "https://www.youtube.com/embed/iZ-PIqFOY0M", "https://youtu.be/V1w8DhkWARc?si=abc": "https://www.youtube.com/embed/V1w8DhkWARc", "https://www.youtube.com/watch?v=-oaWiVEUSx8": "https://www.youtube.com/embed/-oaWiVEUSx8", "https://m.youtube.com/shorts/V1w8DhkWARc": "https://www.youtube.com/embed/V1w8DhkWARc", " https://rutube.ru/video/0123456789abcdef0123456789abcdef/ ": "https://rutube.ru/play/embed/0123456789abcdef0123456789abcdef", "https://rutube.ru/play/embed/0123456789abcdef0123456789abcdef": "https://rutube.ru/play/embed/0123456789abcdef0123456789abcdef", } for in, want := range good { got, err := embedVideoURL(in) if err != nil || got != want { t.Errorf("embedVideoURL(%q) = %q, %v; want %q", in, got, err, want) } } for _, in := range []string{ "javascript:alert(1)", "https://evil.example/embed/iZ-PIqFOY0M", "https://youtube.com.evil.example/embed/iZ-PIqFOY0M", "https://www.youtube.com/embed/iZ-PIqFOY0M/../../evil", "https://www.youtube.com/watch?v=", "//www.youtube.com/embed/iZ-PIqFOY0M", } { if got, err := embedVideoURL(in); err == nil { t.Errorf("embedVideoURL(%q) accepted as %q", in, got) } } }