parent
999758c790
commit
e5aa8fe187
pkg/validation
@ -2,6 +2,8 @@ package validation
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
neturl "net/url"
|
neturl "net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -16,9 +18,16 @@ var (
|
|||||||
// Music hosts that we support
|
// Music hosts that we support
|
||||||
var musicHosts = []string{
|
var musicHosts = []string{
|
||||||
"youtube.com",
|
"youtube.com",
|
||||||
|
"www.youtube.com",
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsUrl(url string) (bool, *neturl.URL) {
|
func IsUrl(url string) (bool, *neturl.URL) {
|
||||||
|
// If a URL has no scheme, this will fail.
|
||||||
|
// So we'll add one if not present
|
||||||
|
if !strings.Contains(url, "://") {
|
||||||
|
url = fmt.Sprintf("https://%s", url)
|
||||||
|
}
|
||||||
|
|
||||||
parsed, err := neturl.ParseRequestURI(url)
|
parsed, err := neturl.ParseRequestURI(url)
|
||||||
|
|
||||||
return (err == nil), parsed
|
return (err == nil), parsed
|
||||||
@ -37,6 +46,7 @@ func IsMusicUrl(url string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, host := range musicHosts {
|
for _, host := range musicHosts {
|
||||||
|
log.Println(parsed.Host)
|
||||||
if host == parsed.Host {
|
if host == parsed.Host {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package validation
|
package validation
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"log"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestIsUrl(t *testing.T) {
|
func TestIsUrl(t *testing.T) {
|
||||||
is, _ := IsUrl("definitely not a url")
|
is, _ := IsUrl("definitely not a url")
|
||||||
@ -16,6 +19,38 @@ func TestIsUrl(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSchemeHandling(t *testing.T) {
|
||||||
|
// No scheme but valid url
|
||||||
|
is, _ := IsUrl("youtube.com")
|
||||||
|
|
||||||
|
if !is {
|
||||||
|
t.Error("URL without scheme came back as not a url")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preserve scheme
|
||||||
|
is, parsed := IsUrl("ftp://youtube.com")
|
||||||
|
|
||||||
|
if !is {
|
||||||
|
t.Error("URL without scheme came back as not a url")
|
||||||
|
}
|
||||||
|
|
||||||
|
if parsed.Scheme != "ftp" {
|
||||||
|
t.Error("URL scheme was replaced incorrectly")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSupportedMusicUrls(t *testing.T) {
|
||||||
|
// Test actual music url
|
||||||
|
for _, url := range musicHosts {
|
||||||
|
log.Println(url)
|
||||||
|
is, _ := IsMusicUrl(url)
|
||||||
|
|
||||||
|
if !is {
|
||||||
|
t.Error("Supported service was detected as unsupported")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIsMusicUrlErrors(t *testing.T) {
|
func TestIsMusicUrlErrors(t *testing.T) {
|
||||||
// Not a URL
|
// Not a URL
|
||||||
is, err := IsMusicUrl("not a url")
|
is, err := IsMusicUrl("not a url")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user