73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package ingestion
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFetchOGImage(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
body string
|
|
want string // suffix match
|
|
}{
|
|
{
|
|
name: "og:image property",
|
|
body: `<html><head><meta property="og:image" content="https://example.com/lead.jpg"></head></html>`,
|
|
want: "https://example.com/lead.jpg",
|
|
},
|
|
{
|
|
name: "content before property",
|
|
body: `<html><head><meta content="https://example.com/swap.jpg" property="og:image"/></head></html>`,
|
|
want: "https://example.com/swap.jpg",
|
|
},
|
|
{
|
|
name: "twitter:image fallback",
|
|
body: `<html><head><meta name="twitter:image" content="https://example.com/twit.jpg"></head></html>`,
|
|
want: "https://example.com/twit.jpg",
|
|
},
|
|
{
|
|
name: "relative URL resolved",
|
|
body: `<html><head><meta property="og:image" content="/img/lead.jpg"></head></html>`,
|
|
want: "/img/lead.jpg",
|
|
},
|
|
{
|
|
name: "no og tags",
|
|
body: `<html><head><title>nothing</title></head></html>`,
|
|
want: "",
|
|
},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(c.body))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
got := FetchOGImage(srv.URL + "/article")
|
|
if c.want == "" {
|
|
if got != "" {
|
|
t.Errorf("expected empty, got %q", got)
|
|
}
|
|
return
|
|
}
|
|
if !strings.HasSuffix(got, c.want) {
|
|
t.Errorf("got %q, want suffix %q", got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFirstImgSrc(t *testing.T) {
|
|
in := `<p>Body <img alt="x" src="https://cdn.example.com/lead.png" width="600"/> more</p>`
|
|
if got := firstImgSrc(in); got != "https://cdn.example.com/lead.png" {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
if got := firstImgSrc(""); got != "" {
|
|
t.Errorf("empty input should yield empty, got %q", got)
|
|
}
|
|
}
|