package clean import ( "strings" "testing" "golang.org/x/net/html" ) func prepareString(t *testing.T, markup string) string { t.Helper() doc, err := html.Parse(strings.NewReader("" + markup + "")) if err != nil { t.Fatal(err) } Prepare(doc, base) restoreImageSources(doc) // show real URLs rather than stand-ins var b strings.Builder body := doc.FirstChild.LastChild for c := body.FirstChild; c != nil; c = c.NextSibling { html.Render(&b, c) } return b.String() } func TestPrepareReplacesVideoEmbeds(t *testing.T) { tests := []struct{ name, in, want string }{ {"YouTube with title", ``, `

▶ Video: Never Gonna Give You Up

`}, {"YouTube without title", ``, `

▶ Video: https://www.youtube.com/watch?v=dQw4w9WgXcQ

`}, {"YouTube no-cookie, protocol-relative", ``, `

▶ Video: https://www.youtube.com/watch?v=abcDEF12345

`}, {"Vimeo", ``, `

▶ Video: The New Vimeo Player

`}, {"other iframes are left alone", ``, ``}, {"lookalike hosts are left alone", ``, ``}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := prepareString(t, tt.in); got != tt.want { t.Errorf("got %q\nwant %q", got, tt.want) } }) } } func TestPrepareResolvesLinks(t *testing.T) { // base is https://example.com/posts/article; relative links resolve // against its folder, /posts/. got := prepareString(t, `

a b c d

`) want := `

a b c d

` if got != want { t.Errorf("got %q\nwant %q", got, want) } }