dumb/handlers/lyrics_test.go

58 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-03-06 20:53:29 +01:00
package handlers
import (
"net/http"
"net/http/httptest"
2024-05-02 21:29:50 +01:00
"os"
2024-03-06 20:53:29 +01:00
"testing"
"github.com/PuerkitoBio/goquery"
2024-05-02 21:29:50 +01:00
"github.com/rramiachraf/dumb/utils"
2024-03-06 20:53:29 +01:00
)
func TestLyrics(t *testing.T) {
2024-04-06 00:14:51 -06:00
urls := []string{"/The-silver-seas-catch-yer-own-train-lyrics",
"/1784308/The-silver-seas-catch-yer-own-train",
"/1784308/The-silver-seas-catch-yer-own-train-lyrics",
"/1784308/The-silver-seas-catch-yer-own-train/Baby-you-and-i-are-not-the-same-you-say-you-like-sun-i-like-the-rain",
"/1784308/The-silver-seas-catch-yer-own-train-lyrics/Baby-you-and-i-are-not-the-same-you-say-you-like-sun-i-like-the-rain",
"/1784308"}
for _, url := range urls {
t.Run(url, func(t *testing.T) { testLyrics(t, url) })
}
}
func testLyrics(t *testing.T, url string) {
2024-03-06 20:53:29 +01:00
title := "The Silver Seas"
artist := "Catch Yer Own Train"
r, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
2024-05-02 21:29:50 +01:00
l := utils.NewLogger(os.Stdout)
2024-05-03 14:47:57 +01:00
m := New(l, &assets{})
2024-03-06 20:53:29 +01:00
m.ServeHTTP(rr, r)
defer rr.Result().Body.Close()
doc, err := goquery.NewDocumentFromReader(rr.Result().Body)
if err != nil {
t.Fatal(err)
}
2024-06-11 21:53:22 +01:00
docArtist := doc.Find("#metadata-info h1").Text()
docTitle := doc.Find("#metadata-info h2").Text()
2024-03-06 20:53:29 +01:00
if docTitle != title {
t.Fatalf("expected %q, got %q\n", title, docTitle)
}
if docArtist != artist {
t.Fatalf("expected %q, got %q\n", artist, docArtist)
}
}