linkinfo/default_test.go

70 lines
1.5 KiB
Go

package linkinfo
import (
"strings"
"testing"
)
func Test_LinkInfoApi_detectContentType(t *testing.T) {
api := New()
contentType := api.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)
}
}
func Test_LinkInfoApi_defaultLinkHandler(t *testing.T) {
type expectedData struct {
Link string
Title string
Type string
}
testLinks := []expectedData{
{"http://example.com", "Example Domain", "text/html"},
{"http://techslides.com/demos/sample-videos/small.mp4", "", "video/mp4"},
}
api := New()
for _, link := range testLinks {
l, err := api.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_LinkInfoApi_retrieveHtmlLinkTitle(t *testing.T) {
ret := &LinkInfo{}
api := New()
if err := api.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)
}
}