linkinfo/default_test.go

64 lines
1.4 KiB
Go
Raw Normal View History

package linkinfo
2019-10-03 23:44:38 +00:00
import (
"strings"
2019-10-03 23:44:38 +00:00
"testing"
)
func Test_detectContentType(t *testing.T) {
contentType := detectContentType("http://example.com", "")
if contentType == "" {
t.Fatal("Unable to test example.com, returned empty type")
}
if idx := strings.Index(contentType, ";"); idx != -1 {
contentType = contentType[:idx]
}
if contentType != ContentTypeHtml {
t.Fatal("Unexpected content type:", contentType)
2019-10-03 23:44:38 +00:00
}
}
2019-10-03 23:44:38 +00:00
func Test_defaultLinkHandler(t *testing.T) {
2019-10-03 23:44:38 +00:00
type expectedData struct {
Link string
2019-10-03 23:44:38 +00:00
Title string
Type string
2019-10-03 23:44:38 +00:00
}
testLinks := []expectedData{
{"https://paste.ee", "Paste.ee", "text/html"},
{"http://techslides.com/demos/sample-videos/small.mp4", "", "video/mp4"},
}
for _, link := range testLinks {
l, err := defaultLinkHandler(link.Link)
if err != nil {
t.Fatal("Unable to retrieve link info:", err)
}
if link.Title != "" && link.Title != l.Title {
t.Fatal("Unexpected title, expected:", link.Title, "got:", l.Title)
}
if link.Type != "" && link.Type != l.ContentType {
t.Fatal("Unexpected content type, expected:", link.Type, "got:", l.ContentType)
}
}
}
func Test_retrieveHtmlLinkTitle(t *testing.T) {
ret := &LinkInfo{}
if err := retrieveHtmlLinkTitle(ret, "http://example.com"); err != nil {
t.Fatal("Unable to retrieve html link title:", err)
}
if ret.Title != "Example Domain" {
t.Fatal("Unexpected title", ret.Title)
}
}