2020-10-27 17:35:28 +00:00
|
|
|
package urls
|
|
|
|
|
|
|
|
import (
|
2021-01-20 12:31:11 +00:00
|
|
|
"strings"
|
2021-01-20 12:37:02 +00:00
|
|
|
"testing"
|
2020-10-27 17:35:28 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetLinkPreviewData(t *testing.T) {
|
|
|
|
|
|
|
|
statusTownhall := LinkPreviewData{
|
|
|
|
Site: "YouTube",
|
|
|
|
Title: "Status Town Hall #67 - 12 October 2020",
|
|
|
|
ThumbnailURL: "https://i.ytimg.com/vi/mzOyYtfXkb0/hqdefault.jpg",
|
|
|
|
}
|
|
|
|
|
|
|
|
previewData, err := GetLinkPreviewData("https://www.youtube.com/watch?v=mzOyYtfXkb0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, statusTownhall.Site, previewData.Site)
|
|
|
|
require.Equal(t, statusTownhall.Title, previewData.Title)
|
|
|
|
require.Equal(t, statusTownhall.ThumbnailURL, previewData.ThumbnailURL)
|
|
|
|
|
|
|
|
previewData, err = GetLinkPreviewData("https://youtu.be/mzOyYtfXkb0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, statusTownhall.Site, previewData.Site)
|
|
|
|
require.Equal(t, statusTownhall.Title, previewData.Title)
|
|
|
|
require.Equal(t, statusTownhall.ThumbnailURL, previewData.ThumbnailURL)
|
|
|
|
|
|
|
|
_, err = GetLinkPreviewData("https://www.test.com/unknown")
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
}
|
2021-01-19 15:53:27 +00:00
|
|
|
|
2021-02-03 11:27:04 +00:00
|
|
|
// split at "." and ignore the first item
|
2021-02-03 13:25:42 +00:00
|
|
|
func thumbnailURLWithoutSubdomain(url string) []string {
|
2021-02-03 11:27:04 +00:00
|
|
|
return strings.Split(url, ".")[1:]
|
|
|
|
}
|
|
|
|
|
2021-01-19 15:53:27 +00:00
|
|
|
func TestGetGiphyPreviewData(t *testing.T) {
|
|
|
|
validGiphyLink := "https://giphy.com/gifs/FullMag-robot-boston-dynamics-dance-lcG3qwtTKSNI2i5vst"
|
|
|
|
previewData, err := GetGiphyPreviewData(validGiphyLink)
|
|
|
|
bostonDynamicsEthGifData := LinkPreviewData{
|
|
|
|
Site: "GIPHY",
|
|
|
|
Title: "Boston Dynamics Yes GIF by FullMag - Find & Share on GIPHY",
|
|
|
|
ThumbnailURL: "https://media1.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif",
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, bostonDynamicsEthGifData.Site, previewData.Site)
|
|
|
|
require.Equal(t, bostonDynamicsEthGifData.Title, previewData.Title)
|
2021-01-20 12:31:11 +00:00
|
|
|
|
|
|
|
// Giphy oembed returns links to different servers: https://media1.giphy.com, https://media2.giphy.com and so on
|
|
|
|
// We don't care about the server as long as other parts are equal, so we split at "." and ignore the first item
|
2021-02-03 13:25:42 +00:00
|
|
|
require.Equal(t, thumbnailURLWithoutSubdomain(bostonDynamicsEthGifData.ThumbnailURL), thumbnailURLWithoutSubdomain(previewData.ThumbnailURL))
|
2021-01-19 15:53:27 +00:00
|
|
|
|
|
|
|
invalidGiphyLink := "https://giphy.com/gifs/this-gif-does-not-exist-44444"
|
|
|
|
_, err = GetGiphyPreviewData(invalidGiphyLink)
|
|
|
|
require.Error(t, err)
|
2021-02-03 11:27:04 +00:00
|
|
|
|
|
|
|
mediaLink := "https://media.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif"
|
|
|
|
|
|
|
|
mediaLinkData, _ := GetGiphyPreviewData(mediaLink)
|
|
|
|
|
2021-02-03 13:25:42 +00:00
|
|
|
require.Equal(t, thumbnailURLWithoutSubdomain(mediaLinkData.ThumbnailURL), thumbnailURLWithoutSubdomain(previewData.ThumbnailURL))
|
2021-01-19 15:53:27 +00:00
|
|
|
}
|
2021-01-20 11:14:24 +00:00
|
|
|
|
2021-02-03 13:11:53 +00:00
|
|
|
func TestGetGiphyLongURL(t *testing.T) {
|
|
|
|
shortURL := "https://gph.is/g/aXLyK7P"
|
|
|
|
computedLongURL, _ := GetGiphyLongURL(shortURL)
|
|
|
|
actualLongURL := "https://giphy.com/gifs/FullMag-robot-boston-dynamics-dance-lcG3qwtTKSNI2i5vst"
|
|
|
|
|
|
|
|
require.Equal(t, computedLongURL, actualLongURL)
|
|
|
|
|
|
|
|
_, err := GetGiphyLongURL("http://this-giphy-site-doesn-not-exist.se/bogus-url")
|
|
|
|
require.Error(t, err)
|
|
|
|
|
2021-02-03 13:58:12 +00:00
|
|
|
_, err = GetGiphyLongURL("http://gph.is/bogus-url-but-correct-domain")
|
2021-02-03 13:11:53 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetGiphyShortURLPreviewData(t *testing.T) {
|
|
|
|
shortURL := "https://gph.is/g/aXLyK7P"
|
|
|
|
previewData, err := GetGiphyShortURLPreviewData(shortURL)
|
|
|
|
|
|
|
|
bostonDynamicsEthGifData := LinkPreviewData{
|
|
|
|
Site: "GIPHY",
|
|
|
|
Title: "Boston Dynamics Yes GIF by FullMag - Find & Share on GIPHY",
|
|
|
|
ThumbnailURL: "https://media1.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, bostonDynamicsEthGifData.Site, previewData.Site)
|
|
|
|
require.Equal(t, bostonDynamicsEthGifData.Title, previewData.Title)
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:14:24 +00:00
|
|
|
func TestGetTenorPreviewData(t *testing.T) {
|
|
|
|
validTenorLink := "https://tenor.com/view/robot-dance-do-you-love-me-boston-boston-dynamics-dance-gif-19998728"
|
|
|
|
previewData, err := GetTenorPreviewData(validTenorLink)
|
|
|
|
|
|
|
|
gifData := LinkPreviewData{
|
|
|
|
Site: "Tenor",
|
|
|
|
Title: "Annihere",
|
|
|
|
ThumbnailURL: "https://media.tenor.com/images/975f6b95d188c277ebba62d9b5511685/tenor.gif",
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, gifData.Site, previewData.Site)
|
|
|
|
require.Equal(t, gifData.Title, previewData.Title)
|
|
|
|
require.Equal(t, gifData.ThumbnailURL, previewData.ThumbnailURL)
|
|
|
|
|
|
|
|
invalidTenorLink := "https://giphy.com/gifs/this-gif-does-not-exist-44444"
|
|
|
|
_, err = GetTenorPreviewData(invalidTenorLink)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|