Compare commits
3 Commits
master
...
of-encoder
Author | SHA1 | Date | |
---|---|---|---|
24eab40def | |||
6a6ea03e0b | |||
08890d15c0 |
@ -1,21 +0,0 @@
|
|||||||
name: Run Go Tests
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches: [master]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: Run Tests
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout Code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v4
|
|
||||||
with:
|
|
||||||
go-version: '1.22'
|
|
||||||
|
|
||||||
- name: Run Tests
|
|
||||||
run: make test
|
|
3
Makefile
3
Makefile
@ -1,3 +0,0 @@
|
|||||||
test:
|
|
||||||
@echo "Running tests for main and all packages"
|
|
||||||
go test ./...
|
|
@ -6,7 +6,7 @@ Currently in development.
|
|||||||
- `ffmpeg` make sure libopus is included
|
- `ffmpeg` make sure libopus is included
|
||||||
- `yt-dlp`
|
- `yt-dlp`
|
||||||
|
|
||||||
## Running locally
|
## Current testing steps
|
||||||
|
|
||||||
Copy .env.example to .env
|
Copy .env.example to .env
|
||||||
|
|
||||||
@ -15,7 +15,3 @@ Populate the discord bot keys
|
|||||||
`go run .`
|
`go run .`
|
||||||
|
|
||||||
type `!` in any channel in the discord while you're in a voice channel.
|
type `!` in any channel in the discord while you're in a voice channel.
|
||||||
|
|
||||||
## Running tests locally
|
|
||||||
|
|
||||||
`make test`
|
|
@ -1,54 +0,0 @@
|
|||||||
package validation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
neturl "net/url"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Errors
|
|
||||||
var (
|
|
||||||
ErrNotAUrl = errors.New("not a URL")
|
|
||||||
ErrIncorrectProtocol = errors.New("incorrect url protocol")
|
|
||||||
ErrServiceUnsupported = errors.New("not a URL")
|
|
||||||
)
|
|
||||||
|
|
||||||
// Music hosts that we support
|
|
||||||
var musicHosts = []string{
|
|
||||||
"youtube.com",
|
|
||||||
"www.youtube.com",
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
return (err == nil), parsed
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsMusicUrl(url string) (bool, error) {
|
|
||||||
isUrl, parsed := IsUrl(url)
|
|
||||||
|
|
||||||
if !isUrl {
|
|
||||||
return false, ErrNotAUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have a scheme and it's not http/https we fail
|
|
||||||
if parsed.Scheme != "" && !strings.Contains(parsed.Scheme, "http") {
|
|
||||||
return false, ErrIncorrectProtocol
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, host := range musicHosts {
|
|
||||||
if host == parsed.Host {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, ErrServiceUnsupported
|
|
||||||
}
|
|
@ -1,85 +0,0 @@
|
|||||||
package validation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestIsUrl(t *testing.T) {
|
|
||||||
is, _ := IsUrl("definitely not a url")
|
|
||||||
|
|
||||||
if is {
|
|
||||||
t.Error("Non-url text detected as URL")
|
|
||||||
}
|
|
||||||
|
|
||||||
is, _ = IsUrl("https://example.com")
|
|
||||||
|
|
||||||
if !is {
|
|
||||||
t.Error("URL not detected as URL")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
is, _ := IsMusicUrl(url)
|
|
||||||
|
|
||||||
if !is {
|
|
||||||
t.Error("Supported service was detected as unsupported")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsMusicUrlErrors(t *testing.T) {
|
|
||||||
// Not a URL
|
|
||||||
is, err := IsMusicUrl("not a url")
|
|
||||||
|
|
||||||
if is {
|
|
||||||
t.Error("Non-URL was detected as url")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != ErrNotAUrl {
|
|
||||||
t.Error("Incorrect error returned for Non-url link")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Incorrect protocol
|
|
||||||
is, err = IsMusicUrl("ssh://youtube.com")
|
|
||||||
|
|
||||||
if is {
|
|
||||||
t.Error("Incorrect protocol was not caught")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != ErrIncorrectProtocol {
|
|
||||||
t.Error("Incorrect error returned for incorrect protocol")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unsupported service
|
|
||||||
is, err = IsMusicUrl("https://www.deezer.com/")
|
|
||||||
|
|
||||||
if is {
|
|
||||||
t.Error("Unsupported service was not caught")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != ErrServiceUnsupported {
|
|
||||||
t.Error("Unsupported service did not return the correct error")
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user