|
- package extractor
-
- import (
- "reflect"
- "testing"
- )
-
- func TestExtractStreams(t *testing.T) {
- html := `
- <script>
- var streamsrc = 'https://example.com/live/stream.mp3';
- var streamhash="https://cdn.example.net/relay.m3u8";
- var audioUrl="https://example.com/audio.ogg";
- </script>
- <a href="https://streams.example.org/radio.aac?user=test">listen</a>
- <source src="//players.example.eu/ambient.ogg" type="audio/ogg" />
- <audio src="https://stream.example.com/live"></audio>
- <audio data-src="https://pod.example.com/episode.opus"></audio>
- <div data-value="https://example.com/secret.pls"></div>
- `
-
- streams := ExtractStreams(html)
- if len(streams) != 7 {
- t.Fatalf("wanted 7 streams, got %d: %v", len(streams), streams)
- }
- found := false
- for _, s := range streams {
- if s == "https://stream.example.com/live" {
- found = true
- break
- }
- }
- if !found {
- t.Fatalf("expected audio tag stream to be present: %v", streams)
- }
- }
-
- func TestExtractPlaylistLinks(t *testing.T) {
- html := `
- <a href="https://example.com/stream.m3u">m3u</a>
- <a href="https://example.com/playlist.pls">pls</a>
- <a href="https://example.com/radio.xspf">xspf</a>
- <a href="https://example.com/data.json">json</a>
- `
- links := ExtractPlaylistLinks(html)
- if len(links) != 4 {
- t.Fatalf("wanted 4 playlist links, got %d: %v", len(links), links)
- }
- }
-
- func TestExtractEmbedURLs(t *testing.T) {
- html := `<iframe src="//example.com/embed"></iframe><iframe src="https://example.org/player"></iframe>`
- urls := ExtractEmbedURLs(html)
- want := []string{"https://example.com/embed", "https://example.org/player"}
- if !reflect.DeepEqual(urls, want) {
- t.Fatalf("wanted iframe URLs %v, got %v", want, urls)
- }
- }
-
- func TestExtractScriptURLs(t *testing.T) {
- html := `<script src="/js/app.js"></script><script src="https://example.org/player.js"></script>`
- urls := ExtractScriptURLs(html)
- want := []string{"/js/app.js", "https://example.org/player.js"}
- if !reflect.DeepEqual(urls, want) {
- t.Fatalf("wanted script URLs %v, got %v", want, urls)
- }
- }
-
- func TestParsePlaylist(t *testing.T) {
- m3u := "#EXTM3U\nhttps://example.com/live.mp3\n"
- pls := "[playlist]\nFile1=https://example.com/stream.aac\n"
- xspf := "<playlist><location>https://example.com/hls.m3u8</location></playlist>"
-
- if len(ParsePlaylist(m3u, "audio/x-mpegurl", "https://example.com/playlist.m3u")) != 1 {
- t.Fatal("expected m3u playlist to yield 1 stream")
- }
- if len(ParsePlaylist(pls, "audio/x-scpls", "https://example.com/playlist.pls")) != 1 {
- t.Fatal("expected pls playlist to yield 1 stream")
- }
- if len(ParsePlaylist(xspf, "application/xspf+xml", "https://example.com/playlist.xspf")) != 1 {
- t.Fatal("expected xspf playlist to yield 1 stream")
- }
-
- relative := "stream/live.mp3\n"
- resolved := ParsePlaylist(relative, "audio/x-mpegurl", "https://example.com/radio/list.m3u")
- if len(resolved) != 1 || resolved[0] != "https://example.com/radio/stream/live.mp3" {
- t.Fatalf("expected relative URL to resolve against base: %v", resolved)
- }
- }
|