2019-10-03 23:59:20 +00:00
|
|
|
package linkinfo
|
2019-10-03 23:44:38 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-03 23:59:20 +00:00
|
|
|
"strings"
|
2019-10-03 23:44:38 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-10-04 03:32:56 +00:00
|
|
|
func TestLinkInfoApi_detectContentType(t *testing.T) {
|
2019-10-04 01:32:00 +00:00
|
|
|
api := New()
|
2019-10-04 01:25:57 +00:00
|
|
|
|
|
|
|
contentType := api.detectContentType("http://example.com", "")
|
2019-10-03 23:59:20 +00:00
|
|
|
|
|
|
|
if contentType == "" {
|
|
|
|
t.Fatal("Unable to test example.com, returned empty type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx := strings.Index(contentType, ";"); idx != -1 {
|
|
|
|
contentType = contentType[:idx]
|
|
|
|
}
|
|
|
|
|
2019-10-04 00:50:58 +00:00
|
|
|
if contentType != contentTypeHtml {
|
2019-10-03 23:59:20 +00:00
|
|
|
t.Fatal("Unexpected content type:", contentType)
|
2019-10-03 23:44:38 +00:00
|
|
|
}
|
2019-10-03 23:59:20 +00:00
|
|
|
}
|
2019-10-03 23:44:38 +00:00
|
|
|
|
2019-10-04 03:32:56 +00:00
|
|
|
func TestLinkInfoApi_defaultLinkHandler(t *testing.T) {
|
2019-10-03 23:44:38 +00:00
|
|
|
type expectedData struct {
|
2019-10-03 23:59:20 +00:00
|
|
|
Link string
|
2019-10-03 23:44:38 +00:00
|
|
|
Title string
|
2019-10-03 23:59:20 +00:00
|
|
|
Type string
|
2019-10-03 23:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testLinks := []expectedData{
|
2019-10-04 01:32:00 +00:00
|
|
|
{"http://example.com", "Example Domain", "text/html"},
|
2019-10-03 23:44:38 +00:00
|
|
|
{"http://techslides.com/demos/sample-videos/small.mp4", "", "video/mp4"},
|
|
|
|
}
|
|
|
|
|
2019-10-04 01:32:00 +00:00
|
|
|
api := New()
|
2019-10-04 01:25:57 +00:00
|
|
|
|
2019-10-03 23:44:38 +00:00
|
|
|
for _, link := range testLinks {
|
2019-10-04 01:25:57 +00:00
|
|
|
l, err := api.DefaultLinkHandler(link.Link)
|
2019-10-03 23:44:38 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2019-10-03 23:59:20 +00:00
|
|
|
}
|
2019-10-04 00:02:33 +00:00
|
|
|
|
2019-10-04 03:32:56 +00:00
|
|
|
func TestLinkInfoApi_retrieveHtmlLinkTitle(t *testing.T) {
|
2019-10-04 00:02:33 +00:00
|
|
|
ret := &LinkInfo{}
|
|
|
|
|
2019-10-04 01:32:00 +00:00
|
|
|
api := New()
|
2019-10-04 01:25:57 +00:00
|
|
|
|
|
|
|
if err := api.retrieveHtmlLinkTitle(ret, "http://example.com"); err != nil {
|
2019-10-04 00:02:33 +00:00
|
|
|
t.Fatal("Unable to retrieve html link title:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret.Title != "Example Domain" {
|
|
|
|
t.Fatal("Unexpected title", ret.Title)
|
|
|
|
}
|
|
|
|
}
|