You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.8KB

  1. package extractor
  2. import "testing"
  3. func TestExtractStreams(t *testing.T) {
  4. html := `
  5. <script>
  6. var streamsrc = 'https://example.com/live/stream.mp3';
  7. var streamhash="https://cdn.example.net/relay.m3u8";
  8. var audioUrl="https://example.com/audio.ogg";
  9. </script>
  10. <a href="https://streams.example.org/radio.aac?user=test">listen</a>
  11. <source src="//players.example.eu/ambient.ogg" type="audio/ogg" />
  12. <audio data-src="https://pod.example.com/episode.opus"></audio>
  13. <div data-value="https://example.com/secret.pls"></div>
  14. `
  15. streams := ExtractStreams(html)
  16. if len(streams) != 6 {
  17. t.Fatalf("wanted 6 streams, got %d: %v", len(streams), streams)
  18. }
  19. }
  20. func TestExtractPlaylistLinks(t *testing.T) {
  21. html := `
  22. <a href="https://example.com/stream.m3u">m3u</a>
  23. <a href="https://example.com/playlist.pls">pls</a>
  24. <a href="https://example.com/radio.xspf">xspf</a>
  25. <a href="https://example.com/data.json">json</a>
  26. `
  27. links := ExtractPlaylistLinks(html)
  28. if len(links) != 4 {
  29. t.Fatalf("wanted 4 playlist links, got %d: %v", len(links), links)
  30. }
  31. }
  32. func TestParsePlaylist(t *testing.T) {
  33. m3u := "#EXTM3U\nhttps://example.com/live.mp3\n"
  34. pls := "[playlist]\nFile1=https://example.com/stream.aac\n"
  35. xspf := "<playlist><location>https://example.com/hls.m3u8</location></playlist>"
  36. if len(ParsePlaylist(m3u, "audio/x-mpegurl")) != 1 {
  37. t.Fatal("expected m3u playlist to yield 1 stream")
  38. }
  39. if len(ParsePlaylist(pls, "audio/x-scpls")) != 1 {
  40. t.Fatal("expected pls playlist to yield 1 stream")
  41. }
  42. if len(ParsePlaylist(xspf, "application/xspf+xml")) != 1 {
  43. t.Fatal("expected xspf playlist to yield 1 stream")
  44. }
  45. }